in reply to Ways to limit bandwidth of downloads?

Here's one way. If you supply the hash => \*FHGLOB parameter on the Net:FTP constructor, then a "hash mark" will be printed to the Filehandle glob each buffer load. If you tie the glob before passing it, the PRINT method will be called after each buffer is read. If you insert a short sleep at that point, you will effectively throttle the request for the buffer load.

This demonstrates the technique. Performing the calculations to allow the download rate to be specified and wrapping it up into clean interface is left as an exercise for those that need it.

#! perl -slw use strict; use Time::HiRes qw[ time sleep ]; use Net::FTP; $|=1; sub TIEHANDLE { return bless [ 0 ], $_[0] } sub PRINT{ my $self = shift; if( $self->[ 0 ] ) { my $delay = ( $self->[ 0 ]+1 - time() ); printf "%f\n", $delay; sleep 1+$delay; ## Insert delay } $self->[ 0 ] = time(); } local *GLOB; tie *GLOB, 'main'; my( $site, $dir, $file ) = $ARGV[ 0 ] =~ m[ ^(?:ftp://)? ([^/]+) (/.*?) / ([^/]+$) ]x or die "Couldn't parse url"; my $ftp = Net::FTP->new( $site, Hash => \*GLOB ) or die $@; $ftp->login( 'anonymous', 'anonymous@' ); $ftp->cwd( $dir ) or die $@; $ftp->get( $file ) or die $@;

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Ways to limit bandwidth of downloads?
by Tanktalus (Canon) on Apr 18, 2006 at 16:45 UTC

    That actually seems like overkill ... ;-) After all, I just wrote in TIMTOWTDI Challenge: Open a file how to use the retr function of Net::FTP to grab stuff - inserting the sleep in there may be simpler than having to tie stuff ;-)

    (And, when I wrote it, I thought it was completely obscure ... here, less than one day later, it's coming in handy!)

      Maybe...though when I played with subclassing Net::FTP when trying to track down a mysterious bug some time ago, I found that once you start messing with doing the buffering yourself, you rapidly end up duplicating quite a lot of what Net::FTP normally does for you. I was more interested in finding the bug than solving the issues with the subclassing, so it's quite possible I was just doing it wrong.

      I thought the tie solution was rather nice because it relied on a documented interface but otherwise left the internals untouched. Theory says that substituting a subclass of a module for the module itself should be simple, but the practice often proves otherwise I found. But, TIMTOWTDI. If you can avoid doing the buffering yourself, then overriding the retr method should work just as well :)


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.