PHP Class 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(); }
Author: Markus Malkusch ([email protected])
Datei anzeigen Open project: bandwidth-throttle/token-bucket Class Usage Examples

Public Methods

Method Description
__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.

Private Methods

Method Description
loadTokensAndTimestamp ( ) : array Loads the stored timestamp and its respective amount of tokens.

Method Details

__construct() public method

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() public method

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() public method

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.
return boolean If tokens were consumed.

getCapacity() public method

The token capacity of this bucket.
public getCapacity ( ) : integer
return integer The capacity.

getRate() public method

Returns the token add rate.
public getRate ( ) : Rate
return Rate The rate.

getTokens() public method

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
return integer amount of currently available tokens