| 1 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | package LWP::Concurrent; | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Runs multiple LWP Client connections in parallel | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Time::HiRes qw(time sleep); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use HTTP::Async; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | has timeout => ( is => "rw", default => sub { 3.00 } ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | has idlesleep => ( is => "rw", default => sub { 0.03 } ); # same as HTTP::Async's poll_interval | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # my $data = get_concurrent( { urls => [$u1, $u2], timeout=>0.99, idlesleep=>0.01 } ); | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # $data is a hashref where the keys are the urls and the responses are | ||||||||||||||||||||||||||||||||||||||||||
| 21 | # HTTP::Response objects. | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # as they come back from HTTP::Async. See text after the __END_ tag below for an example. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub get_concurrent { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_concurrent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | my ($self, %hash) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my ($urls ) = @hash{ qw( urls ) }; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | my @requests = map { HTTP::Request->new( GET => $_ ) } @$urls; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | my $ret = $self->operate_concurrently( requests => \@requests ); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | return $ret; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | sub operate_concurrently { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
operate_concurrently: called CC=6 SCAR=22.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | my ($self, %hash) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | my $requests = $hash{requests}; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | my $async = HTTP::Async->new( timeout=>$self->timeout, poll_interval=>$self->idlesleep ); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | my $start_t = time(); | |||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | my $now; | |||||||||||||||||||||||||||||||||||||||||
| 38 | my %to_return; | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | my $counter = 0; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | for my $request (@$requests) { | |||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | $async->add( $request ); # an HTTP::Request object | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | while ( ($now = time()) && $now - $start_t < $self->timeout && $async->not_empty ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$now = &LWP::Concurrent::time() and $now - $start_t < $self->timeout and $async->not_empty
$now = &LWP::Concurrent::time() and $now - $start_t < $self->timeout
MC/DC0%0/3
$now = &LWP::Concurrent::time() and $now - $start_t < $self->timeout and $async->not_empty
$now = &LWP::Concurrent::time()
$now - $start_t < $self->timeout
$async->not_empty
Truth table25%1/4
A and B and C
A= $now = &LWP::Concurrent::time()B= $now - $start_t < $self->timeoutC= $async->not_empty
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ( my $response = $async->wait_for_next_response($self->idlesleep) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my $uri = $response->request()->url(); | |||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $to_return{$counter} = {uri=>$uri, response=>$response}; # uri => the response _object_ | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | $counter++; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # else { | ||||||||||||||||||||||||||||||||||||||||||
| 50 | #sleep( $self->idlesleep ) if $self->idlesleep; # sleep a while if the caller so desires | ||||||||||||||||||||||||||||||||||||||||||
| 51 | #} | ||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 1 | return \%to_return; # return the data we found; keys are the response numbers, | |||||||||||||||||||||||||||||||||||||||||
| 54 | # values are a hash with urls and HTTP::Response objects. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | LWP::Concurrent -- Provides easy interface to making parallel/concurrent LWP requests | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | my $lwpc = LWP::Concurrent->new() | ||||||||||||||||||||||||||||||||||||||||||
| 69 | my $responses = $lwpc->get_concurrent( urls=>[ "http://example.com/url1", "http://example.com/url2" ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | Makes concurrent LWP requests | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =item $lwpc = LWP::Concurrent->new( ); # or | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =item $lwpc = LWP::Concurrent->new( idlesleep => 0.05, timeout => 0.4 ); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | returns a new LWP::Concurrent object. | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =item $lwpc->timeout() | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =item $lwpc->idlesleep() | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | Gets or sets the timeout or idlesleep params. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =item $results = $lwpc->get_concurrent( urls=> \@urls ) | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | performs a GET on the specified urls, returning a hashref where the keys are the response numbers, | ||||||||||||||||||||||||||||||||||||||||||
| 94 | and the values are the urls and their responses | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | =item $results = $lwpc->operate_concurrently( requests => \@request_objects ) | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | performs actions on HTTP servers as specified by the passed request objects, | ||||||||||||||||||||||||||||||||||||||||||
| 99 | returning a hashref where the keys are the response numbers, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | and the values are the urls and their responses | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =head1 TO DO | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | If you want such a section. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | None | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | Copyright (c) 2012 Josh Rabinowitz, All Rights Reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | Josh Rabinowitz | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||