ratelimit

package
v0.0.0-...-51c1140 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	// contains filtered or unexported fields
}

A Bucket represents a simple token bucket based rate limiter. Token buckets act as a reservoir with a fixed capacity. In order to perform a rate limited operation, a token must be acquired from the bucket. If sufficient tokens are available, the operation preceeds immediately and those tokens are removed from the bucket. If sufficient tokens are not available the operation is blocked.

See: https://en.wikipedia.org/wiki/Token_bucket

func ConstantRPM

func ConstantRPM(n int) *Bucket

ConstantRPM returns a new token bucket targeting n requests per minute.

func NewBucket

func NewBucket(mean, max uint64, d time.Duration) *Bucket

NewBucket returns a new token bucket with average rate mean/d.Seconds() ops/sec and maximum rate max/d.Seconds() ops/sec.

For constant throughput, set mean == max.

func (*Bucket) Take

func (b *Bucket) Take()

Take acquires a single token from the Bucket. If no tokens are available, Take blocks until the Bucket is refilled.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL