简体   繁体   中英

LWP::UserAgent get callback with timeout

Is the chunk lost with this code if the time is out (2 seconds), or does get retry to download the missed chunk?

use LWP::UserAgent;

my $url = '...';
my $file_name = '...';
my $ua = LWP::UserAgent->new();

open my $fh, '>>:raw', $file_name or die $!;
my $res = $ua->get( 
    $url,
    ':content_cb' => sub {
        my ( $chunk, $res, $proto ) = @_;
        eval {
            local $SIG{ALRM} = sub { die "time out\n" };
            alarm 2;
            print $fh $chunk;
            alarm 0;
        };
        # ...
    },
);
close $fh;

If the 'content_cb' callback is called for a chunk, then that means the chunk has been successfully returned from the request. The LWP::UserAgent layer has done its job at that point (with respect to that chunk). Your program is then responsible for doing whatever with the chunk. LWP::UserAgent has no idea about how your program sets or handles system signals, so it can't possibly redo any request, or re-notify your program of any chunk, in response to a system signal or any other event that goes on in the context of your program (and that is outside the context of LPW::UserAgent ).

Furthermore, it should be mentioned that even if you set the LWP::UserAgent timeout property, which applies to pending server activity (such as responding to the request at all, or sending the next chunk), then LWP::UserAgent would not even resend the request in the case of such a timeout. The module has simply not been designed to do that under any circumstances:

The requests is aborted if no activity on the connection to the server is observed for timeout seconds.

You can always resend the request in your code if any kind of timeout occurs, or if your code deems that it has not received sufficient response data from the server.

If you are worried about timeouts, use the timeout method . In your code, when a chunk of data arrives, LWP::UserAgent calls your ':content_cb' callback, and no retry is done.

IMO it has no sense to handle timeout in that way because it will never happens (unless print $fh $chunk; takes its time).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM