Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type LeakyBucket ¶
type LeakyBucket interface {
// Use atomically attempts to use the leaky bucket. Use takeAmount to set how many tokens should be attempted to be removed
// from the bucket: they are atomic, either all tokens are taken, or the ratelimit is unsuccessful.
Use(ctx context.Context, bucket *LeakyBucketOptions, takeAmount int) (*UseLeakyBucketResponse, error)
}
LeakyBucket defines an interface compatible with LeakyBucketImpl
type LeakyBucketImpl ¶
type LeakyBucketImpl struct {
// Adapter defines the Redis adapter
Adapter adapters.Adapter
// contains filtered or unexported fields
}
LeakyBucketImpl implements a leaky bucket ratelimiter, this struct is compatible with the LeakyBucket interface
func NewLeakyBucket ¶
func NewLeakyBucket(adapter adapters.Adapter) *LeakyBucketImpl
NewLeakyBucket creates a new leaky bucket instance
func (*LeakyBucketImpl) Use ¶
func (r *LeakyBucketImpl) Use(ctx context.Context, bucket *LeakyBucketOptions, takeAmount int) (*UseLeakyBucketResponse, error)
Use atomically attempts to use the leaky bucket. Use takeAmount to set how many tokens should be attempted to be removed from the bucket: they are atomic, either all tokens are taken, or the ratelimit is unsuccessful.
type LeakyBucketOptions ¶
type LeakyBucketOptions struct {
// KeyPrefix is the bucket key name in Redis.
//
// Note that this ratelimiter will create two keys in Redis, and suffix them with :last_fill and :tokens.
KeyPrefix string
// MaximumCapacity defines the maximum number of tokens in the leaky bucket. If a bucket has expired or otherwise doesn't exist,
// the bucket is set to this size, it also ensures the bucket can never contain more than this number of tokens at any time.
//
// Note that if you decrease the number of tokens in an existing bucket, that bucket is automatically reduced to the new max size,
// however, if you increase the maximum capacity of the bucket, it will refill faster, but not immediately be placed to the new, higher
// capacity.
MaximumCapacity int
// WindowSeconds defines the maximum amount of time it takes to refill the bucket, the refill rate of the bucket is calculated using
// maximumCapacity/windowSeconds, in other words, if your capacity was 60 tokens, and the window was 1 minute, you would refill at a constant
// rate of 1 token per second.
//
// Windows have a maximum resolution of 1 second.
WindowSeconds int
}
LeakyBucketOptions defines the options available to LeakyBucket ratelimiters
type UseLeakyBucketResponse ¶
type UseLeakyBucketResponse struct {
// Success is true when we were successfully able to take tokens from the bucket.
Success bool
// RemainingTokens defines hwo many tokens are left in the bucket
RemainingTokens int
// ResetAt is the time at which the bucket will be fully refilled
ResetAt time.Time
}
UseLeakyBucketResponse defines the response parameters for LeakyBucket.Use()
Click to show internal directories.
Click to hide internal directories.