Documentation
¶
Overview ¶
Package leakybucket provides an in-memory implementation of the leaky bucket algorithm for rate limiting.
Summary ¶
The leaky bucket algorithm (wikilink) is often used for rate limits or to create an even stream of traffic from a variable source. This package is only for rate limits.
When configuring a leaky bucket, you specify the allowed throughput (volume and time) and a burst limit.
Throughput limit is the rate at which the bucket drains. In other words, with a full bucket, this is the rate of events that will leak out. The throughput limit should be specified with two parameters, the DrainAmount is the number of entries to leak, and the DrainPeriod is the frequency to drain the bucket. For example, you would specify a throughput limit of 5 entries per second with a DrainAmount of 5 and a DrainPeriod of time.Second
Burst limit is the number of entries you want to allow before starting to reject new entries. This is the size of the bucket - as entries arrive the bucket fills, and when it reaches capacity new entries will be rejected. Burst limit should not be less than the throughput limit (or the throughput limit will never be reached, as the bucket will reject entries when it hits capacity).
When the rate limit is reached and adding a new entry would exceed the burst limit, a BucketOverflow error is returned.
Usage ¶
Create one Bucket for each resource you wish to protect with a rate limit. When a new entry arrives for that resource, attempt to Add() the event to the bucket. If successful, Add() will return nil. If the rate limit has been exceeded, Add() will return a BucketOverflow error.
Buckets have internal locking so Add()ing to a bucket is thread safe.
Example ¶
// Create a new bucket for a rate limit of 10/sec, with a burst limit of 50
b := Bucket{
Capacity: 50,
DrainAmount: 10,
DrainPeriod: time.Second,
}
// override time for the sake of the example. ignore the next 2 lines.
n := newFakeNower()
b.testNower = n
// count successes and failures
var successes, failures int
// Adding the first 50 entries will succeed, the 51st will fail.
// This is us hitting the burst limit.
for i := 0; i < 51; i++ {
if err := b.Add(); err == nil {
successes++
} else {
failures++
}
}
fmt.Printf("Successes: %d, Failures: %d\n", successes, failures)
// "wait" 1 second, and we should be able to add 10 more entries to the bucket
n.incrementSec(1)
// Add another 11 - the first 10 will succeed, the 11th will fail.
// This shows the rate limit constraining throughput
for i := 0; i < 11; i++ {
if err := b.Add(); err == nil {
successes++
} else {
failures++
}
}
fmt.Printf("Successes: %d, Failures: %d\n", successes, failures)
Output: Successes: 50, Failures: 1 Successes: 60, Failures: 2
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bucket ¶
type Bucket struct {
// Capacity of the bucket is how many total spots are available in the bucket.
// It is the burst limit. A capacity of 0 will reject all Add()s
Capacity int
// DrainAmount is the number of spots that open up each drain period. It is
// the volume portion of the throughput limit. A drain amount of 0 means the
// bucket doesn't leak, and once capacity is reached, will reject all
// subsequent events.
DrainAmount int
// DrainPeriod is the frequency the bucket drains. It is the time portion of
// the throughput limit. A drain period of 0 means always drain - every Add()
// also drains, effectively meaning no throughput limit (so long as Capacity
// and DrainAmount are greater than zero).
DrainPeriod time.Duration
// contains filtered or unexported fields
}
Bucket contains the counters and timers to implement your rate limit.
type BucketOverflow ¶
type BucketOverflow struct{}
BucketOverflow error will be returned when you fail to Add() to a bucket because the rate limit has been reached.
func (BucketOverflow) Error ¶
func (b BucketOverflow) Error() string
Error implements the error interface