Documentation
¶
Overview ¶
Package ratelimit provides an efficient token bucket implementation that can be used to limit the rate of arbitrary things. See http://en.wikipedia.org/wiki/Token_bucket.
Index ¶
- func Conn(w net.Conn, bucket *Bucket) net.Conn
- func NewPipes(rate float64, capacity int64) (net.Conn, net.Conn)
- func NewPipesWithClock(rate float64, capacity int64, clock Clock) (net.Conn, net.Conn)
- func NewPipesWithRates(rate1, rate2 float64, capacity1, capacity2 int64, clock1, clock2 Clock) (net.Conn, net.Conn)
- func ReadWriteCloser(w io.ReadWriteCloser, bucket *Bucket) io.ReadWriteCloser
- func Reader(r io.Reader, bucket *Bucket) io.Reader
- func Writer(w io.Writer, bucket *Bucket) io.Writer
- type Bucket
- func NewBucket(fillInterval time.Duration, capacity int64) *Bucket
- func NewBucketWithClock(fillInterval time.Duration, capacity int64, clock Clock) *Bucket
- func NewBucketWithQuantum(fillInterval time.Duration, capacity, quantum int64) *Bucket
- func NewBucketWithQuantumAndClock(fillInterval time.Duration, capacity, quantum int64, clock Clock) *Bucket
- func NewBucketWithRate(rate float64, capacity int64) *Bucket
- func NewBucketWithRateAndClock(rate float64, capacity int64, clock Clock) *Bucket
- func (tb *Bucket) Available() int64
- func (tb *Bucket) Capacity() int64
- func (tb *Bucket) Rate() float64
- func (tb *Bucket) Take(count int64) time.Duration
- func (tb *Bucket) TakeAvailable(count int64) int64
- func (tb *Bucket) TakeMaxDuration(count int64, maxWait time.Duration) (time.Duration, bool)
- func (tb *Bucket) Wait(count int64)
- func (tb *Bucket) WaitMaxDuration(count int64, maxWait time.Duration) bool
- type Clock
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewPipesWithClock ¶
func NewPipesWithRates ¶
func ReadWriteCloser ¶
func ReadWriteCloser(w io.ReadWriteCloser, bucket *Bucket) io.ReadWriteCloser
Types ¶
type Bucket ¶
type Bucket struct {
// contains filtered or unexported fields
}
Bucket represents a token bucket that fills at a predetermined rate. Methods on Bucket may be called concurrently.
func NewBucket ¶
NewBucket returns a new token bucket that fills at the rate of one token every fillInterval, up to the given maximum capacity. Both arguments must be positive. The bucket is initially full.
func NewBucketWithClock ¶
NewBucketWithClock is identical to NewBucket but injects a testable clock interface.
func NewBucketWithQuantum ¶
NewBucketWithQuantum is similar to NewBucket, but allows the specification of the quantum size - quantum tokens are added every fillInterval.
func NewBucketWithQuantumAndClock ¶
func NewBucketWithQuantumAndClock(fillInterval time.Duration, capacity, quantum int64, clock Clock) *Bucket
NewBucketWithQuantumAndClock is like NewBucketWithQuantum, but also has a clock argument that allows clients to fake the passing of time. If clock is nil, the system clock will be used.
func NewBucketWithRate ¶
NewBucketWithRate returns a token bucket that fills the bucket at the rate of rate tokens per second up to the given maximum capacity. Because of limited clock resolution, at high rates, the actual rate may be up to 1% different from the specified rate.
func NewBucketWithRateAndClock ¶
NewBucketWithRateAndClock is identical to NewBucketWithRate but injects a testable clock interface.
func (*Bucket) Available ¶
Available returns the number of available tokens. It will be negative when there are consumers waiting for tokens. Note that if this returns greater than zero, it does not guarantee that calls that take tokens from the buffer will succeed, as the number of available tokens could have changed in the meantime. This method is intended primarily for metrics reporting and debugging.
func (*Bucket) Take ¶
Take takes count tokens from the bucket without blocking. It returns the time that the caller should wait until the tokens are actually available.
Note that if the request is irrevocable - there is no way to return tokens to the bucket once this method commits us to taking them.
func (*Bucket) TakeAvailable ¶
TakeAvailable takes up to count immediately available tokens from the bucket. It returns the number of tokens removed, or zero if there are no available tokens. It does not block.
func (*Bucket) TakeMaxDuration ¶
TakeMaxDuration is like Take, except that it will only take tokens from the bucket if the wait time for the tokens is no greater than maxWait.
If it would take longer than maxWait for the tokens to become available, it does nothing and reports false, otherwise it returns the time that the caller should wait until the tokens are actually available, and reports true.
func (*Bucket) WaitMaxDuration ¶
WaitMaxDuration is like Wait except that it will only take tokens from the bucket if it needs to wait for no greater than maxWait. It reports whether any tokens have been removed from the bucket If no tokens have been removed, it returns immediately.