Mojo/Reactor/Poll.pm

filter: Statementstmt 100% 101 / 101 Branchbran 89.2% 50 / 56 Conditioncond 62.5% 15 / 24 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 22 / 22 Podpod 100% 12 / 12 totaltotal 89.5% 205 / 229 CC 38 SCAR 24.4
CC
38
Cov
92%
one_tick
24.9
watch
4.0
remove
3.0
CRAP 11.4
« Mojolicious/Plugin/DefaultHelpers.pm ↑ Summary Mojolicious/Plugin/TagHelpers.pm »
1 package Mojo::Reactor::Poll;
2 33 ▶use Mojo::Base 'Mojo::Reactor';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 33 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use IO::Poll qw(POLLERR POLLHUP POLLIN POLLNVAL POLLOUT POLLPRI);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Mojo::Util qw(md5_sum steady_time);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 33 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 10613 ▶sub again {
Subroutine again: called CC=3 SCAR=11.0
Pod documented
11 10613 my ($self, $id, $after) = @_;
12 10613 ▶ croak 'Timer not active' unless my $timer = $self->{timers}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless my $timer = $self->{"timers"}{$id} 1 10612 10613
13 10612 ▶ $timer->{after} = $after if defined $after;
Branch: 1 branch
BranchTrueFalseTotal
if defined $after 510 10102 10612
14 10612 $timer->{time} = steady_time + $timer->{after};
15 }
16  
17 236 ▶sub io {
Subroutine io: called CC=2 SCAR=7.1
Pod documented
18 236 my ($self, $handle, $cb) = @_;
19 236 ▶ $self->{io}{fileno($handle) // croak 'Handle is closed'} = {cb => $cb};
Condition50%1/2
fileno $handle // croak("Handle is closed")
l!l
2360
MC/DC0%0/1
fileno $handle // croak("Handle is closed")
fileno $handle
Truth table50%1/2
A
A= fileno $handle
Aresult
11
00
20 236 return $self->watch($handle, 1, 1);
21 }
22  
23 617 ▶sub is_running { !!shift->{running} }
Subroutine is_running: called CC=1 SCAR=0.0
Pod documented
24  
25 839 ▶sub next_tick {
Subroutine next_tick: called CC=2 SCAR=7.0
Pod documented
26 839 my ($self, $cb) = @_;
27 839 push @{$self->{next_tick}}, $cb;
28 839 ▶ $self->{next_timer} //= $self->timer(0 => \&_next);
Condition66%2/3
$self->{"next_timer"} //= $self->timer(0, \&Mojo::Reactor::Poll::_next)
l!l&&r!l&&!r
5492900
MC/DC0%0/2
$self->{"next_timer"} //= $self->timer(0, \&Mojo::Reactor::Poll::_next)
$self->{"next_timer"} $self->timer(0, \&Mojo::Reactor::Poll::_next)
Truth table66%2/3
A //= B
A= $self->{"next_timer"}B= $self->timer(0, \&Mojo::Reactor::Poll::_next)
ABresult
1X1
011
000
29 839 return undef;
30 }
31  
32 166266 ▶sub one_tick {
Subroutine one_tick: called CC=24 SCAR=32.2
Pod documented
33 166266 my $self = shift;
34  
35 # Just one tick
36 166266 ▶ local $self->{running} = 1 unless $self->{running};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"running"} 1061 165205 166266
37  
38 # Wait for one event
39 166266 my $i;
40 166266 ▶ until ($i || !$self->{running}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($i or not $self->{"running"}) 166266 166257 332523
Condition66%2/3
$i or not $self->{"running"}
l!l&&r!l&&!r
1662570166266
MC/DC50%1/2
$i or not $self->{"running"}
$i not $self->{"running"}
Truth table66%2/3
A or not(B)
A= $iB= not $self->{"running"}
ABresult
1X1
011
000
41  
42 # Stop automatically if there is nothing to watch
43 166266 ▶ return $self->stop unless keys %{$self->{timers}} || keys %{$self->{io}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"timers"};} or keys %{$$self{"io"};} 9 166257 166266
Condition100%3/3
keys %{$$self{"timers"};} or keys %{$$self{"io"};}
l!l&&r!l&&!r
16625619
MC/DC100%2/2
keys %{$$self{"timers"};} or keys %{$$self{"io"};}
keys %{$$self{"timers"};} keys %{$$self{"io"};}
Truth table100%3/3
A or B
A= keys %{$$self{"timers"};}B= keys %{$$self{"io"};}
ABresult
1X1
011
000
44  
45 # Calculate ideal timeout based on timers and round up to next millisecond
46 166257 my $min = min map { $_->{time} } values %{$self->{timers}};
47 166257 ▶ my $timeout = defined $min ? $min - steady_time : 0.5;
Branch: 1 branch
BranchTrueFalseTotal
defined $min ? : 166256 1 166257
48 166257 ▶ $timeout = $timeout <= 0 ? 0 : int($timeout * 1000) + 1;
Branch: 1 branch
BranchTrueFalseTotal
$timeout <= 0 ? : 103607 62650 166257
49  
50 # I/O
51 166257 ▶ if (keys %{$self->{io}}) {
Branch: 2 branches
BranchTrueFalseTotal
if (keys %{$$self{'io'};}) { } 66273 99984 166257
elsif ($timeout) { } 18 99966 99984
52 66273 my @poll = map { $_ => $self->{io}{$_}{mode} } keys %{$self->{io}};
53  
54 # This may break in the future, but is worth it for performance
55 66273 ▶ if (IO::Poll::_poll($timeout, @poll) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (IO::Poll::_poll($timeout, @poll) > 0) 66073 200 66273
56 66073 ▶ while (my ($fd, $mode) = splice @poll, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@poll, 0, 2)) 105610 66073 171683
57  
58 105610 ▶ if ($mode & (POLLIN | POLLPRI | POLLNVAL | POLLHUP | POLLERR)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode & 59) 60791 44819 105610
59 60791 ▶ next unless my $io = $self->{io}{$fd};
Branch: 1 branch
BranchTrueFalseTotal
unless my $io = $self->{"io"}{$fd} 0 60791 60791
60 60791 ▶ ++$i and $self->_try('I/O watcher', $io->{cb}, 0);
Branch: 1 branch
BranchTrueFalseTotal
++$i and $self->_try("I/O watcher", $io->{"cb"}, 0) 60791 0 60791
61 }
62 105610 ▶ next unless $mode & POLLOUT && (my $io = $self->{io}{$fd});
Branch: 1 branch
BranchTrueFalseTotal
unless $mode & 4 and my $io = $self->{"io"}{$fd} 88395 17215 105610
Condition100%3/3
$mode & 4 and my $io = $self->{"io"}{$fd}
!ll&&!rl&&r
88391417215
MC/DC100%2/2
$mode & 4 and my $io = $self->{"io"}{$fd}
$mode & 4 my $io = $self->{"io"}{$fd}
Truth table100%3/3
A and B
A= $mode & 4B= my $io = $self->{"io"}{$fd}
ABresult
0X0
100
111
63 17215 ▶ ++$i and $self->_try('I/O watcher', $io->{cb}, 1);
Branch: 1 branch
BranchTrueFalseTotal
++$i and $self->_try("I/O watcher", $io->{"cb"}, 1) 17215 0 17215
64 }
65 }
66 }
67  
68 # Wait for timeout if poll can't be used
69 18 elsif ($timeout) { usleep($timeout * 1000) }
70  
71 # Timers (time should not change in between timers)
72 166257 my $now = steady_time;
73 166257 for my $id (keys %{$self->{timers}}) {
74 304145 ▶ next unless my $t = $self->{timers}{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless my $t = $self->{"timers"}{$id} 0 304145 304145
75 304145 ▶ next unless $t->{time} <= $now;
Branch: 1 branch
BranchTrueFalseTotal
unless $t->{"time"} <= $now 200480 103665 304145
76  
77 # Recurring timer
78 103665 ▶ if ($t->{recurring}) { $t->{time} = $now + $t->{after} }
Branch: 1 branch
BranchTrueFalseTotal
if ($t->{'recurring'}) { } 103339 326 103665
79  
80 # Normal timer
81 326 else { $self->remove($id) }
82  
83 103665 ▶ ++$i and $self->_try('Timer', $t->{cb}) if $t->{cb};
Branch: 1 branch
BranchTrueFalseTotal
if $t->{"cb"} 103665 0 103665
Condition33%2/6
$t->{'cb'} && (++$i && $self->_try('Timer', $t->{'cb'}))
!ll
01036650
++$i and $self->_try("Timer", $t->{"cb"})
!ll
01036650
MC/DC0%0/3
$t->{'cb'} && (++$i && $self->_try('Timer', $t->{'cb'}))
$t->{'cb'} ++$i $self->_try("Timer", $t->{"cb"})
Truth table0%0/4
A && B and C
A= $t->{'cb'}B= ++$iC= $self->_try("Timer", $t->{"cb"})
ABCresult
0XX0
10X0
1100
1111
84 }
85 }
86 }
87  
88 10 ▶sub recurring { shift->_timer(1, @_) }
Subroutine recurring: called CC=1 SCAR=0.0
Pod documented
89  
90 733 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.1
Pod documented
91 733 my ($self, $remove) = @_;
92 733 ▶ return !!delete $self->{timers}{$remove} unless ref $remove;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $remove 529 204 733
93 204 ▶ return !!delete $self->{io}{fileno($remove) // croak 'Handle is closed'};
Condition50%1/2
fileno $remove // croak("Handle is closed")
l!l
2040
MC/DC0%0/1
fileno $remove // croak("Handle is closed")
fileno $remove
Truth table50%1/2
A
A= fileno $remove
Aresult
11
00
94 }
95  
96 7 ▶sub reset { delete @{shift()}{qw(events io next_tick next_timer running timers)} }
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
97  
98 236 ▶sub start {
Subroutine start: called CC=3 SCAR=11.1
Pod documented
99 236 my $self = shift;
100 236 ▶ local $self->{running} = ($self->{running} || 0) + 1;
Condition50%1/2
$self->{'running'} || 0
l!l
0236
MC/DC0%0/1
$self->{'running'} || 0
$self->{'running'}
Truth table50%1/2
A
A= $self->{'running'}
Aresult
11
00
101 236 ▶ $self->one_tick while $self->{running};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"running"} and do { $self->one_tick } 165204 236 165440
102 }
103  
104 240 ▶sub stop { delete shift->{running} }
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
105  
106 459 ▶sub timer { shift->_timer(0, @_) }
Subroutine timer: called CC=1 SCAR=0.0
Pod documented
107  
108 10377 ▶sub watch {
Subroutine watch: called CC=4 SCAR=13.9
Pod documented
109 10377 my ($self, $handle, $read, $write) = @_;
110  
111 10377 ▶ croak 'I/O watcher not active' unless my $io = $self->{io}{fileno $handle};
Branch: 1 branch
BranchTrueFalseTotal
unless my $io = $self->{"io"}{fileno $handle} 1 10376 10377
112 10376 $io->{mode} = 0;
113 10376 ▶ $io->{mode} |= POLLIN | POLLPRI if $read;
Branch: 1 branch
BranchTrueFalseTotal
if $read 10330 46 10376
114 10376 ▶ $io->{mode} |= POLLOUT if $write;
Branch: 1 branch
BranchTrueFalseTotal
if $write 9895 481 10376
115  
116 10376 return $self;
117 }
118  
119 469 ▶sub _id {
Subroutine _id: called CC=2 SCAR=7.0
120 469 my $self = shift;
121 469 my $id;
122 469 ▶ do { $id = md5_sum 't' . steady_time . rand } while $self->{timers}{$id};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"timers"}{$id} and do { do { $id = md5_sum('t' . &steady_time() . (rand)) } } 0 469 469
123 469 return $id;
124 }
125  
126 283 ▶sub _next {
Subroutine _next: called CC=2 SCAR=6.9
127 283 my $self = shift;
128 283 delete $self->{next_timer};
129 283 ▶ while (my $cb = shift @{$self->{next_tick}}) { $self->$cb }
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = shift @{$$self{"next_tick"};}) 838 283 1121
130 }
131  
132 469 ▶sub _timer {
Subroutine _timer: called CC=1 SCAR=0.0
133 469 my ($self, $recurring, $after, $cb) = @_;
134 469 my $id = $self->_id;
135 469 $self->{timers}{$id} = {cb => $cb, after => $after, recurring => $recurring, time => steady_time + $after};
136 469 return $id;
137 }
138  
139 181671 ▶sub _try {
Subroutine _try: called CC=2 SCAR=6.9
140 181671 my ($self, $what, $cb) = (shift, shift, shift);
141 181671 ▶ eval { $self->$cb(@_); 1 } or $self->emit(error => "$what failed: $@");
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->$cb(@_); 1 } } or $self->emit("error", "$what failed: $@") 181669 2 181671
142 }
143  
144 33 1;
145  
146 =encoding utf8
147  
148 =head1 NAME
149  
150 Mojo::Reactor::Poll - Low-level event reactor with poll support
151  
152 =head1 SYNOPSIS
153  
154 use Mojo::Reactor::Poll;
155  
156 # Watch if handle becomes readable or writable
157 my $reactor = Mojo::Reactor::Poll->new;
158 $reactor->io($first => sub ($reactor, $writable) {
159 say $writable ? 'First handle is writable' : 'First handle is readable';
160 });
161  
162 # Change to watching only if handle becomes writable
163 $reactor->watch($first, 0, 1);
164  
165 # Turn file descriptor into handle and watch if it becomes readable
166 my $second = IO::Handle->new_from_fd($fd, 'r');
167 $reactor->io($second => sub ($reactor, $writable) {
168 say $writable ? 'Second handle is writable' : 'Second handle is readable';
169 })->watch($second, 1, 0);
170  
171 # Add a timer
172 $reactor->timer(15 => sub ($reactor) {
173 $reactor->remove($first);
174 $reactor->remove($second);
175 say 'Timeout!';
176 });
177  
178 # Start reactor if necessary
179 $reactor->start unless $reactor->is_running;
180  
181 =head1 DESCRIPTION
182  
183 L<Mojo::Reactor::Poll> is a low-level event reactor based on L<IO::Poll>.
184  
185 =head1 EVENTS
186  
187 L<Mojo::Reactor::Poll> inherits all events from L<Mojo::Reactor>.
188  
189 =head1 METHODS
190  
191 L<Mojo::Reactor::Poll> inherits all methods from L<Mojo::Reactor> and implements the following new ones.
192  
193 =head2 again
194  
195 $reactor->again($id);
196 $reactor->again($id, 0.5);
197  
198 Restart timer and optionally change the invocation time. Note that this method requires an active timer.
199  
200 =head2 io
201  
202 $reactor = $reactor->io($handle => sub {...});
203  
204 Watch handle for I/O events, invoking the callback whenever handle becomes readable or writable.
205  
206 # Callback will be executed twice if handle becomes readable and writable
207 $reactor->io($handle => sub ($reactor, $writable) {
208 say $writable ? 'Handle is writable' : 'Handle is readable';
209 });
210  
211 =head2 is_running
212  
213 my $bool = $reactor->is_running;
214  
215 Check if reactor is running.
216  
217 =head2 next_tick
218  
219 my $undef = $reactor->next_tick(sub {...});
220  
221 Execute callback as soon as possible, but not before returning or other callbacks that have been registered with this
222 method, always returns C<undef>.
223  
224 =head2 one_tick
225  
226 $reactor->one_tick;
227  
228 Run reactor until an event occurs or no events are being watched anymore.
229  
230 # Don't block longer than 0.5 seconds
231 my $id = $reactor->timer(0.5 => sub {});
232 $reactor->one_tick;
233 $reactor->remove($id);
234  
235 =head2 recurring
236  
237 my $id = $reactor->recurring(0.25 => sub {...});
238  
239 Create a new recurring timer, invoking the callback repeatedly after a given amount of time in seconds.
240  
241 =head2 remove
242  
243 my $bool = $reactor->remove($handle);
244 my $bool = $reactor->remove($id);
245  
246 Remove handle or timer.
247  
248 =head2 reset
249  
250 $reactor->reset;
251  
252 Remove all handles and timers.
253  
254 =head2 start
255  
256 $reactor->start;
257  
258 Start watching for I/O and timer events, this will block until L</"stop"> is called or no events are being watched
259 anymore.
260  
261 # Start reactor only if it is not running already
262 $reactor->start unless $reactor->is_running;
263  
264 =head2 stop
265  
266 $reactor->stop;
267  
268 Stop watching for I/O and timer events.
269  
270 =head2 timer
271  
272 my $id = $reactor->timer(0.5 => sub {...});
273  
274 Create a new timer, invoking the callback after a given amount of time in seconds.
275  
276 =head2 watch
277  
278 $reactor = $reactor->watch($handle, $readable, $writable);
279  
280 Change I/O events to watch handle for with true and false values. Note that this method requires an active I/O watcher.
281  
282 # Watch only for readable events
283 $reactor->watch($handle, 1, 0);
284  
285 # Watch only for writable events
286 $reactor->watch($handle, 0, 1);
287  
288 # Watch for readable and writable events
289 $reactor->watch($handle, 1, 1);
290  
291 # Pause watching for events
292 $reactor->watch($handle, 0, 0);
293  
294 =head1 SEE ALSO
295  
296 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
297  
298 =cut
« Mojolicious/Plugin/DefaultHelpers.pm ↑ Summary Mojolicious/Plugin/TagHelpers.pm »