Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TokenBucket ¶
type TokenBucket struct {
// contains filtered or unexported fields
}
TokenBucket implements the token bucket rate-limiting algorithm. Once configured with its parameters, users can get the current number of tokens in the bucket or attempt to use some tokens (non-blocking).
func NewTokenBucket ¶
func NewTokenBucket( initCount int64, maxCount int64, refillRate time.Duration, ) (*TokenBucket, error)
NewTokenBucket creates a new token bucket. The first parameters specify the inital and maximum token counts of this bucket. One token is added every `refillRate` intervals.
func (*TokenBucket) Count ¶
func (b *TokenBucket) Count() int64
Count retrieves the number of tokens currently in the bucket.
func (*TokenBucket) TryUse ¶
func (b *TokenBucket) TryUse(count int64) (int64, bool)
TryUse attempts to take the given number of tokens from the bucket in a non-blocking manner. The return value shows the current count of tokens and whether the operation succeeded (if it didn't succeed, the token count is unchanged).