other.token_bucket¶
Implementation of the Token Bucket Algorithm Token rate is added to the bucket every frequency seconds. The bucket can hold tokens up to capacity (full). The bucket starts full. Each request consumes one token. If a token arrives when the bucket is full, the token is discarded. If a request arrives when the bucket is empty, it is discarded. If the bucket has tokens available, requests will pass. https://en.wikipedia.org/wiki/Token_bucket
Attributes¶
Classes¶
Module Contents¶
- class other.token_bucket.TokenBucketRateLimiter(rate: int, capacity: int, frequency: int)¶
- _add_tokens() None¶
Refill tokens only when a full minute has passed. >>> bucket = TokenBucketRateLimiter(1, 4, 60) >>> bucket.tokens # Initially has a rate of 4 tokens 4 >>> bucket._add_tokens() >>> bucket.tokens # Bucket already full 4
- allow_request() bool¶
Check if a request is allowed. If there are enough tokens, it consumes one token. :return: True if the request is allowed, False otherwise. >>> bucket = TokenBucketRateLimiter(1, 2, 60) >>> bucket.allow_request() # Token is available, request passes True >>> bucket.allow_request() # Token is available, request passes True >>> bucket.allow_request() # No token left, request is dropped False
- capacity¶
- frequency¶
- last_checked¶
- lock¶
- rate¶
- tokens¶
- other.token_bucket.bucket¶