Documentation
¶
Overview ¶
Package ratelimit provides an in-memory token-bucket implementation of aauth.Limiter for single-instance deployments. State is per process: a deployment with several instances needs a shared limiter instead.
Index ¶
Constants ¶
const DefaultMaxKeys = 100_000
DefaultMaxKeys bounds the number of keys a TokenBucket tracks. Keys are created by callers, so the table must be bounded.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TokenBucket ¶
type TokenBucket struct {
// Rate is the refill rate in actions per second; Burst the bucket
// size. Both must be positive.
Rate float64
Burst int
// MaxKeys bounds tracked keys (default DefaultMaxKeys).
MaxKeys int
// Now returns the current time; nil means time.Now.
Now func() time.Time
// contains filtered or unexported fields
}
TokenBucket is an aauth.Limiter that allows Burst actions per key at once and refills at Rate actions per second. Idle keys whose bucket has refilled are dropped; when MaxKeys is reached, refilled keys are pruned and, if none can be, the request is refused (fail closed).
func New ¶
func New(perSecond float64, burst int) *TokenBucket
New returns a TokenBucket allowing burst actions per key, refilled at perSecond actions per second.