blib/lib/Fetch/Loop/Hyperman.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 166 CC 15 SCAR 34.5
CC
15
Cov
0%
_ft_arm
90.0
_sweep
20.0
_ft_timer
6.0
CRAP 31.6
1 package Fetch::Loop::Hyperman;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.29';
7  
8 use parent -norequire, 'Fetch::Loop';
9 use Fetch::Loop ();
10 use Time::HiRes ();
11  
12 sub new {
13 my ($class, $loop) = @_;
14 $loop ||= do { require Hyperman::Loop; Hyperman::Loop->new };
15 return bless { loop => $loop, fd => {} }, $class;
16 }
17  
18 sub loop { $_[0]{loop} }
19  
20 sub _ft_arm {
21 my ($self, $fd, $mask, $cv) = @_;
22 my $loop = $self->{loop};
23 my $st = $self->{fd}{$fd};
24  
25 if (!$mask) {
26 if ($st) {
27 $loop->unwatch_io($st->{r}) if $st->{r};
28 $loop->unwatch_io($st->{w}) if $st->{w};
29 delete $self->{fd}{$fd};
30 }
31 return;
32 }
33  
34 $st ||= $self->{fd}{$fd} = {};
35  
36 if ($mask & Fetch::Loop::FT_READ) {
37 $st->{r} ||= $loop->watch_io($fd, 'r', sub { $cv->() });
38 } elsif ($st->{r}) {
39 $loop->unwatch_io(delete $st->{r});
40 }
41 if ($mask & Fetch::Loop::FT_WRITE) {
42 $st->{w} ||= $loop->watch_io($fd, 'w', sub { $cv->() });
43 } elsif ($st->{w}) {
44 $loop->unwatch_io(delete $st->{w});
45 }
46 return;
47 }
48  
49 # Per-request deadlines are not armed as one kernel timer each: under load that
50 # is two hot paths at once - a kevent syscall to arm every request, and (since a
51 # normally-completing request only marks its timer cancelled) a kqueue that
52 # accumulates one live ~timeout-long timer per request. Instead we keep the
53 # pending deadlines in a plain list and run a SINGLE coarse repeating timer that
54 # sweeps it, firing whatever is due. One kernel timer total, re-armed only while
55 # deadlines are outstanding, so an idle loop stops ticking. The cost is up to
56 # SWEEP granularity of slack before a timeout fires - immaterial for the
57 # second(s)-scale request timeouts this exists to enforce.
58 use constant _SWEEP => 0.1; # sweep granularity (seconds)
59  
60 sub _ft_timer {
61 my ($self, $secs, $cv) = @_;
62 my $g = { at => Time::HiRes::time() + $secs, cv => $cv, cancelled => 0 };
63 push @{ $self->{timers} }, $g;
64 unless ($self->{sweeping}) {
65 $self->{sweeping} = 1;
66 $self->_arm_sweep;
67 }
68 return $g;
69 }
70  
71 sub _ft_untimer {
72 my ($self, $g) = @_;
73 $g->{cancelled} = 1 if ref $g;
74 return;
75 }
76  
77 sub _arm_sweep {
78 my ($self) = @_;
79 $self->{loop}->timer(_SWEEP, sub { $self->_sweep });
80 return;
81 }
82  
83 sub _sweep {
84 my ($self) = @_;
85 my $now = Time::HiRes::time();
86 my $due = delete $self->{timers} || [];
87 $self->{timers} = []; # a fired timeout may enqueue a fresh request
88 for my $g (@$due) {
89 next if $g->{cancelled};
90 if ($g->{at} <= $now) { $g->{cancelled} = 1; $g->{cv}->() }
91 else { push @{ $self->{timers} }, $g }
92 }
93 if (@{ $self->{timers} }) { $self->_arm_sweep }
94 else { $self->{sweeping} = 0 }
95 return;
96 }
97  
98 sub _ft_await {
99 my ($self, $f) = @_;
100 return if $f->is_ready;
101 my $loop = $self->{loop};
102 $f->on_ready(sub { $loop->stop });
103 $loop->run;
104 return;
105 }
106  
107 sub install_await {
108 my ($self) = @_;
109 $Fetch::Future::AWAIT = sub { $self->_ft_await($_[0]) };
110 return $self;
111 }
112  
113 1;
114  
115 __END__