Algorithm-TokenBucket
view release on metacpan or search on metacpan
`conform($N)` check in `until($N)` seconds instead of checking
repeatedly.
Note that `until()` does not take into account `burst size`. This means
that a bucket will not conform to _N_ even after sleeping for `until($N)`
seconds if _N_ is greater than `burst size`.
- get\_token\_count()
Returns the current number of tokens in the bucket. This method may be
useful for inspection or debugging purposes. You should not examine
the state of the bucket for rate limiting purposes.
This number will frequently be fractional so it is not exactly a
"count".
# EXAMPLES
Imagine a rate limiter for a mail sending application. We would like to
allow 2 mails per minute but no more than 20 mails per hour.
}
}
# we're likely to have processed 200 items (without hogging the CPU)
# BUGS
Documentation lacks the actual algorithm description. See links or read
the source (there are about 20 lines of sparse Perl in several subs).
`until($N)` does not return infinity if `$N` is greater than `burst
size`. Sleeping for infinity seconds is both useless and hard to debug.
# ACKNOWLEDGMENTS
Yuval Kogman contributed the ["until($)"](#until) method, proper [Storable](https://metacpan.org/pod/Storable) support
and other things.
Alexey Shrub contributed the ["get\_token\_count()"](#get_token_count) method.
Paul Cochrane contributed various documentation and infrastructure fixes.
lib/Algorithm/TokenBucket.pm view on Meta::CPAN
return 0;
} else {
my $needed = $size - $self->{_tokens};
return ($needed / $self->{info_rate});
}
}
=item get_token_count()
Returns the current number of tokens in the bucket. This method may be
useful for inspection or debugging purposes. You should not examine
the state of the bucket for rate limiting purposes.
This number will frequently be fractional so it is not exactly a
"count".
=cut
sub get_token_count {
my Algorithm::TokenBucket $self = shift;
$self->_token_flow;
lib/Algorithm/TokenBucket.pm view on Meta::CPAN
}
}
# we're likely to have processed 200 items (without hogging the CPU)
=head1 BUGS
Documentation lacks the actual algorithm description. See links or read
the source (there are about 20 lines of sparse Perl in several subs).
C<until($N)> does not return infinity if C<$N> is greater than C<burst
size>. Sleeping for infinity seconds is both useless and hard to debug.
=head1 ACKNOWLEDGMENTS
Yuval Kogman contributed the L</until($)> method, proper L<Storable> support
and other things.
Alexey Shrub contributed the L</get_token_count()> method.
Paul Cochrane contributed various documentation and infrastructure fixes.
( run in 1.467 second using v1.01-cache-2.11-cpan-49f99fa48dc )