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.