Im stuck on understanding how EV calculcate timers.
I have this code:
#!/usr/bin/perl
use common::sense;
use AnyEvent::HTTP;
use Data::Dumper;
sub do {
my $iter = shift;
# It will resolve every timeout problems, but it is not question
# AnyEvent->now_update;
my $cv = AnyEvent->condvar;
$cv->begin;
http_get
'https://imasheep.hurrdurr.org/category/about.html',
timeout => 2,
sub {
my ($body, $hdr) = @_;
say "$iter $hdr->{Status}";
unless ($hdr->{Status} == 200) {
warn Data::Dumper->Dump([$hdr],['hdr']);
}
$cv->end;
}
;
my $timer = AnyEvent->timer(
after => 300,
cb => sub { warn "Exit by timer"; $cv->send },
);
my ($n, $t) = (AnyEvent->now, AnyEvent->time);
my $d = $t-$n;
say "$iter diff = $d, time = $t, now = $n";
$cv->recv;
}
for (1..4) {
&do($_);
sleep(5);
}
output:
1 diff = 0.0233771800994873, time = 1427912097.54858, now = 1427912097
+.5252
1 200
2 diff = 5.0014123916626, time = 1427912103.05933, now = 1427912098.05
+791
2 596
$hdr = {
'Reason' => 'Connection timed out',
'URL' => 'https://imasheep.hurrdurr.org/category/about.html',
'Status' => 596
};
3 diff = 5.00508308410645, time = 1427912108.06631, now = 1427912103.0
+6123
3 200
4 diff = 5.00244045257568, time = 1427912113.56622, now = 1427912108.5
+6378
4 596
$hdr = {
'Status' => 596,
'Reason' => 'Connection timed out',
'URL' => 'https://imasheep.hurrdurr.org/category/about.html'
};
Iteration 2 - This is OK for me. Timer was set when internal time has 5 sec lag
Iteration 3 - But I do not understand this case. Everything is the same. Why there is no timeout ???
Iteration 4 - And once again. So every even call to do() generate timeout, and every odd call doesn't generate.
Could you please clarify me about this behaviour?