ratelimit

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package ratelimit provides the bounded, LRU-evicting per-key token-bucket store shared by the HTTP and gRPC server-side rate limiters. Keeping it in one place means both transports get identical memory-safety guarantees rather than two divergent stores.

Index

Constants

View Source
const DefaultMaxTrackedKeys = 8192

DefaultMaxTrackedKeys bounds the per-key store so an attacker rotating source keys cannot allocate unbounded *rate.Limiter values and exhaust memory.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

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

Store is a bounded, LRU-evicting map of per-key token buckets. It is mutex-guarded and caps the number of tracked keys; when full, the least-recently-used key is evicted. A re-sighted evicted key gets a fresh, full bucket — acceptable because eviction only happens under key churn.

func NewStore

func NewStore(limit rate.Limit, burst, maxKeys int) *Store

NewStore returns a Store handing out buckets of the given rate and burst, tracking at most maxKeys keys (clamped to >= 1).

func (*Store) Len

func (s *Store) Len() int

Len reports the number of tracked keys.

func (*Store) LimiterFor

func (s *Store) LimiterFor(key string) *rate.Limiter

LimiterFor returns the token bucket for key, creating it on first sighting and promoting it to most-recently-used. Evicts the LRU key when over capacity.

Jump to

Keyboard shortcuts

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