Documentation
¶
Overview ¶
Package ratelimiter implements two common rate limiters; queue and token/leaky bucket.
Index ¶
Constants ¶
const (
// DefaultCopyChunkSizeBytes is the write chunk size in bytes.
DefaultCopyChunkSizeBytes = 32 * 1024
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CopyOption ¶ added in v1.20211204.3
type CopyOption func(*CopyOptions)
CopyOption mutates CopyOptions.
func OptCopyBuffer ¶ added in v1.20211204.3
func OptCopyBuffer(buf []byte) CopyOption
OptCopyBuffer sets the buffer for the copy.
func OptCopyChunkSize ¶ added in v1.20211204.3
func OptCopyChunkSize(cs int) CopyOption
OptCopyChunkSize sets the quantum portion of the rate.
func OptCopyOnWrite ¶ added in v1.20211204.3
func OptCopyOnWrite(handler func(bytesWritten int, elapsed time.Duration)) CopyOption
OptCopyOnWrite sets the on write handler for the copy.
func OptCopyRateBytes ¶ added in v1.20211204.3
func OptCopyRateBytes(b int64) CopyOption
OptCopyRateBytes sets the bytes portion of the rate.
func OptCopyRateQuantum ¶ added in v1.20211204.3
func OptCopyRateQuantum(q time.Duration) CopyOption
OptCopyRateQuantum sets the quantum portion of the rate.
type CopyOptions ¶ added in v1.20211204.3
type CopyOptions struct { RateBytes int64 RateQuantum time.Duration ChunkSize int Buffer []byte OnWrite func(int, time.Duration) }
CopyOptions are options for the throttled copy.
type LeakyBucket ¶
type LeakyBucket struct { NumActions int Quantum time.Duration Tokens map[string]*Token Now func() time.Time }
LeakyBucket implements the token bucket rate limiting algorithm.
func NewLeakyBucket ¶
func NewLeakyBucket(numActions int, quantum time.Duration) *LeakyBucket
NewLeakyBucket returns a new token bucket rate limiter. The rate is formed by `numActions` and `quantum`; the resulting rate is numActions/quantum.
func (*LeakyBucket) Check ¶
func (lb *LeakyBucket) Check(id string) bool
Check returns true if an id has exceeded the rate limit, and false otherwise.
type Queue ¶
type Queue struct { NumberOfActions int Quantum time.Duration Limits map[string]collections.Queue[any] Now func() time.Time }
Queue is a simple implementation of a rate checker.
type RateLimiter ¶
type RateLimiter interface { // Check returns for a given id `true` if that id is _above_ the rate limit, and false otherwise. Check(string) bool }
RateLimiter is a type that can be used as a rate limiter.
type Token ¶
type Token struct { Count float64 // the rate adjusted count; initialize at max*rate, remove rate tokens per call Last time.Time // last is used to calculate the elapsed, and subsequently the rate }
Token is an individual id's work.
type Wait ¶ added in v1.20211204.3
Wait is a type that allows you to throttle actions with sleeps based on a desired rate.
func (Wait) Calculate ¶ added in v1.20211204.3
Calculate takes the observed rate and the desired rate, and returns a quantum to sleep for that adjusts the observed rate to match the desired rate.
If the observed rate is _lower_ than the desired rate, the returned value will be negative and you're free to ignore it.
If the observed rate is _higher_ than the desired rate, a positive duration will be returned which you can pass to a `time.Sleep(...)` or similar.
The wait quantum is derrived from the following algebraic steps (where ? is what we're solving for):
pb/(pq+?) = rb/rq 1/(pq+?) = rb/pb*rq pq+? = (pb*rq)/rb ? = ((pb*rq)/rb) - pq