Skip to content

Commit

Permalink
New global upload/download limits
Browse files Browse the repository at this point in the history
  • Loading branch information
sanko committed Jun 22, 2010
1 parent f74dcf2 commit 3a25c0a
Showing 1 changed file with 54 additions and 24 deletions.
78 changes: 54 additions & 24 deletions lib/AnyEvent/Handle/Throttle.pm
Expand Up @@ -15,38 +15,61 @@ package AnyEvent::Handle::Throttle;
sub download_limit {
$_[1] ? $_[0]->{download_limit} = $_[1] : $_[0]->{download_limit};
}
sub upload_period { $_[0]->{upload_period} }
sub download_period { $_[0]->{download_period} }
sub upload_speed { $_[0]->{upload_speed} }
sub download_speed { $_[0]->{download_speed} }
my ($global_upload_limit, $global_download_limit,
$global_upload_speed, $global_download_speed);
my $global_period = 1;

sub global_upload_limit {
$_[1] ? $global_upload_limit = $_[1] : $global_upload_limit;
}

sub global_download_limit {
$_[1] ? $global_download_limit = $_[1] : $global_download_limit;
}
sub global_upload_speed {$global_upload_speed}
sub global_download_speed {$global_download_speed}
my ($global_read_size, $global_write_size,
$global__upload_speed, $global__download_speed);
my $global_reset_cb = sub {
$global_read_size = $global_download_limit;
$global_write_size = $global_upload_limit || 8 * 1024;
$global_upload_speed = $global__upload_speed;
$global_download_speed = $global__download_speed;
$global__upload_speed = $global__download_speed = 0;
};
$global_reset_cb->();
our $global_reset = AE::timer(0, $global_period, $global_reset_cb);

sub _start {
my $self = shift;
$self->SUPER::_start(@_);
my $self = shift;
my $reset = sub {
$self->{read_size} = $self->{download_rate};
$self->{write_size} = $self->{upload_rate} || 8 * 1024;
$self->{upload_period} = $self->{_upload_period};
$self->{download_period} = $self->{_download_period};
$self->{_upload_period} = $self->{_download_period} = 0;
$self->{read_size} = $self->{download_limit};
$self->{write_size} = $self->{upload_limit} || 8 * 1024;
$self->{upload_speed} = $self->{_upload_speed};
$self->{download_speed} = $self->{_download_speed};
$self->{_upload_speed} = $self->{_download_speed} = 0;
};
$self->{_period} ||= 1;
$self->{_reset} = AE::timer(0, $self->{_period}, $reset);
$reset->();
$self->SUPER::_start(@_);
}

sub start_read {
my ($self) = @_;
unless ($self->{_rw} || $self->{_eof} || !$self->{fh}) {
Scalar::Util::weaken $self;
$self->{_rw} = AE::io $self->{fh}, 0, sub {
my $_read
= defined $self->{download_rate}
&& defined $self->{read_size} ? $self->{read_size}
: defined $self->{rbuf_max} ? $self->{rbuf_max}
: ();
if (defined $_read && $_read == 0) {
my ($read) = sort grep {defined} $global_read_size,
$self->{read_size};
my ($period) = sort grep {defined} $global_period,
$self->{_period};
if (defined $read && $read <= 0) {
$self->stop_read;
return $self->{_pause_read} = AE::timer(
0.5, 0,
$period, 0,
sub {
delete $self->{_pause_read};
$self->start_read;
Expand All @@ -55,11 +78,13 @@ package AnyEvent::Handle::Throttle;
}
my $rbuf = \($self->{tls} ? my $buf : $self->{rbuf});
$$rbuf ||= '';
my $len = sysread $self->{fh}, $$rbuf, $_read || 8192,
my $len = sysread $self->{fh}, $$rbuf, $read || 8192,
length $$rbuf;
if ($len > 0) {
$self->{read_size} -= $len;
$self->{_download_period} += $len;
$global_read_size -= $len;
$self->{_download_speed} += $len;
$global__download_speed += $len;
$self->{_activity} = $self->{_ractivity} = AE::now;
if ($self->{tls}) {
Net::SSLeay::BIO_write($self->{_rbio}, $$rbuf);
Expand Down Expand Up @@ -91,7 +116,11 @@ package AnyEvent::Handle::Throttle;
if length $self->{wbuf};
};
$cb = sub {
if (!$self->{write_size}) {
my ($write) = sort grep {defined} $global_write_size,
$self->{write_size};
my ($period) = sort grep {defined} $global_period,
$self->{_period};
if (defined $write && $write <= 0) {
if (length $self->{wbuf}) {
delete $self->{_ww};
return $self->{_pause_ww} = AE::timer(
Expand All @@ -104,11 +133,12 @@ package AnyEvent::Handle::Throttle;
}
return 1;
}
my $len = syswrite $self->{fh}, $self->{wbuf},
$self->{write_size};
my $len = syswrite $self->{fh}, $self->{wbuf}, $write;
if (defined $len) {
$self->{write_size} -= $len;
$self->{_upload_period} += $len;
$global_write_size -= $len;
$self->{_upload_speed} += $len;
$global__upload_speed += $len;
substr $self->{wbuf}, 0, $len, "";
$self->{_activity} = $self->{_wactivity} = AE::now;
$self->{on_drain}($self)
Expand Down Expand Up @@ -211,11 +241,11 @@ Sets/returns the current upload rate in bytes per period.
Sets/returns the current download rate in bytes per period.
=item $bytes = $handle->B<upload_period>( )
=item $bytes = $handle->B<upload_speed>( )
Returns the amount of data written during the previous period.
=item $bytes = $handle->B<download_period>( )
=item $bytes = $handle->B<download_speed>( )
Returns the amount of data read during the previous period.
Expand Down

0 comments on commit 3a25c0a

Please sign in to comment.