PHP Класс bandwidthThrottle\tokenBucket\TokenBucket

The token bucket algorithm can be used for controlling the usage rate of a resource. The scope of that rate is determined by the underlying storage. Example: use bandwidthThrottle\tokenBucket\Rate; use bandwidthThrottle\tokenBucket\TokenBucket; use bandwidthThrottle\tokenBucket\storage\FileStorage; $storage = new FileStorage(__DIR__ . "/api.bucket"); $rate = new Rate(10, Rate::SECOND); $bucket = new TokenBucket(10, $rate, $storage); $bucket->bootstrap(10); if (!$bucket->consume(1, $seconds)) { http_response_code(429); header(sprintf("Retry-After: %d", floor($seconds))); exit(); }
Автор: Markus Malkusch ([email protected])
Показать файл Открыть проект Примеры использования класса

Открытые методы

Метод Описание
__construct ( integer $capacity, Rate $rate, bandwidthThrottle\tokenBucket\storage\Storage $storage ) Initializes the Token bucket.
bootstrap ( integer $tokens ) Bootstraps the storage with an initial amount of tokens.
consume ( integer $tokens, &$seconds ) : boolean Consumes tokens from the bucket.
getCapacity ( ) : integer The token capacity of this bucket.
getRate ( ) : Rate Returns the token add rate.
getTokens ( ) : integer Returns the currently available tokens of this bucket.

Приватные методы

Метод Описание
loadTokensAndTimestamp ( ) : array Loads the stored timestamp and its respective amount of tokens.

Описание методов

__construct() публичный Метод

The storage determines the scope of the bucket.
public __construct ( integer $capacity, Rate $rate, bandwidthThrottle\tokenBucket\storage\Storage $storage )
$capacity integer positive capacity of the bucket
$rate Rate rate
$storage bandwidthThrottle\tokenBucket\storage\Storage storage

bootstrap() публичный Метод

If the storage was already bootstrapped this method returns silently. While you could call bootstrap() on each request, you should not do that! This method will do unnecessary storage communications just to see that bootstrapping was performed already. You therefore should call that method in your application's bootstrap or deploy process. This method is threadsafe.
public bootstrap ( integer $tokens )
$tokens integer Initial amount of tokens, default is 0.

consume() публичный Метод

This method consumes only tokens if there are sufficient tokens available. If there aren't sufficient tokens, no tokens will be removed and the remaining seconds to wait are written to $seconds. This method is threadsafe.
public consume ( integer $tokens, &$seconds ) : boolean
$tokens integer The token amount.
Результат boolean If tokens were consumed.

getCapacity() публичный Метод

The token capacity of this bucket.
public getCapacity ( ) : integer
Результат integer The capacity.

getRate() публичный Метод

Returns the token add rate.
public getRate ( ) : Rate
Результат Rate The rate.

getTokens() публичный Метод

This is a purely informative method. Use this method if you are interested in the amount of remaining tokens. Those tokens could be consumed instantly. This method will not consume any token. Use {@link consume()} to do so. This method will never return more than the capacity of the bucket.
public getTokens ( ) : integer
Результат integer amount of currently available tokens