Documentation ¶
Overview ¶
Package tokenbucket implements a token bucket rate limiting algorithm.
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 a token bucket that fills tokens at the specified rate. It allows limiting access to resources by rate.
func New ¶
func New(rate float64, capacity int) *TokenBucket
New creates a new token bucket with the given rate and capacity.
func (*TokenBucket) Available ¶
func (tb *TokenBucket) Available() int
Available returns the number of available tokens.
func (*TokenBucket) Capacity ¶
func (tb *TokenBucket) Capacity() int
Capacity returns the capacity of the bucket.
func (*TokenBucket) Close ¶
func (tb *TokenBucket) Close()
Close stops the filling goroutine and closes channels.
func (*TokenBucket) Put ¶
func (tb *TokenBucket) Put() error
Put returns a token back to the bucket.
func (*TokenBucket) Rate ¶
func (tb *TokenBucket) Rate() float64
Rate returns the fill rate of the bucket.
func (*TokenBucket) Take ¶
func (tb *TokenBucket) Take() error
Take retrieves a token from the bucket. It blocks if no tokens available.
func (*TokenBucket) Wait ¶
func (tb *TokenBucket) Wait()
Wait blocks until a token becomes available.
Click to show internal directories.
Click to hide internal directories.