HTTP/MultiGet.pm

filter: Statementstmt 63.1% 204 / 323 Branchbran 38.7% 31 / 80 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 79.6% 43 / 54 Podpod 75.0% 15 / 20 totaltotal 59.8% 297 / 496 CC 54 SCAR 22.9
CC
54
Cov
58%
__ANON__
33.2
run_requests
30.0
has_request
20.0
CRAP 9.9
↑ Summary HTTP/MultiGet/Role.pm »
1 package HTTP::MultiGet;
2  
3 =head1 NAME
4  
5 HTTP::MultiGet - Run many http requests at once!
6  
7 =head1 SYNOPSIS
8  
9 use Modern::Perl;
10 use HTTP::MultiGet;
11 use HTTP::Request;
12  
13 my $getter=new HTTP::MultiGet;
14  
15 my @requests=(map { HTTP::Request->new(GET=>"http://localhost$_") } (1 .. 1000));
16 my @responses=$getter->run_requests(@requests);
17  
18 my $id=0;
19 foreach my $response (@responses) {
20 my $request=$requests[$id++];
21 print "Results for: ".$request->uri."\n";
22 if($response->is_success) {
23 print $response->decoded_content;
24 } else {
25 print $response->status_line,"\n";
26 }
27 }
28  
29  
30 =head2 Handling Multiple Large Downloads
31  
32 use Modern::Perl;
33 use HTTP::MultiGet;
34 use HTTP::Request;
35  
36 my $req=HTTP::Request->new(GET=>'http://some.realy.big/file/to/download.gz');
37 my $req_b=HTTP::Request->new(GET=>'http://some.realy.big/file/to/download2.gz');
38  
39 # create a callback
40 my $code=sub {
41 my ($getter,$request,$headers,$chunk)=@_;
42 # 0: Current HTTP::MultiGet instance
43 # 1: HTTP::Request object
44 # 2: HTTP::Headers object
45 # 3: Chunk of data being downloaded
46 if($headers->header('Status')==200) {
47 # do something
48 } else {
49 # do something with $body
50 }
51 };
52 my $getter=new HTTP::MultiGet;
53 my ($result,$result_b)=$getter->run_requests([$req,on_body=>$code],[$req_b,on_body=>$code]);
54  
55  
56 The arguments: on_body=>$code are called called on each chunk downloaded. $result is created when the download is completed, but $result->decoded_content is going to be empty
57  
58 =head1 DESCRIPTION
59  
60 Created a wrapper for: L<AnyEvent::HTTP>, but provides a more LWP like feel.
61  
62 =cut
63  
64 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 6 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 6 ▶use Data::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 6 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
68 6 Log::Log4perl->wrapper_register(__PACKAGE__);
69 6 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 6 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 6 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 6 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 6 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 6 ▶use AnyEvent::HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 6 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 6 ▶use AnyEvent::HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 6 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
80 6 ▶use Ref::Util qw(is_plain_arrayref is_plain_hashref);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 6 require AnyEvent::HTTP;
82 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
84 6 with 'Log::LogMethods';
85 6 with 'Data::Result::Moo';
86 }
87 6 our $VERSION='1.023';
88  
89 6 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
90 6 my ($self)=@_;
91  
92 6 $self->{stack}=new Data::Queue;
93 6 $self->{que_count}=0;
94 6 $self->{que_id}=0;
95 }
96  
97 =head1 Moo Stuff
98  
99 This is a Moo class the object constructor takes the following arguments, along with the following roles
100  
101 Role List:
102  
103 Log::LogMethods
104 Data:::Result::Moo
105  
106 Arguemnts and object accessors:
107  
108 logger: DOES(Log::Log4perl::Logger)
109 request_opts: See AnyEvent::HTTP params for details
110 timeout: Global timeout for everything ( default 300 )
111 max_que_count: How many requests to run at once ( default 20 )
112 max_retry: How many times to retry if we get a connection/negotiation error
113  
114 For internal use only:
115  
116 in_control_loop: true when in the control loop
117 stack: Data::Queue object
118 que_count: Total Number of elements active in the que
119 retry: Anonymous hash used to map ids to retry counts
120  
121 =head2 UNIT TESTING
122  
123 For Unit testing
124  
125 on_create_request_cb: Anonymous code ref to be called
126 when a new request object has been created
127 sub { my ($id,$request)=@_ }
128  
129 Arguments for the call back
130  
131 id: number for the object
132 req: a new instance of $self->SENDER_CLASS
133  
134 Interal blocking control variables
135  
136 loop_control: AnyEvent->condvar object
137 false_id: internal false id tracker
138 fake_jobs: Internal object for handling fake results
139  
140 =cut
141  
142 6 has false_id=>(
143 is=>'rw',
144 isa=>Int,
145 default=>-1,
146 lazy=>1,
147 );
148  
149 has fake_jobs=>(
150 isa=>HashRef,
151 is=>'rw',
152 6 ▶ default=>sub { {}}
Subroutine __ANON__: called CC=1 SCAR=0.0
153 6 );
154  
155 6 has loop_control=>(
156 is=>'rw',
157 required=>0.
158 );
159  
160 6 has loop_id=>(
161 is=>'rw',
162 isa=>Num,
163 default=>0,
164 );
165  
166 6 has max_retry=>(
167 is=>'rw',
168 isa=>Num,
169 default=>3,
170 );
171  
172 has retry=>(
173 is=>'rw',
174 2 ▶ default=>sub { { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
175 6 lazy=>1,
176 );
177  
178 6 has no_more_after =>(
179 is=>'rw',
180 isa=>Num,
181 default=>0,
182  
183 );
184  
185 has on_create_request_cb=>(
186 is=>'rw',
187 isa=>CodeRef,
188 0 ▶ default=>sub { sub { } },
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 6 lazy=>1,
190 );
191  
192 6 has in_control_loop=>(
193 is=>'rw',
194 isa=>Bool,
195 default=>0,
196 );
197  
198 has running=>(
199 is=>'ro',
200 2 ▶ default=>sub { {}},
Subroutine __ANON__: called CC=1 SCAR=0.0
201 6 lazy=>1,
202 );
203  
204 has request_opts=>(
205 is=>'rw',
206 requires=>1,
207 5 ▶ default=>sub { {cookie_jar=>{},persistent=>0} },
Subroutine __ANON__: called CC=1 SCAR=0.0
208 6 );
209  
210 6 has stack=>( is=>'ro');
211  
212 has results=>(
213 is=>'ro',
214 lazy=>1,
215 2 ▶ default=>sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
216 6 );
217  
218 6 has que_count=>(
219 is=>'rw',
220 isa=>Num,
221 );
222  
223 has timeout =>(
224 is=>'rw',
225 6 ▶ isa=>sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
226 6 my $timeout=$_[0];
227 6 ▶ croak 'timeout is a required option' unless looks_like_number($timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($timeout) 0 6 6
228 6 ▶ croak 'timeout must be greater than 0' if $timeout <=0;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout <= 0 0 6 6
229 6 $timeout,
230 },
231  
232 6 default=>300,
233 );
234  
235  
236 6 has max_que_count=> (
237 is=>'rw',
238 isa=>Num,
239 default=>20,
240 );
241  
242  
243 40 ▶around max_que_count=>sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
244 40 my ($org,$self,$count)=@_;
245 40 ▶ if(looks_like_number($count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($count)) { } 0 40 40
246 0 ▶ $AnyEvent::HTTP::MAX_PER_HOST=$count if $AnyEvent::HTTP::MAX_PER_HOST < $count;
Branch: 1 branch
BranchTrueFalseTotal
if $AnyEvent::HTTP::MAX_PER_HOST < $count 0 0 0
247 0 return $org->($self,$count);
248 } else {
249 40 shift;
250 40 return $org->(@_);
251 }
252 6 };
253  
254 =head1 Class constants
255  
256 =over 4
257  
258 =item * my $class=$self->RESPONSE_CLASS
259  
260 Returns the http response class, typically AnyEvent::HTTP::Response.
261  
262 =cut
263  
264 6 ▶use constant RESPONSE_CLASS=>'AnyEvent::HTTP::Response';
Subroutine BEGIN: called CC=1 SCAR=0.0
265  
266 =item * my $class=$self->HEADER_CLASS
267  
268 Returns the header class, typically HTTP::Headers.
269  
270 =back
271  
272 =cut
273  
274 6 ▶use constant HEADER_CLASS=>'HTTP::Headers';
Subroutine BEGIN: called CC=1 SCAR=0.0
275  
276  
277 =head1 OO Methods
278  
279 =over 4
280  
281 =item * my $id=$self->next_que_id
282  
283 Returns the next id.
284  
285 =cut
286  
287 2 ▶sub next_que_id {
Subroutine next_que_id: called CC=1 SCAR=0.0
Pod documented
288 2 return $_[0]->{que_id} +=1;
289 }
290  
291 0 ▶sub running_count {
Subroutine running_count: not called CC=1 SCAR=6.9
Pod undocumented
292 0 my ($self)=@_;
293 0 return scalar(keys %{$self->running});
294 }
295  
296 =item * my @ids=$self->add(@requests)
297  
298 Adds @requests to the stack, @ids relates as id=>request
299  
300 =item * my @ids=$self->add([$request,key=>value]);
301  
302 Wrapping [$request] allows passing additional key value to L<AnyEvent::HTTP::Request>, with one exception, on_body=>$code is wrapped an additional callback.
303  
304 =cut
305  
306 2 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
307 2 my ($self,@list)=@_;
308 2 my @ids;
309  
310 2 foreach my $req (@list) {
311 2 my $id=$self->next_que_id;
312 2 push @ids,$id;
313 2 $self->add_by_id($id=>$req);
314 }
315 2 return @ids;
316 }
317  
318 =item * my $id=$self->add_by_id($id=>$request);
319  
320 Adds the request with an explicit id.
321  
322 =cut
323  
324 2 ▶sub add_by_id {
Subroutine add_by_id: called CC=2 SCAR=7.0
Pod documented
325 2 my ($self,$id,$request)=@_;
326 2 my $req=$self->create_request($request,$id);
327 2 $self->stack->add_by_id($id,$req);
328 2 ▶ $self->retry->{$id}=$self->max_retry unless exists $self->retry->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->retry->{$id} 2 0 2
329 2 $self->on_create_request_cb->($id,$req);
330 2 return $id;
331 }
332  
333 =item * $self->run_fake_jobs
334  
335 Runs all current fake jobs
336  
337 =cut
338  
339 26 ▶sub run_fake_jobs {
Subroutine run_fake_jobs: called CC=3 SCAR=11.0
Pod documented
340 26 my ($self)=@_;
341
342 26 ▶ while($self->has_fake_jobs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_fake_jobs) 15 26 41
343 15 my $fj=$self->fake_jobs;
344 15 $self->fake_jobs({});
345 15 ▶ while(my (undef,$job)=each (%{$fj})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(undef, $job) = each %{$fj;}) 24 15 39
346 24 $job->();
347 }
348 }
349 }
350  
351 =item * $self->run_next
352  
353 Internal functoin, used to run the next request from the stack.
354  
355 =cut
356  
357 20 ▶sub run_next {
Subroutine run_next: called CC=4 SCAR=18.5
Pod documented
358 20 my ($self)=@_;
359  
360 20 $self->run_fake_jobs;
361 20 ▶ return if $self->{que_count} >= $self->max_que_count;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"que_count"} >= $self->max_que_count 0 20 20
362  
363 20 ▶ while($self->{que_count} < $self->max_que_count and $self->stack->has_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"que_count"} < $self->max_que_count and $self->stack->has_next) 0 20 20
Condition33%1/3
$self->{"que_count"} < $self->max_que_count and $self->stack->has_next
!ll&&!rl&&r
0200
MC/DC0%0/2
$self->{"que_count"} < $self->max_que_count and $self->stack->has_next
$self->{"que_count"} < $self->max_que_count $self->stack->has_next
Truth table33%1/3
A and B
A= $self->{"que_count"} < $self->max_que_countB= $self->stack->has_next
ABresult
0X0
100
111
364 0 my ($id,$next)=$self->stack->get_next;
365 0 $self->running->{$id}=$next;
366 0 $next->send;
367 0 ++$self->{que_count};
368 0 $self->log_debug("Total of: $self->{que_count} running");
369 }
370  
371 20 return 1;
372 }
373  
374 =item * my $id=$self->add_result($cb)
375  
376 Internal function, added for L<HTTP::MultiGet::Role>
377  
378 =cut
379  
380 24 ▶sub add_result {
Subroutine add_result: called CC=1 SCAR=0.0
Pod documented
381 24 my ($self,$cb)=@_;
382 24 my $current=$self->false_id;
383 24 $current -=1;
384 24 $self->false_id($current);
385 24 $self->fake_jobs->{$current}=$cb;
386 24 return $current;
387 }
388  
389 =item * if($self->has_fake_jobs) { ... }
390  
391 Checks if any fake jobs exist
392  
393 =cut
394  
395 41 ▶sub has_fake_jobs {
Subroutine has_fake_jobs: called CC=1 SCAR=0.0
Pod undocumented
396 41 return 0 < keys %{$_[0]->fake_jobs}
397 }
398  
399 =item * my $result=$self->has_request($id)
400  
401 Returns a Charter::Result Object.
402  
403 When true it contains a string showing where that id is in the list.
404  
405 Values are:
406  
407 complete: Has finished running
408 running: The request has been sent, waiting on the response
409 in_que: The request has not been run yet
410  
411 =cut
412  
413 0 ▶sub has_request {
Subroutine has_request: not called CC=4 SCAR=30.0
Pod documented
414 0 my ($self,$id)=@_;
415  
416 0 my $result=$self->new_false('request does not exist!');
417  
418 0 ▶ if(exists $self->results->{$id}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->results->{$id}) { } 0 0 0
elsif (exists $self->running->{$id}) { } 0 0 0
elsif ($self->stack->has_id($id)) { } 0 0 0
419 0 $result=$self->new_true('complete');
420  
421 } elsif(exists $self->running->{$id}) {
422 0 $result=$self->new_true('running');
423  
424 } elsif( $self->stack->has_id($id) ) {
425  
426 0 $result=$self->new_true('in_que');
427 }
428
429 0 return $result;
430 }
431  
432 =item * if($self->has_running_or_pending) { ... }
433  
434 Returns a Charter::Result object
435  
436 true: it contains how many objects are not yet complete.
437 false: no incomplete jobs exist
438  
439 =cut
440  
441 0 ▶sub has_running_or_pending {
Subroutine has_running_or_pending: not called CC=2 SCAR=17.9
Pod undocumented
442 0 my ($self)=@_;
443 0 my $total=$self->stack->total;
444 0 $total +=$self->running_count;
445 0 ▶ return $self->new_true($total) if $total >0;
Branch: 1 branch
BranchTrueFalseTotal
if $total > 0 0 0 0
446 0 return $self->new_false("there are no objects running");
447 }
448  
449 =item * if($self->has_any) { ... }
450  
451 Returns a Charter::Result object
452  
453 true: conains how many total jobs of any state there are
454 false: there are no jobs in any state
455  
456 =cut
457  
458 0 ▶sub has_any {
Subroutine has_any: not called CC=2 SCAR=17.9
Pod undocumented
459 0 my ($self)=@_;
460 0 my $total=$self->stack->total;
461 0 $total +=keys %{$self->running};
462 0 $total +=keys %{$self->results};
463  
464 0 ▶ return $self->new_true($total) if $total >0;
Branch: 1 branch
BranchTrueFalseTotal
if $total > 0 0 0 0
465 0 return $self->new_false("there are no objects running");
466 }
467  
468 =item * my $result=$self->block_for_ids(@ids)
469  
470 Returns a Charter::Result object
471  
472 when true: some results were found
473  
474 while(my ($id,$result)=each %{$result->get_data}) {
475 if($result) {
476 my $response=$result->get_data;
477 print "Got response: $id, code was: ",$response->code,"\n";
478 } else {
479 print "Failed to get: $id error was: $result\n";
480 }
481 }
482  
483 =cut
484  
485 6 ▶sub block_for_ids {
Subroutine block_for_ids: called CC=8 SCAR=22.4
Pod documented
486 6 my ($self,@ids)=@_;
487  
488 6 $self->run_fake_jobs;
489 6 my $results={};
490  
491 6 my @check;
492  
493 6 my $ok=0;
494 6 foreach my $id (@ids) {
495 6 ▶ if(exists $self->results->{$id}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $self->results->{$id}) { } 2 4 6
elsif (exists $self->running->{$id}) { } 0 4 4
elsif ($self->stack->has_id($id)) { } 0 4 4
496 # the result is done
497 2 $results->{$id}=$self->results->{$id};
498 2 delete $self->results->{$id};
499 2 ++$ok;
500  
501 } elsif(exists $self->running->{$id}) {
502  
503 0 push @check,$id;
504 0 $results->{$id}=$self->new_false('Request Timed out');
505  
506 } elsif( $self->stack->has_id($id) ) {
507  
508 0 push @check,$id;
509 0 $results->{$id}=$self->new_false('Request Never made it to the que');
510  
511 } else {
512  
513 4 push @check,$id;
514 4 $results->{$id}=$self->new_false('request does not exist!');
515 }
516 }
517  
518 6 $self->block_loop;
519 6 foreach my $id (@check) {
520 4 ▶ next unless exists $self->results->{$id};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->results->{$id} 4 0 4
521  
522 0 ++$ok;
523  
524 0 $results->{$id}=$self->results->{$id};
525 0 delete $self->results->{$id};
526 }
527  
528 6 delete @{$self->retry}{@ids};
529 6 ▶ return $self->new_true($results) if $ok!=0;
Branch: 1 branch
BranchTrueFalseTotal
if $ok != 0 2 4 6
530 4 return $self->new_false("Request(s) timed out");
531 }
532  
533 =item * my $class=$self->SENDER_CLASS
534  
535 $class is the class used to send requests.
536  
537 =cut
538  
539 2 ▶sub SENDER_CLASS { 'AnyEvent::HTTP::Request' }
Subroutine SENDER_CLASS: called CC=1 SCAR=0.0
Pod documented
540  
541 =item * my $req=$self->create_request($req,$id)
542  
543 Internal method. Returns a new instance of $self->SENDER_CLASS for Teh given $request and $id.
544  
545 =cut
546  
547 2 ▶sub create_request {
Subroutine create_request: called CC=6 SCAR=25.1
Pod documented
548 2 my ($self,$obj,$id)=@_;
549
550 2 my $req;
551 my $opt={
552 2 params=>{%{$self->request_opts}},
553 };
554 2 ▶ if(is_plain_arrayref($obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($obj)) { } 0 2 2
555 0 my @args;
556 0 ($req,@args)=@{$obj};
557 0 my $code=$self->que_function($req,$id);
558 0 ▶ while(my ($key,$value)=splice(@args,0,2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@args, 0, 2)) 0 0 0
559 0 ▶ if($key eq 'on_body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'on_body') { } 0 0 0
560 0 ▶ my $code=sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
561 0 my ($body,$headers)=@_;
562 0 my $header=new HTTP::Headers( %{$headers});
563 0 $value->($self,$req,$header,$body);
564 0 };
565 0 $opt->{params}->{$key}=$code;
566 } else {
567 0 $opt->{params}->{$key}=$value;
568 }
569 }
570 0 $opt->{cb}=$code;
571 } else {
572 2 $req=$obj;
573 2 my $code=$self->que_function($req,$id);
574 2 $opt->{cb}=$code;
575 }
576 2 foreach my $key (qw(keepalive persistent)) {
577 4 ▶ $opt->{params}->{$key}=0 unless exists $opt->{params}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opt->{"params"}{$key} 2 2 4
578 }
579 2 my $request=$self->SENDER_CLASS->new(
580 $req,
581 $opt,
582 );
583  
584 2 return $request;
585 }
586  
587 =item * my $code=$self->que_function($req,$id);
588  
589 Internal method. Creates a code refrence for use in the que process.
590  
591 =cut
592  
593 2 ▶sub que_function {
Subroutine que_function: called CC=1 SCAR=1.1
Pod documented
594 2 my ($self,$req,$id)=@_;
595 2 my $loop_id=$self->loop_id;
596  
597 2 my $code;
598 2 ▶ $code=sub {
Subroutine __ANON__: called CC=12 SCAR=35.0
599 2 my $response=$self->RESPONSE_CLASS->new(@_)->to_http_message;
600 2 $self->log_debug("Got Response for id: [$id] Status Line: ".$response->status_line);
601  
602 # work around for unit testing;
603 2 ▶ --$self->{que_count} if $self->{que_count} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"que_count"} > 0 0 2 2
604  
605 2 ▶ if(exists $self->retry->{$id} and $response->code > 594 and $self->retry->{$id}-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->retry->{$id} and $response->code > 594 and $self->retry->{$id}-- > 0) 0 2 2
Condition33%2/6
exists $self->retry->{$id} and $response->code > 594 and $self->retry->{$id}-- > 0
!ll&&!rl&&r
200
exists $self->retry->{$id} and $response->code > 594
!ll&&!rl&&r
020
MC/DC0%0/3
exists $self->retry->{$id} and $response->code > 594 and $self->retry->{$id}-- > 0
exists $self->retry->{$id} $response->code > 594 $self->retry->{$id}-- > 0
Truth table25%1/4
A and B and C
A= exists $self->retry->{$id}B= $response->code > 594C= $self->retry->{$id}-- > 0
ABCresult
0XX0
10X0
1100
1111
606 0 $self->log_info("Request negotiation error: ".$response->code." for id: $id retry count is: ".( 1 + $self->retry->{$id} )." will retry");
607 0 $self->add_by_id($id=>$req);
608 0 $self->run_next;
609 0 return;
610 }
611  
612 2 delete $self->retry->{$id};
613  
614 2 ▶ if(exists $self->running->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->running->{$id}) { } 0 2 2
615 0 delete $self->running->{$id};
616 } else {
617 2 $self->log_debug("$id never ran, but the cb is being used");
618 2 $self->stack->remove($id);
619 }
620
621 2 $self->results->{$id}=$self->new_true($response);
622
623 2 $self->run_next;
624 2 ▶ if($self->in_control_loop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->in_control_loop) 0 2 2
625 0 ▶ if($self->que_count==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->que_count == 0) 0 0 0
626 0 ▶ $self->loop_control->send if $self->loop_control;
Branch: 1 branch
BranchTrueFalseTotal
if $self->loop_control 0 0 0
627 }
628 0 undef $code;
629 0 return;
630 }
631  
632 2 ▶ if($self->{que_count}==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'que_count'} == 0) { } 2 0 2
633 2 $self->log_debug('Que Count has reached 0');
634 2 ▶ if($loop_id!=$self->loop_id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop_id != $self->loop_id) 0 2 2
635 0 $self->log_info("A result outside of it's lifecycle has arived loop_id: $loop_id que_id: $id, but we are in loop_id: ".$self->loop_id);
636 0 undef $code;
637 0 return;
638 }
639 2 ▶ $self->loop_control->send if $self->loop_control;
Branch: 1 branch
BranchTrueFalseTotal
if $self->loop_control 0 2 2
640 } else {
641 0 $self->log_debug("Que Count is at $self->{que_count}");
642 }
643 2 undef $code;
644 2 };
645 2 return $code;
646 }
647  
648 =item * $self->block_loop
649  
650 Internal Function. Does a single timed pass against the current set of data, stops when the requests complete.
651  
652 =cut
653  
654 6 ▶sub block_loop : BENCHMARK_DEBUG {
Subroutine block_loop: called CC=4 SCAR=24.4
655 6 my ($self)=@_;
656 6 my $timeout=$self->timeout;
657 6 my $stack=$self->stack;
658 6 my $count=$self->que_count;
659  
660 6 ▶ return $self->new_true("No http requests in que") if($stack->total==0 and $count==0);
Branch: 1 branch
BranchTrueFalseTotal
if $stack->total == 0 and $count == 0 6 0 6
Condition33%1/3
$stack->total == 0 and $count == 0
!ll&&!rl&&r
006
MC/DC0%0/2
$stack->total == 0 and $count == 0
$stack->total == 0 $count == 0
Truth table33%1/3
A and B
A= $stack->total == 0B= $count == 0
ABresult
0X0
100
111
661  
662 0 my $result=$self->new_true();
663 0 $self->log_info("There are: $self->{que_count} jobs in the in the que");
664
665 0 my $t;
666 LOOP_CONTROL: {
667 0 $self->in_control_loop(1);
668 # make sure we don't run forever!
669 0 $self->loop_control(AnyEvent->condvar);
670 0 ▶ $t=AnyEvent->timer(after=>$self->timeout,cb=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
671 0 $result=$self->new_false("Timed out before we got a response");
672 0 $self->log_error("Request Que timed out, Que Count is: ".$self->que_count);
673 0 $self->loop_control->send;
674 0 });
675 0 $self->run_next;
676 0 ▶ $self->loop_control->send if $self->{que_count}<=0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"que_count"} <= 0 0 0 0
677  
678 0 $self->loop_control->recv;
679 0 undef $t;
680 }
681 0 $self->loop_control(undef);
682 0 undef $t;
683 0 $self->in_control_loop(0);
684 0 $self->loop_id($self->loop_id + 1);
685 0 return $result;
686 6 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
687  
688 =item * my @responses=$self->run_requests(@requests);
689  
690 Ques runs and blocks for all https requests, and returns the result objects
691  
692 Arguments:
693  
694 @requests: list of HTTP::Request Objects
695  
696 Responses
697  
698 @responses: list of HTTP::Result in order of the requests.
699  
700 =cut
701  
702 0 ▶sub run_requests {
Subroutine run_requests: not called CC=5 SCAR=34.0
Pod documented
703 0 my ($self,@requests)=@_;
704 0 my @ids=$self->add(@requests);
705  
706 0 my $init_result=$self->block_for_ids(@ids);
707  
708 0 my @results;
709 0 my $results=$init_result->get_data;
710 0 ▶ if($init_result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($init_result) { } 0 0 0
711 0 foreach my $id (@ids) {
712 0 my $result=$results->{$id};
713 0 ▶ if($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
714 0 my $res=$result->get_data;
715 0 $res->code;
716 0 push @results,$res;
717 } else {
718 0 $self->log_debug("Failed to get response, error was: $init_result");
719 0 push @results,$self->RESPONSE_CLASS->new('',{Status=>500,Reason=>"Request Timed out"})->to_http_message;
720 }
721 }
722 } else {
723 0 foreach my $id (@ids) {
724 0 $self->log_debug("Failed to get response, error was: $init_result");
725 0 push @results,$self->RESPONSE_CLASS->new('',{Status=>500,Reason=>"Request Timed out"})->to_http_message;
726 }
727 }
728 0 return @results;
729 }
730  
731 =item * $self->clean_results
732  
733 Used to remove any results that are unclaimed ( Use to prevent memory leaks! ).
734  
735 =cut
736  
737 0 ▶sub clean_results {
Subroutine clean_results: not called CC=1 SCAR=6.9
Pod documented
738 0 %{$_[0]->results}=();
739 }
740  
741 =item * my @responses=$self->block_for_results_by_id(@ids)
742  
743 Blocks on the @ids lsit for list of HTTP::Response objects
744  
745 =cut
746  
747 6 ▶sub block_for_results_by_id {
Subroutine block_for_results_by_id: called CC=5 SCAR=16.2
Pod documented
748 6 my ($self,@ids)=@_;
749  
750 6 my $result=$self->block_for_ids(@ids);
751  
752 6 my @results;
753 6 ▶ if($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 2 4 6
754 2 my $hash=$result->get_data;
755 2 foreach my $id (@ids) {
756 2 my $result=$hash->{$id};
757 2 ▶ if($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 2 0 2
758 2 push @results,$result->get_data;
759 } else {
760 0 push @results,$self->RESPONSE_CLASS->new('',{Status=>500,Reason=>"Request Timed out"})->to_http_message;
761 }
762 }
763  
764 } else {
765 4 foreach my $id (@ids) {
766 4 push @results,$self->RESPONSE_CLASS->new('',{Status=>500,Reason=>"Request Timed out"})->to_http_message;
767 }
768 }
769  
770 6 return @results;
771 }
772  
773 =item * my @results=$self->get_results(@ids);
774  
775 Does not block, just returns a list of HTTP::Response objects based on @ids
776  
777 =cut
778  
779 0 ▶sub get_results {
Subroutine get_results: not called CC=4 SCAR=30.0
Pod documented
780 0 my ($self,@ids)=@_;
781  
782 0 my $result=$self->new_false('request does not exist!');
783  
784 0 my @results;
785 0 my $ok=0;
786 0 foreach my $id (@ids) {
787 0 ▶ if(exists $self->results->{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->results->{$id}) { } 0 0 0
788 0 ++$ok;
789 0 my $result=$self->results->{$id};
790 0 delete $self->results->{$id};
791 0 ▶ if($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 0 0 0
792 0 push @results,$result->get_data;
793 } else {
794 0 push @results,$self->RESPONSE_CLASS->new('',{Status=>500,Reason=>"Request Timed out"})->to_http_message;
795 }
796 } else {
797 0 push @results,$self->RESPONSE_CLASS->new('',{Status=>500,Reason=>"Request Timed out"})->to_http_message;
798 }
799 }
800
801 0 return @results;
802 }
803  
804 =back
805  
806 =head1 Using with AnyEvent
807  
808 See L<AnyEvent::HTTP::MultiGet>
809  
810 =head1 AUTHOR
811  
812 Mike Shipper L<mailto:AKALINUX@CPAN.ORG>
813  
814 =cut
815  
816 6 1
↑ Summary HTTP/MultiGet/Role.pm »