Front page | perl.libwww |
Postings from March 2002
LWP::HalfSleepyUA
Thread Next
From:
Sean M. Burke
Date:
March 19, 2002 02:28
Subject:
LWP::HalfSleepyUA
Message ID:
5.1.0.14.1.20020319024103.0268b390@mail.spinn.net
I use LWP mainly on my laptop, which accesses the Internet across slow
dialup. And I was thinking today how I wanted a LWP::UserAgent object
which was sort of like LWP::RobotUA in that it delayed between requests so
that it wouldn't be using too much of my bandwidth -- except behaving a bit
different.
So after some pondering, I made an LWP subclass that does just that -- it's
asleep (about) half the time, hence its name: LWP::HalfSleepyUA. And here
it is!
Should I release this to CPAN? It's trivial, but who knows, it may be
useful to someone:
require 5;
package LWP::HalfSleepyUA;
use LWP ();
use LWP::Debug ();
use LWP::UserAgent ();
@ISA = ('LWP::UserAgent');
use strict;
sub simple_request {
my $this = shift;
if(defined( $this->{'_not_until'} )) {
my $sleep = $this->{'_not_until'} - time();
if($sleep > 0) {
LWP::Debug::debug("Sleeping for $sleep seconds.");
sleep($sleep);
}
}
my $start_time = time();
my $resp = $this->SUPER::simple_request(@_);
$this->{'_not_until'} = time() + (time() - $start_time) + 1;
# We could be clever and do 2*time() - $start_time + 1, but
# that could cause us to stop dealing with INTs.
# Well, we shouldn't really bother sleeping if the request is
# to a data: URL, but that just means less sleeping /next/
# time! Funny how it all works out right that way.
LWP::Debug::debug('No further requests until T='
. $this->{'_not_until'} . 's!');
return $resp;
}
1;
__END__
--
Sean M. Burke sburke@cpan.org http://www.spinn.net/~sburke/
Thread Next
-
LWP::HalfSleepyUA
by Sean M. Burke