ratelimit

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2026 License: MIT Imports: 5 Imported by: 0

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

View Source
const DefaultMaxKeys = 100_000

DefaultMaxKeys bounds the number of keys a TokenBucket tracks per key space. 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 in each key space (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).

Keys are counted per key space: the prefix up to and including the first ':' ("code:", "poll:", "revoke:", ...), so callers filling one space — many interaction code clients, say — cannot exhaust the table for another and lock out polling or revocation.

func New

func New(perSecond float64, burst int) *TokenBucket

New returns a TokenBucket allowing burst actions per key, refilled at perSecond actions per second.

func Per

func Per(n int, interval time.Duration) *TokenBucket

Per returns a TokenBucket allowing n actions per interval per key, all of which may be used at once.

func (*TokenBucket) Allow

func (b *TokenBucket) Allow(_ context.Context, key string) (bool, time.Duration)

Allow implements aauth.Limiter.

Jump to

Keyboard shortcuts

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