Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
40 lines (26 sloc) 1.43 KB


There are many existing algorithms for throttling, you need to choose the one that fits the best your needs. At the moment, only the following algorithm can be found in Tolerance:

Each implementation implements the RateLimit interface that contains the following methods:

  • hasReachedLimit(string $identifier): bool Returns true if the given identifier reached the limit
  • getTicksBeforeUnderLimit(string $identifier): float Returns the number of ticks that represents the moment when the rate will be under the limit.
  • tick(string $identifier) Register a tick on the bucket, meaning that an operation was executed

Leaky bucket

The leaky bucket algorithm ensure that the number of operations won't exceed a rate on a given rolling time frame.

use Tolerance\Throttling\Rate\TimeRate;
use Tolerance\Throttling\RateLimit\LeakyBucket;
use Tolerance\Throttling\RateMeasureStorage\InMemoryStorage;

$rateLimit = new LeakyBucket(
    new InMemoryStorage(),
    new TimeRate(10, TimeRate::PER_SECOND)

You can have a look to the LeakyBucket unit tests to have a better idea of how you can use it directly.