ratelimit

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxBuckets is the maximum number of rate limit buckets kept in memory.
	// When exceeded, stale buckets (fully refilled and older than their window) are evicted.
	MaxBuckets = 10000
)

Variables

This section is empty.

Functions

func ParseWindow

func ParseWindow(s string) (time.Duration, error)

ParseWindow converts a window string like "1m", "30s", "1h" to a Duration.

Types

type BucketSnapshot added in v0.9.0

type BucketSnapshot struct {
	Key        string
	Tokens     int
	Max        int
	Window     time.Duration
	LastRefill time.Time
}

BucketSnapshot is a point-in-time copy of one bucket's state, used by the persistence syncer for write-behind and boot restore. It carries the limiter's opaque key ("scope:tenant:agent"); the syncer parses the tenant out of it.

type Limiter

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

Limiter implements a token-bucket rate limiter keyed by scope and agent.

func New

func New() *Limiter

New creates a new rate limiter.

func (*Limiter) Allow

func (l *Limiter) Allow(key string, maxRequests int, window time.Duration) error

Allow checks whether a request identified by key is within the rate limit. maxRequests is the maximum number of requests allowed in the given window. Returns nil if allowed, or an error describing the limit.

func (*Limiter) BucketCount added in v0.5.0

func (l *Limiter) BucketCount() int

BucketCount returns the current number of tracked buckets (for testing).

func (*Limiter) Restore added in v0.9.0

func (l *Limiter) Restore(snaps []BucketSnapshot)

Restore loads buckets from a prior Snapshot (boot hydration). Entries with a matching key are overwritten. Intended to run once, before serving traffic.

func (*Limiter) Snapshot added in v0.9.0

func (l *Limiter) Snapshot() []BucketSnapshot

Snapshot returns a copy of every live bucket. It holds the limiter lock only for the (O(n) struct-copy) duration and is intended for the background persistence syncer — it is never called on the request path, so it cannot affect the proxy's latency budget.

Jump to

Keyboard shortcuts

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