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])
파일 보기 프로젝트 열기: bandwidth-throttle/token-bucket 1 사용 예제들

공개 메소드들

메소드 설명
__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