| 1 | 5 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 5 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | package HTTP::Async::Polite; | ||||||||||||||||||||
| 5 | 5 | ▶ | use base 'HTTP::Async'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 5 | our $VERSION = '0.31'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 5 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 5 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 5 | ▶ | use Time::HiRes qw( time sleep ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 5 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | =head1 NAME | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | HTTP::Async::Polite - politely process multiple HTTP requests | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 SYNOPSIS | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | See L<HTTP::Async> - the usage is unchanged. | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | This L<HTTP::Async> module allows you to have many requests going on at once. | ||||||||||||||||||||
| 25 | This can be very rude if you are fetching several pages from the same domain. | ||||||||||||||||||||
| 26 | This module add limits to the number of simultaneous requests to a given | ||||||||||||||||||||
| 27 | domain and adds an interval between the requests. | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | In all other ways it is identical in use to the original L<HTTP::Async>. | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head1 NEW METHODS | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =head2 send_interval | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | Getter and setter for the C<send_interval> - the time in seconds to leave | ||||||||||||||||||||
| 36 | between each request for a given domain. By default this is set to 5 seconds. | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =cut | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub send_interval { | ||||||||||||||||||||
| 41 | 9 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
send_interval: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 42 | return scalar @_ | ||||||||||||||||||||
| 43 | 9 | ▶ | ? $self->_set_opt( 'send_interval', @_ ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | : $self->_get_opt('send_interval'); | ||||||||||||||||||||
| 45 | } | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | =head1 OVERLOADED METHODS | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | These methods are overloaded but otherwise work exactly as the original | ||||||||||||||||||||
| 50 | methods did. The docs here just describe what they do differently. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head2 new | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | Sets the C<send_interval> value to the default of 5 seconds. | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =cut | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | sub new { | ||||||||||||||||||||
| 59 | 7 | ▶ | my $class = shift; | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 7 | my $self = $class->SUPER::new; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | # Set the interval between sends. | ||||||||||||||||||||
| 64 | 7 | $self->{opts}{send_interval} = 5; # seconds | |||||||||||||||||||
| 65 | 7 | $class->_add_get_set_key('send_interval'); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 7 | $self->_init(@_); | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 7 | return $self; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =head2 add_with_opts | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | Adds the request to the correct queue depending on the domain. | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | =cut | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | sub add_with_opts { | ||||||||||||||||||||
| 79 | 7 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
add_with_opts: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 80 | 7 | my $req = shift; | |||||||||||||||||||
| 81 | 7 | my $opts = shift; | |||||||||||||||||||
| 82 | 7 | my $id = $self->_next_id; | |||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # Instead of putting this request and opts directly onto the to_send array | ||||||||||||||||||||
| 85 | # instead get the domain and add it to the domain's queue. Store this | ||||||||||||||||||||
| 86 | # domain with the opts so that it is easy to get at. | ||||||||||||||||||||
| 87 | 7 | my $uri = URI->new( $req->uri ); | |||||||||||||||||||
| 88 | 7 | my $host = $uri->host; | |||||||||||||||||||
| 89 | 7 | my $port = $uri->port; | |||||||||||||||||||
| 90 | 7 | my $domain = "$host:$port"; | |||||||||||||||||||
| 91 | 7 | $opts->{_domain} = $domain; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | # Get the domain array - create it if needed. | ||||||||||||||||||||
| 94 | 7 | ▶ | my $domain_arrayref = $self->{domain_stats}{$domain}{to_send} ||= []; | ||||||||||||||||||
|
Condition100%2/2
$self->{"domain_stats"}{$domain}{"to_send"} ||= []
MC/DC100%1/1
$self->{"domain_stats"}{$domain}{"to_send"} ||= []
$self->{"domain_stats"}{$domain}{"to_send"}
Truth table100%2/2
A
A= $self->{"domain_stats"}{$domain}{"to_send"}
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 7 | push @{$domain_arrayref}, [ $req, $id ]; | |||||||||||||||||||
| 97 | 7 | $self->{id_opts}{$id} = $opts; | |||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 7 | $self->poke; | |||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 7 | return $id; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =head2 to_send_count | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | Returns the number of requests waiting to be sent. This is the number in the | ||||||||||||||||||||
| 107 | actual queue plus the number in each domain specific queue. | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | sub to_send_count { | ||||||||||||||||||||
| 112 | 200 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
to_send_count: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 113 | 200 | $self->poke; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 200 | my $count = scalar @{ $$self{to_send} }; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 388 | $count += scalar @{ $self->{domain_stats}{$_}{to_send} } | |||||||||||||||||||
| 118 | 200 | for keys %{ $self->{domain_stats} }; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 200 | return $count; | |||||||||||||||||||
| 121 | } | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | sub _process_to_send { | ||||||||||||||||||||
| 124 | 605 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_process_to_send: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | # Go through the domain specific queues and add all requests that we can | ||||||||||||||||||||
| 127 | # to the real queue. | ||||||||||||||||||||
| 128 | 605 | foreach my $domain ( keys %{ $self->{domain_stats} } ) { | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 1171 | my $domain_stats = $self->{domain_stats}{$domain}; | |||||||||||||||||||
| 131 | 1171 | ▶ | next unless scalar @{ $domain_stats->{to_send} }; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | # warn "TRYING TO ADD REQUEST FOR $domain"; | ||||||||||||||||||||
| 134 | # warn sleep 5; | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | # Check that this request is good to go. | ||||||||||||||||||||
| 137 | 960 | ▶ | next if $domain_stats->{count}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 921 | ▶ | next unless time > ( $domain_stats->{next_send} || 0 ); | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$domain_stats->{'next_send'} || 0
MC/DC100%1/1
$domain_stats->{'next_send'} || 0
$domain_stats->{'next_send'}
Truth table100%2/2
A
A= $domain_stats->{'next_send'}
| |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | # We can add this request. | ||||||||||||||||||||
| 141 | 7 | $domain_stats->{count}++; | |||||||||||||||||||
| 142 | 7 | push @{ $self->{to_send} }, shift @{ $domain_stats->{to_send} }; | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | # Use the original to send the requests on the queue. | ||||||||||||||||||||
| 146 | 605 | return $self->SUPER::_process_to_send; | |||||||||||||||||||
| 147 | } | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | sub _add_to_return_queue { | ||||||||||||||||||||
| 150 | 7 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
_add_to_return_queue: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 151 | 7 | my $req_and_id = shift; | |||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | # decrement the count for this domain so that another request can start. | ||||||||||||||||||||
| 154 | # Also set the interval so that we don't scrape too fast. | ||||||||||||||||||||
| 155 | 7 | my $id = $req_and_id->[1]; | |||||||||||||||||||
| 156 | 7 | my $domain = $self->{id_opts}{$id}{_domain}; | |||||||||||||||||||
| 157 | 7 | my $domain_stat = $self->{domain_stats}{$domain}; | |||||||||||||||||||
| 158 | 7 | my $interval = $self->_get_opt( 'send_interval', $id ); | |||||||||||||||||||
| 159 | |||||||||||||||||||||
| 160 | 7 | $domain_stat->{count}--; | |||||||||||||||||||
| 161 | 7 | $domain_stat->{next_send} = time + $interval; | |||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | 7 | return $self->SUPER::_add_to_return_queue($req_and_id); | |||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | =head1 SEE ALSO | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | L<HTTP::Async> - the module that this one is based on. | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head1 AUTHOR | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | Edmund von der Burg C<< <evdb@ecclestoad.co.uk> >>. | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | L<http://www.ecclestoad.co.uk/> | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | Copyright (c) 2006, Edmund von der Burg C<< <evdb@ecclestoad.co.uk> >>. | ||||||||||||||||||||
| 179 | All rights reserved. | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 182 | the same terms as Perl itself. | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | =head1 DISCLAIMER OF WARRANTY | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE | ||||||||||||||||||||
| 187 | SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE | ||||||||||||||||||||
| 188 | STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE | ||||||||||||||||||||
| 189 | SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, | ||||||||||||||||||||
| 190 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | ||||||||||||||||||||
| 191 | FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND | ||||||||||||||||||||
| 192 | PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, | ||||||||||||||||||||
| 193 | YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY | ||||||||||||||||||||
| 196 | COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE | ||||||||||||||||||||
| 197 | SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, | ||||||||||||||||||||
| 198 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING | ||||||||||||||||||||
| 199 | OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO | ||||||||||||||||||||
| 200 | LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR | ||||||||||||||||||||
| 201 | THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER | ||||||||||||||||||||
| 202 | SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE | ||||||||||||||||||||
| 203 | POSSIBILITY OF SUCH DAMAGES. | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =cut | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 5 | 1; | |||||||||||||||||||
| 208 | |||||||||||||||||||||