Resolver.pm

filter: Statementstmt 76.9% 250 / 325 Branchbran 55.3% 73 / 132 Conditioncond 46.3% 19 / 41 MC/DCmcdc 8.0% 2 / 25 Subroutinesub 79.3% 23 / 29 Podpod 18.1% 2 / 11 totaltotal 65.5% 369 / 563 CC 98 SCAR 32.5
CC
98
Cov
69%
process_results
61.3
_preexport
43.1
shrink_pool
30.0
CRAP 25.7
↑ Summary Resolver/IO/Async.pm »
1 package Socket::More::Resolver;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use feature qw<say state>;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 our $VERSION="v0.1.3";
9  
10 2 ▶use constant::more DEBUG=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use constant::more qw<CMD_GAI=0 CMD_GNI CMD_SPAWN CMD_KILL CMD_REAP>;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use constant::more qw<WORKER_ID=0 WORKER_READ WORKER_WRITE WORKER_CREAD WORKER_CWRITE WORKER_QUEUE WORKER_BUSY>;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use constant::more qw<REQ_CMD=0 REQ_ID REQ_DATA REQ_CB REQ_ERR REQ_WORKER>;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Export::These qw<getaddrinfo getnameinfo close_pool>;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Socket::More::Lookup ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 my $gai_data_pack="l> l> l> l> l>/a* l>/a*";
21  
22 #REQID and as above
23 #
24 2 my $gai_pack="($gai_data_pack)*";
25  
26  
27  
28 sub _results_available;
29 sub process_results;
30 sub getaddrinfo;
31 sub shrink_pool;
32 sub monitor_workers;
33 2 my $i=0; # Sequential ID of requests
34  
35 2 my $in_flight=0;
36  
37 #my @pool_free; # pids (keys) of workers we can use
38 2 my $pool_max=4;
39 2 my $enable_shrink;
40  
41  
42 my @pairs; # file handles for parent/child pipes
43 # preallocated with first import of this module
44  
45 #my $template_pid;
46  
47 2 our $Shared;
48  
49 2 my %fd_worker_map;
50  
51  
52  
53 # In the pre export, we start the workers if not already started.
54 # Also detect event system.
55 #
56 2 ▶sub _preexport {
Subroutine _preexport: called CC=18 SCAR=37.6
57 2 shift; shift;
58  
59  
60 2 my %options=map %$_, grep ref, @_;
61
62 #my @imports=map %$_, grep !ref, @_;
63
64
65 # Don't generate pairs if they already exist
66 2 ▶ if(!@pairs){
Branch: 1 branch
BranchTrueFalseTotal
unless (@pairs) 2 0 2
67  
68 2 ▶ $pool_max=($options{max_workers}//4);
Condition50%1/2
$options{"max_workers"} // 4
l!l
20
MC/DC0%0/1
$options{"max_workers"} // 4
$options{"max_workers"}
Truth table50%1/2
A
A= $options{"max_workers"}
Aresult
11
00
69 2 ▶ $pool_max=4 if $pool_max <=0;
Branch: 1 branch
BranchTrueFalseTotal
if $pool_max <= 0 0 2 2
70 2 $pool_max++;
71 2 $enable_shrink=$options{enable_shrink};
72  
73  
74 #pre allocate enough pipes for full pool
75 2 for(1..$pool_max){
76 12 pipe my $c_read, my $p_write;
77 12 pipe my $p_read, my $c_write;
78 12 fcntl $c_read, F_SETFD, 0; #Make sure we clear CLOSEXEC
79 12 fcntl $c_write, F_SETFD,0;
80  
81 12 push @pairs,[0, $p_read, $p_write, $c_read, $c_write, [], 0];
82 }
83  
84  
85  
86  
87 # Create the template process here. This is the first worker
88 #Need to bootstrap/ create the first worker, which is used as a template
89 2 DEBUG and say STDERR "Create worker: Bootrapping first/template worker";
90 2 spawn_template();
91  
92 # Prefork
93  
94 1 ▶ if($options{prefork}){
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"prefork"}) 0 1 1
95 0 for(1..($pool_max-1)){
96 0 unshift $pairs[0][WORKER_QUEUE]->@*, [CMD_SPAWN, $i++, $_];
97 0 $in_flight++;
98 }
99 }
100  
101 # Work with event systems
102 1 my $sub;
103  
104 1 my @search=qw<AE IO::Async Mojo::IOLoop>; # Built in drivers
105 1 ▶ for($options{loop_driver}//()){
Condition33%1/3
$options{"loop_driver"} // ()
l!l&&r!l&&!r
001
MC/DC0%0/2
$options{"loop_driver"} // ()
$options{"loop_driver"} ()
Truth table33%1/3
A // B
A= $options{"loop_driver"}B= ()
ABresult
1X1
011
000
106 0 ▶ if(ref eq "CODE"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq "CODE") 0 0 0
107 0 $sub=$_;
108 }
109 0 ▶ if(ref eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ eq 'ARRAY') { } 0 0 0
110 0 unshift @search, @$_;
111 }
112 else {
113 #Assume a string
114 0 unshift @search, $_;
115 }
116 }
117  
118 1 ▶ if($options{no_loop}){
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'no_loop'}) { } 0 1 1
119 # Prevent event loop integration
120 0 $sub=undef;
121 }
122 else{
123 # Use search list
124 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
125 1 for(@search){
126 3 ▶ if(%{$_."::"}){
Branch: 1 branch
BranchTrueFalseTotal
if (%{$_ . '::';}) 0 3 3
127 0 $sub=eval "require Socket::More::Resolver::$_";
128 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
129 0 last;
130 }
131 }
132 }
133 1 ▶ $sub->() if($sub);
Branch: 1 branch
BranchTrueFalseTotal
if $sub 0 1 1
134 #grep !ref, @_;
135 }
136 1 ▶ if($options{prefork}){
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"prefork"}) 0 1 1
137 0 getaddrinfo for(1..($pool_max-1));
138 }
139 1 @_;
140 }
141  
142 1 ▶sub _reexport {
Subroutine _reexport: called CC=1 SCAR=0.0
143 1 Socket::More::Lookup->import("gai_strerror");
144 }
145  
146  
147  
148  
149  
150  
151 #If used as a module, setup the process pool
152  
153 #getaddrinfo Request
154 #REQID FLAGS FAMILY TYPE PROTOCOL HOST PORT
155  
156 #getaddrinfo response
157 #FLAG/ERROR FAMILY TYPE PROTOCOL ADDR CANONNNAME
158 #
159  
160 # Return undef when no worker available.
161 # If under limit, a new worker is spawned for next run
162 # Return the worker struct to use otherwise
163 #
164 4 ▶sub _get_worker{
Subroutine _get_worker: called CC=11 SCAR=25.4
165  
166 #_results_available unless $Shared;
167 4 my $worker;
168 my $fallback;
169 4 my $unspawned;
170 4 my $index;
171 4 my $busy_count=0;
172 4 state $robin=1;
173 4 for(1..$#pairs){
174 12 $index=$_;
175 12 $worker=$pairs[$index];
176 12 ▶ if($worker->[WORKER_BUSY]){
Branch: 1 branch
BranchTrueFalseTotal
if ($$worker['6']) { } 1 11 12
177 1 ▶ if($worker->[WORKER_ID]){
Branch: 1 branch
BranchTrueFalseTotal
if ($$worker[0]) { } 0 1 1
178 0 $busy_count++;
179 # Fully spawned and working on a request
180 0 DEBUG and say STDERR "GETTING WORKER: fully spawned $index";
181 }
182 else {
183 # half spawned, this has at least 1 message
184 # if all other workers are busy we use the first one of these we come accros
185 1 ▶ $fallback//=$index if $worker->[WORKER_QUEUE]->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$$worker[5]} 1 0 1
Condition33%1/3
$fallback //= $index
l!l&&r!l&&!r
010
MC/DC0%0/2
$fallback //= $index
$fallback $index
Truth table33%1/3
A //= B
A= $fallbackB= $index
ABresult
1X1
011
000
186 1 DEBUG and say STDERR "GETTING WORKER: half spawned fallback $index";
187 }
188 }
189 else {
190 # Not busy
191 #
192 11 ▶ if($worker->[WORKER_ID]){
Branch: 1 branch
BranchTrueFalseTotal
if ($$worker[0]) { } 2 9 11
193 # THIS IS THE WORKER WE WANT
194 2 DEBUG and say STDERR "GETTING WORKER: found unbusy $index";
195 2 return $worker;
196 }
197 else{
198 # Not spawned. Use first one we come accross if we need to spawn
199 9 ▶ $unspawned//=$index;
Condition66%2/3
$unspawned //= $index
l!l&&r!l&&!r
720
MC/DC0%0/2
$unspawned //= $index
$unspawned $index
Truth table66%2/3
A //= B
A= $unspawnedB= $index
ABresult
1X1
011
000
200 9 DEBUG and say STDERR "GETTING WORKER: found unspawned $index";
201 }
202 }
203 }
204  
205 # Use the about to be spawned worker
206 2 ▶ return $pairs[$fallback] if defined $fallback;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fallback 1 1 2
207  
208 # Here we actaully need to spawn a worker
209
210 1 my $template_worker=spawn_template(); #ensure template exists
211
212 1 ▶ if($busy_count < (@pairs-1)){
Branch: 1 branch
BranchTrueFalseTotal
if ($busy_count < @pairs - 1) { } 1 0 1
213 1 DEBUG and say STDERR "Queue spawn command to template for inext $unspawned";
214 1 push $template_worker->[WORKER_QUEUE]->@*, [CMD_SPAWN, $i++, $unspawned];
215 1 $index=$unspawned;
216 1 $in_flight++;
217 1 $pairs[$unspawned][WORKER_BUSY]=1;
218 }
219 else{
220 0 $index=$robin++;
221 0 ▶ $robin=1 if $robin >=@pairs;
Branch: 1 branch
BranchTrueFalseTotal
if $robin >= @pairs 0 0 0
222 }
223  
224 #$pairs[$index][WORKER_BUSY]=1;
225 #$pairs[$index][WORKER_ID]=-1;
226 1 $pairs[$index];
227  
228 }
229  
230  
231 sub pool_next;
232 # Serialize messages to worker from queue
233 23 ▶sub pool_next{
Subroutine pool_next: called CC=19 SCAR=30.7
Pod undocumented
234 23 my $w=shift;
235  
236 # handle returns first .. TODO: This is only if no event system is being used
237 23 ▶ _results_available unless $Shared;
Branch: 1 branch
BranchTrueFalseTotal
unless $Socket::More::Resolver::Shared 23 0 23
238 23 my $redo;
239 23 ▶ for($w?$w:@pairs){
Branch: 1 branch
BranchTrueFalseTotal
$w ? : 0 23 23
240 138 DEBUG and say STDERR "POOL next for ".$_->[WORKER_ID]." busy: $_->[WORKER_BUSY], queue; ".$_->[WORKER_QUEUE]->@*;
241 138 my $ofd;
242 # only process worker is initialized not busy and have something to process
243 138 ▶ next unless $_->[WORKER_ID];
Branch: 1 branch
BranchTrueFalseTotal
unless $$_[0] 102 36 138
244 36 ▶ next if $_->[WORKER_BUSY];
Branch: 1 branch
BranchTrueFalseTotal
if $$_[6] 25 11 36
245 11 ▶ next unless $_->[WORKER_QUEUE]->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$$_[5]} 2 9 11
246  
247 9 $_->[WORKER_BUSY]=1;
248  
249 #my $req=shift $_->[WORKER_QUEUE]->@*;
250 9 my $req=$_->[WORKER_QUEUE][0];
251 9 $req->[REQ_WORKER]=$_->[WORKER_ID];
252
253 #$reqs{$req->[REQ_ID]}=$req; #Add to outstanding
254  
255  
256 # Header
257 9 my $out=pack "l> l>", $req->[REQ_CMD], $req->[REQ_ID];
258  
259 # Body
260 9 ▶ if($req->[REQ_CMD]==CMD_SPAWN){
Branch: 5 branches
BranchTrueFalseTotal
if ($$req['0'] == '2') { } 1 8 9
elsif ($$req['0'] == '0') { } 2 6 8
elsif ($$req['0'] == '1') { } 2 4 6
elsif ($$req['0'] == '3') { } 0 4 4
elsif ($$req['0'] == '4') { } 4 0 4
261 # Write to template process
262 #DEBUG and
263 1 my $windex=$req->[2];
264 1 DEBUG and say STDERR ">> SENDING CMD_SPWAN TO WORKER: $req->[REQ_WORKER], worker index $windex";
265 1 my $cread=fileno $pairs[$windex][WORKER_CREAD];
266 1 my $cwrite=fileno $pairs[$windex][WORKER_CWRITE];
267  
268 1 $out.=pack("l> l>", $cread, $cwrite);
269 1 $ofd=$pairs[0][WORKER_WRITE];
270 1 $redo=1;
271 }
272 elsif($req->[REQ_CMD]==CMD_GAI) {
273 # getaddrinfo request
274 2 DEBUG and say STDERR ">> SENDING CMD_GAI TO WORKER: $req->[REQ_WORKER]";
275 2 ▶ if(ref $req->[REQ_DATA] eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$req['2'] eq 'ARRAY') { } 0 2 2
276 0 $out.=pack $gai_pack, $req->[REQ_DATA]->@*;
277 }
278 else {
279 # assume a hash
280 2 for($req->[REQ_DATA]){
281 #$out.=pack $gai_pack, $_->{flags}//0, $_->{family}//0, $_->{socktype}//0, $_->{protocol}//0, $_->{host}, $_->{port};
282 2 ▶ $out.=pack $gai_pack, $_->{flags}//0, $_->{family}//0, $_->{socktype}//0, $_->{protocol}//0, $_->{address}, $_->{port};
Condition50%4/8
$_->{"flags"} // 0
l!l
02
$_->{"family"} // 0
l!l
02
$_->{"socktype"} // 0
l!l
02
$_->{"protocol"} // 0
l!l
02
MC/DC0%0/1
$_->{"flags"} // 0
$_->{"flags"}
MC/DC0%0/1
$_->{"family"} // 0
$_->{"family"}
MC/DC0%0/1
$_->{"socktype"} // 0
$_->{"socktype"}
MC/DC0%0/1
$_->{"protocol"} // 0
$_->{"protocol"}
Truth table50%1/2
A
A= $_->{"flags"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"family"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"socktype"}
Aresult
11
00
Truth table50%1/2
A
A= $_->{"protocol"}
Aresult
11
00
283 }
284 }
285  
286 2 $ofd=$_->[WORKER_WRITE];
287 }
288 elsif($req->[REQ_CMD]==CMD_GNI){
289 2 DEBUG and say STDERR ">> SENDING CMD_GNI TO WORKER: $req->[REQ_WORKER]";
290 2 $out.=pack "l>/A* l>", $req->[REQ_DATA]->@*;
291 2 $ofd=$_->[WORKER_WRITE];
292  
293 }
294 elsif($req->[REQ_CMD]== CMD_KILL){
295 0 DEBUG and say STDERR ">> Sending CMD_KILL to worker: $req->[REQ_WORKER]";
296 0 $ofd=$_->[WORKER_WRITE];
297 0 $redo=1;
298 }
299 elsif($req->[REQ_CMD]== CMD_REAP){
300 4 DEBUG and say STDERR ">> Sending CMD_REAP to worker: $req->[REQ_WORKER]";
301 4 $out.=pack("l>/l>*", $req->[REQ_DATA]->@*);
302 4 $ofd=$pairs[0][WORKER_WRITE];
303 4 $redo=1;
304 }
305 else {
306 0 die "UNkown command in pool_next";
307 }
308  
309 9 DEBUG and say STDERR ">> WRITING WITH FD $ofd";
310 9 syswrite $ofd, unpack("H*", $out)."\n"; # bypass buffering
311  
312 }
313 23 ▶ pool_next if $redo;
Branch: 1 branch
BranchTrueFalseTotal
if $redo 5 18 23
314 }
315  
316  
317 # Peforms a read on the pipe, parses response from worker
318 # and executes callbacks as needed
319 #
320 # This is the routine needing to be called from an event loop
321 # when the pipe is readable
322 #
323 9 ▶sub process_results{
Subroutine process_results: called CC=25 SCAR=41.2
Pod undocumented
324 9 my $fd_or_struct=shift;
325 9 my $worker;
326 9 ▶ if(ref $fd_or_struct){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fd_or_struct) { } 9 0 9
327 9 $worker=$fd_or_struct;
328 }
329 else{
330 0 $worker=$fd_worker_map{$fd_or_struct};
331 }
332 #Check which worker is ready to read.
333 # Read the result
334 #For now we wait.
335 9 my $r=$worker->[WORKER_READ];
336 9 local $_=<$r>;
337 9 chomp;
338 9 my $bin=pack "H*", $_;
339  
340 9 my ($cmd, $id)=unpack "l> l>", $bin;
341 9 $bin=substr $bin, 8; #two lots of long
342  
343 # Remove from the outstanding table
344 9 my $entry=shift $worker->[WORKER_QUEUE]->@*;
345 9 $in_flight--;
346 #my $entry=delete $reqs{$id};
347
348 # Mark the returning worker as not busy
349 #
350 9 $worker->[WORKER_BUSY]=0;
351  
352 9 ▶ if($cmd==CMD_GAI){
Branch: 5 branches
BranchTrueFalseTotal
if ($cmd == '0') { } 2 7 9
elsif ($cmd == '1') { } 2 5 7
elsif ($cmd == '2') { } 1 4 5
elsif ($cmd == '3') { } 0 4 4
elsif ($cmd == '4') { } 4 0 4
353 2 DEBUG and say STDERR "<< GAI return from worker $entry->[REQ_WORKER]";
354 2 my @res=unpack $gai_pack, $bin;
355 2 ▶ if($res[0] and $entry->[REQ_ERR]){
Branch: 2 branches
BranchTrueFalseTotal
if ($res[0] and $$entry['4']) { } 0 2 2
elsif (not $res[0] and $$entry['3']) { } 2 0 2
Condition33%1/3
$res[0] and $$entry[4]
!ll&&!rl&&r
200
MC/DC0%0/2
$res[0] and $$entry[4]
$res[0] $$entry[4]
Truth table33%1/3
A and B
A= $res[0]B= $$entry[4]
ABresult
0X0
100
111
356 0 $entry->[REQ_ERR]($res[0]);
357 }
358 ▶ elsif(!$res[0] and $entry->[REQ_CB]){
Condition33%1/3
not $res[0] and $$entry[3]
!ll&&!rl&&r
002
MC/DC0%0/2
not $res[0] and $$entry[3]
not $res[0] $$entry[3]
Truth table33%1/3
not(A) and B
A= not $res[0]B= $$entry[3]
ABresult
0X0
100
111
359 2 my @list;
360 2 ▶ while(@res>=6){
Branch: 1 branch
BranchTrueFalseTotal
if (@res >= 6) 54 2 56
361 54 my @r=splice @res,0, 6;
362 54 ▶ $r[5]||=undef; #Set cannon name to undef if empty string
Condition50%1/2
$r[5] ||= undef
l!l
054
MC/DC0%0/1
$r[5] ||= undef
$r[5]
Truth table50%1/2
A
A= $r[5]
Aresult
11
00
363 54 ▶ if(ref($entry->[REQ_DATA]) eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$entry['2'] eq 'ARRAY') { } 0 54 54
364 0 push @list, \@r;#[$error, $family, $type, $protocol, $addr, $canonname];
365 }
366 else {
367 54 push @list, {
368 error=>$r[0],
369 family=>$r[1],
370 socktype=>$r[2],
371 protocol=>$r[3],
372 addr=>$r[4],
373 cannonname=>$r[5]
374 };
375  
376 }
377 }
378 #}
379 2 $entry->[REQ_CB](@list);
380 }
381 else {
382 # throw away results
383 }
384  
385  
386 }
387  
388 elsif($cmd==CMD_GNI){
389 2 DEBUG and say STDERR "<< GNI return from worker $entry->[REQ_WORKER]";
390 2 my ($error, $host, $port)=unpack "l> l>/A* l>/A*", $bin;
391 2 DEBUG and say STDERR "error $error";
392 2 DEBUG and say STDERR "host $host";
393 2 DEBUG and say STDERR "service Service $port";
394 2 ▶ if($error and $entry->[REQ_ERR]){
Branch: 2 branches
BranchTrueFalseTotal
if ($error and $$entry['4']) { } 0 2 2
elsif (not $error and $$entry['3']) { } 2 0 2
Condition33%1/3
$error and $$entry[4]
!ll&&!rl&&r
200
MC/DC0%0/2
$error and $$entry[4]
$error $$entry[4]
Truth table33%1/3
A and B
A= $errorB= $$entry[4]
ABresult
0X0
100
111
395 0 $entry->[REQ_ERR]($error);
396 }
397 ▶ elsif(!$error and $entry->[REQ_CB]){
Condition33%1/3
not $error and $$entry[3]
!ll&&!rl&&r
002
MC/DC0%0/2
not $error and $$entry[3]
not $error $$entry[3]
Truth table33%1/3
not(A) and B
A= not $errorB= $$entry[3]
ABresult
0X0
100
111
398 2 DEBUG and say $entry->[REQ_CB];
399 2 $entry->[REQ_CB]($host, $port);
400 }
401 else {
402 # Should not get here
403 }
404 }
405 elsif($cmd==CMD_SPAWN){
406 # response from template fork. Add the worker to the pool
407 #
408 1 my $pid=unpack "l>", $bin;
409 1 my $index=$entry->[2]; #
410 1 DEBUG and say STDERR "SPAWN RETURN: pid $pid index $index";
411 #unshift @pool_free, $index;
412 1 my $worker=$pairs[$index];
413 1 $worker->[WORKER_ID]=$pid;
414 # turn on the worker by clearing the busy flag
415 1 $worker->[WORKER_BUSY]=0;
416 1 $fd_worker_map{fileno $worker->[WORKER_READ]}=$worker;
417  
418 1 DEBUG and say STDERR "<< SPAWN RETURN FROM TEMPLATE $entry->[REQ_WORKER]: new worker $pid";
419 }
420 elsif($cmd == CMD_KILL){
421 0 my $id=$entry->[REQ_WORKER];
422 0 DEBUG and say STDERR "<< KILL RETURN FROM WORKER: $id : $worker->[WORKER_ID]";
423 0 $worker->[WORKER_ID]=0;
424 #@pool_free=grep $pairs[$_]->[WORKER_ID] != $id, @pool_free;
425 }
426 elsif($cmd ==CMD_REAP){
427 # Grandchild process checking via template process
428 4 my @pids=unpack "l>/l>*", $bin;
429  
430 4 DEBUG and say STDERR "<< REAP RETURN FROM TEMPLATE $entry->[REQ_WORKER]";
431 4 for(@pids){
432 20 ▶ next unless $_ >0;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ > 0 20 0 20
433  
434 0 my $index=-1; # ignore template
435 #Locate the pid in the worker slots
436 0 for my $windex (1..$#pairs){
437 0 ▶ if($pairs[$windex][WORKER_ID]==$_){
Branch: 1 branch
BranchTrueFalseTotal
if ($pairs[$windex][0] == $_) 0 0 0
438 0 $index=$windex;
439 0 last;
440 }
441 }
442  
443 0 ▶ if($index>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($index > 0) { } 0 0 0
444 0 $pairs[$index][WORKER_ID]=0;
445 0 $pairs[$index][WORKER_BUSY]=0;
446 #only restart if the worker has items in its queue
447 0 ▶ if($pairs[$index][WORKER_QUEUE]->@*){
Branch: 1 branch
BranchTrueFalseTotal
if (@{$pairs[$index][5]}) 0 0 0
448 0 unshift $pairs[0][WORKER_QUEUE]->@*, [CMD_SPAWN, $i++, $index];
449 0 $in_flight++;
450 }
451 }
452 else {
453 # ignore
454 }
455 }
456 }
457  
458 9 ▶ pool_next $worker if $Shared;
Branch: 1 branch
BranchTrueFalseTotal
if $Socket::More::Resolver::Shared 0 9 9
459 }
460  
461 23 ▶sub _results_available {
Subroutine _results_available: called CC=8 SCAR=20.8
462 23 ▶ my $timeout=shift//0;
Condition50%1/2
shift() // 0
l!l
023
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
463 23 DEBUG and say STDERR "CHECKING IF RESULTS AVAILABLE";
464 # Check if any workers are ready to talk
465 23 my $bits="";
466 23 for(@pairs){
467 138 ▶ vec($bits, fileno($_->[WORKER_READ]),1)=1 if $_->[WORKER_ID];
Branch: 1 branch
BranchTrueFalseTotal
if $$_[0] 35 103 138
468 }
469  
470 23 my $count=select $bits, undef, undef, $timeout;
471  
472 23 ▶ if($count>0){
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 5 18 23
473 5 for(@pairs){
474 30 ▶ if($_->[WORKER_ID] and vec($bits, fileno($_->[WORKER_READ]), 1)){
Branch: 1 branch
BranchTrueFalseTotal
if ($$_[0] and vec $bits, fileno $$_[1], 1) 9 21 30
Condition100%3/3
$$_[0] and vec $bits, fileno $$_[1], 1
!ll&&!rl&&r
2019
MC/DC100%2/2
$$_[0] and vec $bits, fileno $$_[1], 1
$$_[0] vec $bits, fileno $$_[1], 1
Truth table100%3/3
A and B
A= $$_[0]B= vec $bits, fileno $$_[1], 1
ABresult
0X0
100
111
475 9 process_results $_;
476 }
477 }
478 }
479 23 $count;
480 }
481  
482 12 ▶sub getaddrinfo{
Subroutine getaddrinfo: called CC=5 SCAR=16.3
Pod documented
483 12 ▶ if( @_ ){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 10 12
484 # If arguments present, then add to the request queue
485  
486 2 my ($host, $port, $hints, $on_result, $on_error)=@_;
487  
488 # Format the resuest into the same as the return structures.
489 2 my $ref=[];
490 2 ▶ if(ref($hints) eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $hints eq 'ARRAY') { } 0 2 2
491 0 push @$hints, $host, $port;
492 }
493 else {
494 #$hints->{host}=$host;
495 2 $hints->{address}=$host;
496 2 $hints->{port}=$port;
497 }
498  
499  
500 # add the request to the queue and to outstanding table
501 2 my $worker=_get_worker;
502 2 my $req=[CMD_GAI, $i++, $hints, $on_result, $on_error, $worker->[WORKER_ID]];
503 2 push $worker->[WORKER_QUEUE]->@*, $req;
504 2 $in_flight++;
505 #
506 2 ▶ monitor_workers unless $Shared;
Branch: 1 branch
BranchTrueFalseTotal
unless $Socket::More::Resolver::Shared 2 0 2
507 2 ▶ shrink_pool if $enable_shrink;
Branch: 1 branch
BranchTrueFalseTotal
if $enable_shrink 0 2 2
508 }
509  
510 12 pool_next;
511 #return true if outstanding requests
512 12 DEBUG and say STDERR "IN FLIGHT: $in_flight";
513 12 $in_flight;
514 }
515  
516 2 ▶sub getnameinfo{
Subroutine getnameinfo: called CC=4 SCAR=14.1
Pod documented
517 2 ▶ if(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
518 2 my ($addr, $flags, $on_result, $on_error)=@_;
519 2 my $worker=_get_worker;
520 2 my $req=[CMD_GNI, $i++, [$addr, $flags], $on_result, $on_error, $worker->[WORKER_ID]];
521 2 push $worker->[WORKER_QUEUE]->@*, $req;
522 2 $in_flight++;
523  
524 2 ▶ monitor_workers unless $Shared;
Branch: 1 branch
BranchTrueFalseTotal
unless $Socket::More::Resolver::Shared 2 0 2
525 2 ▶ shrink_pool if $enable_shrink;
Branch: 1 branch
BranchTrueFalseTotal
if $enable_shrink 0 2 2
526  
527 }
528 2 pool_next;
529 2 DEBUG and say STDERR "IN FLIGHT: $in_flight";
530 2 $in_flight;
531 }
532  
533 0 ▶sub close_pool {
Subroutine close_pool: not called CC=3 SCAR=24.8
Pod undocumented
534  
535 0 my @indexes=1..$#pairs;
536 0 push @indexes, 0;
537  
538 #generate messages to close
539 0 for(@indexes){
540 0 my $worker=$pairs[$_];
541 0 ▶ next unless $worker->[WORKER_ID];
Branch: 1 branch
BranchTrueFalseTotal
unless $$worker[0] 0 0 0
542  
543 0 my $req=[CMD_KILL, $i++, [], undef, undef, $_];
544 0 push $worker->[WORKER_QUEUE]->@*, $req;
545 0 $in_flight++;
546 0 pool_next;
547 }
548 }
549  
550 # Send kill signal to all workers (not template)
551 # This forces respawning.
552 0 ▶sub kill_pool {
Subroutine kill_pool: not called CC=3 SCAR=24.8
Pod undocumented
553 0 my @indexes=1..$#pairs;
554 0 for(@indexes){
555 0 my $worker=$pairs[$_];
556 0 ▶ next unless $worker->[WORKER_ID];
Branch: 1 branch
BranchTrueFalseTotal
unless $$worker[0] 0 0 0
557  
558 0 kill 'KILL', $worker->[WORKER_ID];
559 0 $worker->[WORKER_ID]=0;
560 0 $worker->[WORKER_BUSY]=0;
561 }
562  
563 }
564  
565 # return the parent side reading filehandles. This is what is needed for event loops
566 0 ▶sub to_watch {
Subroutine to_watch: not called CC=1 SCAR=6.9
Pod undocumented
567 0 map $_->[WORKER_READ], @pairs;
568 }
569  
570 4 ▶sub monitor_workers {
Subroutine monitor_workers: called CC=4 SCAR=15.2
Pod undocumented
571 2 ▶ use POSIX qw<:sys_wait_h :errno_h>;
Subroutine BEGIN: called CC=1 SCAR=0.0
572  
573 # check we have a template
574 4 my $tpid=$pairs[0][WORKER_ID];
575 4 my $res=waitpid $tpid, WNOHANG;
576 4 ▶ if($res==$tpid){
Branch: 2 branches
BranchTrueFalseTotal
if ($res == $tpid) { } 0 4 4
elsif ($res == -1 and $! == 10) { } 0 4 4
577 # This is the non event case
578 0 $pairs[0][WORKER_ID]=0;
579 #close_pool;
580 0 kill_pool;
581 }
582 ▶ elsif($res == -1 and $! == ECHILD){
Condition33%1/3
$res == -1 and $! == 10
!ll&&!rl&&r
400
MC/DC0%0/2
$res == -1 and $! == 10
$res == -1 $! == 10
Truth table33%1/3
A and B
A= $res == -1B= $! == 10
ABresult
0X0
100
111
583 # Event loops take over the child listening.... so work around
584 #
585 0 $pairs[0][WORKER_ID]=0;
586 #close_pool;
587 0 kill_pool;
588 }
589 else {
590 # Template still active, use it as proxy
591 4 my @pids= map {$_->[WORKER_ID]} @pairs;
592 4 shift @pids; #remove template from the list
593  
594 4 push $pairs[0][WORKER_QUEUE]->@*, [CMD_REAP, $i++, [@pids], \&_monitor_callback, undef];
595 4 $in_flight++;
596 }
597  
598 4 pool_next;
599 4 $in_flight;
600 }
601  
602 0 ▶sub _monitor_callback {
Subroutine _monitor_callback: not called CC=1 SCAR=0.0
603
604 }
605  
606  
607 3 ▶sub spawn_template {
Subroutine spawn_template: called CC=3 SCAR=11.0
Pod undocumented
608 # This should only be called when modules is first loaded, or when an
609 # external force has killed the template process
610 3 my $worker=$pairs[0];
611 3 ▶ return $worker if $worker->[WORKER_ID];
Branch: 1 branch
BranchTrueFalseTotal
if $$worker[0] 1 2 3
612  
613 2 my $pid=fork;
614 2 ▶ if($pid){
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) { } 1 1 2
615 # parent
616 #
617 1 $worker->[WORKER_ID]=$pid;
618 1 $fd_worker_map{fileno $worker->[WORKER_READ]}=$worker;
619 #push @pool_free, 0;
620 1 $worker;
621  
622 }
623 else {
624 # child
625 # exec an tell the process which fileno we want to communicate on
626 1 close $worker->[WORKER_READ];
627 1 close $worker->[WORKER_WRITE];
628 1 my @ins=map {fileno $_->[WORKER_CREAD]} @pairs; # Child read end
629 1 my @outs=map {fileno $_->[WORKER_CWRITE]} @pairs; # Child write end
630 1 DEBUG and say STDERR "Create worker: exec with ins: @ins";
631 1 DEBUG and say STDERR "Create worker: exec with outs: @outs";
632 1 my $file=__FILE__;
633 1 $file=~s|\.pm|/Worker.pm|;
634 1 local $"=",";
635 1 exec $^X, $file, "--in", "@ins", "--out", "@outs";
636 }
637 }
638  
639 0 ▶sub shrink_pool {
Subroutine shrink_pool: not called CC=5 SCAR=34.0
Pod undocumented
640 # work backwards and send a kill message to any non busy workers
641 0 my $template_worker=spawn_template(); #ensure template exists
642 0 for(reverse(@pairs)){
643 0 ▶ next if $_== $template_worker;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $template_worker 0 0 0
644 0 ▶ next if $_->[WORKER_QUEUE]->@*;
Branch: 1 branch
BranchTrueFalseTotal
if @{$$_[5]} 0 0 0
645 0 ▶ next unless $_->[WORKER_ID];
Branch: 1 branch
BranchTrueFalseTotal
unless $$_[0] 0 0 0
646  
647 # send a kill message to any un needed workers
648 0 my $req=[CMD_KILL, $i++, [], undef, undef, $_];
649 0 push $_->[WORKER_QUEUE]->@*, $req;
650 0 $in_flight++;
651 }
652 }
653  
654 0 ▶sub cleanup {
Subroutine cleanup: not called CC=2 SCAR=17.9
Pod undocumented
655 #say STDERR "END HERE";
656 #kill_pool;
657 ################################
658 # for(@pairs){ #
659 # close $_->[WORKER_CREAD]; #
660 # close $_->[WORKER_CWRITE]; #
661 # } #
662 ################################
663 # The template
664  
665 0 my $tpid=$pairs[0][WORKER_ID];
666 #say STDERR "Template pid: ", $tpid;
667 #say STDERR
668 0 kill 'KILL', $tpid;
669 0 my $res=waitpid $tpid, 0;#, WNOHANG;
670 0 ▶ if($res==$tpid){
Branch: 1 branch
BranchTrueFalseTotal
if ($res == $tpid) { } 0 0 0
671 #say STDERR "TEMPLATE KILLED";
672 # This is the non event case
673 #$pairs[0][WORKER_ID]=0;
674 #close_pool;
675 0 kill_pool;
676 }
677 else {
678 #say STDERR "RES: $res";
679 }
680  
681 }
682  
683 2 1;
↑ Summary Resolver/IO/Async.pm »