Remote/MiniLoop.pm

filter: Statementstmt 74.3% 136 / 183 Branchbran 52.2% 23 / 44 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 44.9% 22 / 49 Podpod 0.0% 0 / 10 totaltotal 63.1% 190 / 301 CC 53 SCAR 14.2
CC
53
Cov
70%
__ANON__
12.0
__ANON__
12.0
loop_once
8.5
CRAP 4.1
« Remote/Connection.pm ↑ Summary Remote/Future.pm »
1 package Object::Remote::MiniLoop;
2  
3 26 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use Object::Remote::Logging qw( :log :dlog router );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 router()->exclude_forwarding
10 }
11  
12 # this is ro because we only actually set it using local in sub run
13 has is_running => (is => 'ro', clearer => 'stop');
14 #maximum duration that select() will block - undef means indefinite,
15 #0 means no blocking, otherwise maximum time in seconds
16 has block_duration => ( is => 'rw' );
17  
18 13 ▶has _read_watches => (is => 'ro', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
19 13 ▶has _read_select => (is => 'ro', default => sub { IO::Select->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
20  
21 13 ▶has _write_watches => (is => 'ro', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
22 13 ▶has _write_select => (is => 'ro', default => sub { IO::Select->new });
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 13 ▶has _timers => (is => 'ro', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 40 ▶sub watch_io {
Subroutine watch_io: called CC=3 SCAR=14.9
Pod undocumented
27 0 ▶ my ($self, %watch) = @_;
Subroutine watch_io: not called CC=3 SCAR=14.9
Pod undocumented
28 0 my $fh = $watch{handle};
29 0 ▶ Dlog_debug { "Adding IO watch for $_" } $fh;
Subroutine __ANON__: not called CC=1 SCAR=2.6
30  
31 0 ▶ if (my $cb = $watch{on_read_ready}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cb = $watch{"on_read_ready"}) 0 0 0
if (my $cb = $watch{"on_read_ready"}) 20 20 40
32 0 ▶ log_trace { "IO watcher is registering with select for reading" };
Subroutine __ANON__: not called CC=1 SCAR=2.6
33 0 $self->_read_select->add($fh);
34 0 $self->_read_watches->{$fh} = $cb;
35 }
36 0 ▶ if (my $cb = $watch{on_write_ready}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cb = $watch{"on_write_ready"}) 0 0 0
if (my $cb = $watch{"on_write_ready"}) 20 20 40
37 0 ▶ log_trace { "IO watcher is registering with select for writing" };
Subroutine __ANON__: not called CC=1 SCAR=2.6
38 0 $self->_write_select->add($fh);
39 0 $self->_write_watches->{$fh} = $cb;
40 }
41 0 return;
42 }
43  
44 sub unwatch_io {
45 21 ▶ my ($self, %watch) = @_;
Subroutine unwatch_io: called CC=3 SCAR=11.1
Pod undocumented
46 21 my $fh = $watch{handle};
47 21 ▶ Dlog_debug { "Removing IO watch for $_" } $fh;
Subroutine __ANON__: not called CC=1 SCAR=1.2
48 21 ▶ if ($watch{on_read_ready}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watch{"on_read_ready"}) 2 19 21
49 2 ▶ log_trace { "IO watcher is removing read from select()" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
50 2 $self->_read_select->remove($fh);
51 2 delete $self->_read_watches->{$fh};
52 }
53 21 ▶ if ($watch{on_write_ready}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($watch{"on_write_ready"}) 19 2 21
54 19 ▶ log_trace { "IO watcher is removing write from select()" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
55 19 $self->_write_select->remove($fh);
56 19 delete $self->_write_watches->{$fh};
57 }
58 21 return;
59 }
60  
61 sub _sort_timers {
62 27 ▶ my ($self, @new) = @_;
Subroutine _sort_timers: called CC=1 SCAR=0.0
63 27 my $timers = $self->_timers;
64  
65 27 ▶ log_trace { "Sorting timers" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
66  
67 27 @{$timers} = sort { $a->[0] <=> $b->[0] } @{$timers}, @new;
68 27 return;
69 }
70  
71 sub watch_time {
72 27 ▶ my ($self, %watch) = @_;
Subroutine watch_time: called CC=4 SCAR=15.9
Pod undocumented
73 27 my $at;
74  
75 27 ▶ Dlog_trace { "watch_time() invoked with $_" } \%watch;
Subroutine __ANON__: not called CC=1 SCAR=1.2
76  
77 27 ▶ if (exists($watch{after})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $watch{'after'}) { } 27 0 27
elsif (exists $watch{'at'}) { } 0 0 0
78 27 $at = time() + $watch{after};
79 } elsif (exists($watch{at})) {
80 0 $at = $watch{at};
81 } else {
82 0 die "watch_time requires after or at";
83 }
84  
85 27 ▶ die "watch_time requires code" unless my $code = $watch{code};
Branch: 1 branch
BranchTrueFalseTotal
unless my $code = $watch{"code"} 0 27 27
86 27 my $timers = $self->_timers;
87 27 my $new = [ $at => $code ];
88 27 $self->_sort_timers($new);
89 27 ▶ log_debug { "Created new timer with id '$new' that expires at '$at'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
90 27 return "$new";
91 }
92  
93 sub unwatch_time {
94 1 ▶ my ($self, $id) = @_;
Subroutine unwatch_time: called CC=2 SCAR=7.1
Pod undocumented
95 1 ▶ log_trace { "Removing timer with id of '$id'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
96 1 @$_ = grep !($_ eq $id), @$_ for $self->_timers;
97 1 return;
98 }
99  
100 sub _next_timer_expires_delay {
101 1204 ▶ my ($self) = @_;
Subroutine _next_timer_expires_delay: called CC=5 SCAR=18.9
102 1204 my $timers = $self->_timers;
103 1204 my $delay_max = $self->block_duration;
104  
105 1204 ▶ return $delay_max unless @$timers;
Branch: 1 branch
BranchTrueFalseTotal
unless @$timers 0 1204 1204
106 1204 my $duration = $timers->[0]->[0] - time;
107  
108 1204 ▶ log_trace { "next timer fires in '$duration' seconds" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
109  
110 1204 ▶ if ($duration < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($duration < 0) { } 0 1204 1204
elsif (defined $delay_max and $duration > $delay_max) { } 0 2408 2408
111 0 $duration = 0;
112 ▶ } elsif (defined $delay_max && $duration > $delay_max) {
Condition33%1/3
defined $delay_max and $duration > $delay_max
!ll&&!rl&&r
120400
MC/DC0%0/2
defined $delay_max and $duration > $delay_max
defined $delay_max $duration > $delay_max
Truth table33%1/3
A and B
A= defined $delay_maxB= $duration > $delay_max
ABresult
0X0
100
111
113 0 $duration = $delay_max;
114 }
115  
116 1204 return $duration;
117 }
118  
119 sub loop_once {
120 1204 ▶ my ($self) = @_;
Subroutine loop_once: called CC=7 SCAR=21.4
Pod undocumented
121 1204 my $read = $self->_read_watches;
122 1204 my $write = $self->_write_watches;
123 1204 my $read_count = 0;
124 1204 my $write_count = 0;
125 1204 my @c = caller;
126 1204 my $wait_time = $self->_next_timer_expires_delay;
127 log_trace {
128 0 ▶ sprintf("Run loop: loop_once() has been invoked by $c[1]:$c[2] with read:%i write:%i select timeout:%s",
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $wait_time ? : 0 0 0
129 scalar(keys(%$read)), scalar(keys(%$write)), defined $wait_time ? $wait_time : 'indefinite' )
130 1204 };
131 1204 my ($readable, $writeable) = IO::Select->select(
132 $self->_read_select, $self->_write_select, undef, $wait_time
133 );
134 log_trace {
135 0 ▶ my $readable_count = defined $readable ? scalar(@$readable) : 0;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
defined $readable ? : 0 0 0
136 0 ▶ my $writable_count = defined $writeable ? scalar(@$writeable) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $writeable ? : 0 0 0
137 0 "Run loop: select returned readable:$readable_count writeable:$writable_count";
138 1204 };
139 # I would love to trap errors in the select call but IO::Select doesn't
140 # differentiate between an error and a timeout.
141 # -- no, love, mst.
142  
143 1204 ▶ log_trace { "Reading from ready filehandles" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
144 1204 foreach my $fh (@$readable) {
145 188 ▶ next unless $read->{$fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $read->{$fh} 0 188 188
146 188 $read_count++;
147 188 $read->{$fh}();
148 #FIXME this is a rough workaround for race conditions that can cause deadlocks
149 #under load
150 188 last;
151 }
152 1204 ▶ log_trace { "Writing to ready filehandles" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
153 1204 foreach my $fh (@$writeable) {
154 1009 ▶ next unless $write->{$fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $write->{$fh} 0 1009 1009
155 1009 $write_count++;
156 1009 $write->{$fh}();
157 #FIXME this is a rough workaround for race conditions that can cause deadlocks
158 #under load
159 1009 last;
160 }
161  
162 #moving the timers above the read() section exposes a deadlock
163 1204 ▶ log_trace { "Read from $read_count filehandles; wrote to $write_count filehandles" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
164 1204 my $timers = $self->_timers;
165 1204 my $now = time();
166 1204 ▶ log_trace { "Checking timers" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
167 1204 ▶ while (@$timers and $timers->[0][0] <= $now) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$timers and $timers->[0][0] <= $now) 7 1204 1211
Condition100%3/3
@$timers and $timers->[0][0] <= $now
!ll&&!rl&&r
212027
MC/DC100%2/2
@$timers and $timers->[0][0] <= $now
@$timers $timers->[0][0] <= $now
Truth table100%3/3
A and B
A= @$timersB= $timers->[0][0] <= $now
ABresult
0X0
100
111
168 7 my $active = $timers->[0];
169 7 ▶ Dlog_trace { "Found timer that needs to be executed: '$active'" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
170 7 shift(@$timers);
171  
172 #execute the timer
173 7 $active->[1]->();
174 }
175  
176 1204 ▶ log_trace { "Run loop: single loop is completed" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
177 1204 return;
178 }
179  
180 sub run {
181 170 ▶ my ($self) = @_;
Subroutine run: called CC=2 SCAR=7.1
Pod undocumented
182 170 ▶ log_trace { "Run loop: run() invoked" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
183 170 local $self->{is_running} = 1;
184 170 ▶ while ($self->is_running) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_running) 1204 170 1374
185 1204 $self->loop_once;
186 }
187 170 ▶ log_trace { "Run loop: run() completed" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
188 170 return;
189 }
190  
191 sub new_future {
192 304 ▶ return Future->new;
Subroutine new_future: called CC=1 SCAR=0.0
Pod undocumented
193 }
194  
195 our @await;
196  
197 sub await {
198 170 ▶ my ($self, $f) = @_;
Subroutine await: called CC=4 SCAR=14.9
Pod undocumented
199 170 ▶ log_trace { my $ir = $f->is_ready; "await_future() invoked; is_ready: $ir" };
Subroutine __ANON__: not called CC=1 SCAR=2.6
200 170 ▶ return $f if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 170 170
201 170 require Object::Remote;
202 170 my $loop = Object::Remote->current_loop;
203 {
204 170 local @await = (@await, $f);
205 $f->on_ready(sub {
206 170 ▶ log_trace { my $l = @await; "future has become ready, length of \@await: '$l'" };
Subroutine __ANON__: called CC=2 SCAR=8.5
207 170 ▶ if ($f == $await[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f == $Object::Remote::MiniLoop::await[-1]) 170 0 170
208 0 ▶ log_trace { "This future is not waiting on anything so calling stop on the run loop" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 0 $loop->stop;
210 }
211 170 });
212 170 ▶ log_trace { "Starting run loop for newly created future" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
213 170 $loop->run;
214 }
215 170 ▶ if (@await and $await[-1]->is_ready) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Object::Remote::MiniLoop::await and $Object::Remote::MiniLoop::await[-1]->is_ready) 57 113 170
Condition66%2/3
@Object::Remote::MiniLoop::await and $Object::Remote::MiniLoop::await[-1]->is_ready
!ll&&!rl&&r
113057
MC/DC50%1/2
@Object::Remote::MiniLoop::await and $Object::Remote::MiniLoop::await[-1]->is_ready
@Object::Remote::MiniLoop::await $Object::Remote::MiniLoop::await[-1]->is_ready
Truth table66%2/3
A and B
A= @Object::Remote::MiniLoop::awaitB= $Object::Remote::MiniLoop::await[-1]->is_ready
ABresult
0X0
100
111
216 57 ▶ log_trace { "Last future in await list was ready, stopping run loop" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
217 57 $loop->stop;
218 }
219 170 ▶ log_trace { "await_future() returning" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
220 170 return $f;
221 }
222  
223 sub await_all {
224 2 ▶ my $self = shift;
Subroutine await_all: called CC=1 SCAR=0.0
Pod undocumented
225 2 $self->await(Future->wait_all(@_));
226 2 return;
227 }
228  
229 1;
« Remote/Connection.pm ↑ Summary Remote/Future.pm »