NAME

Algorithm::LeakyBucket - Perl implementation of leaky bucket rate limiting

SYNOPSIS

use Bucket::Leaky;
my $bucket = Bucket::Leacky->new( ticks => 1, seconds => 1 ); # one per second

while($something_happening)
{
    if ($bucket->tick)
    {
        # allowed
        do_something();
    }
}

DESCRIPTION

Implements leaky bucket as a rate limiter. If you pass memcached options it will also use memcached. If the memcached servers are not availble it falls back to the local counters.

This version uses Cache::Memcached::Fast

my $bucket = Bucket::Leaky->new( ticks => $ticks, seconds => $every_x_seconds,
                                 memcached_key => 'some_key',
                                 memcached_servers => [ { address => 'localhost:11211' } ] );

Multiple instances of the code would all then halt each other from breaking the rate limit. (But see the BUGS section)

This is an early alpha version of the code. I built this as a rate limiter that I could toss in my mod_perl implementations to keep some clients from slamming an API.

BUGS

Probably some. There is a known bug where if you are in an infinite loop you could move faster than memcached could be updated remotely, so you'll likely at that point only bbe limted by the local counters. I'm not sure how im going to fix this yet as this is in early development.

SEE ALSO

http://en.wikipedia.org/wiki/Leaky_bucket

AUTHOR

Marcus Slagle, <marc.slagle@online-rewards.com>

COPYRIGHT AND LICENSE

Copyright (C) 2012 by Marcus Slagle

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.9 or, at your option, any later version of Perl 5 you may have available.