ratelimit

package
v0.1.80-dev.2 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: AGPL-3.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package ratelimit holds the in-memory, bounded-memory state behind the public surface's abuse protection: a keyed map that evicts its least recently used entry when full, and a token bucket limiter built on it. Nothing here is persisted, logged or shared between processes.

Evicting an entry forgets its state, which can only help a client (a fresh bucket). The bound is what matters: an attacker rotating IPv6 /64s or junk keys cannot grow the map, and an operation costs O(1) under one mutex. Shard the mutex if a profile ever shows it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	// Now is the clock; nil means time.Now (tests set it).
	Now func() time.Time
	// contains filtered or unexported fields
}

Limiter is a per-key token bucket: a key may burst up to the burst size, then gets rate requests per second. Memory is bounded by the key cap given to New.

func New

func New(rate float64, burst, maxKeys int) *Limiter

New returns a limiter allowing rate requests per second per key with bursts of burst, tracking at most maxKeys keys. A rate <= 0 disables it (Allow always says yes).

func (*Limiter) Allow

func (l *Limiter) Allow(key string) (ok bool, retryAfter time.Duration)

Allow takes one token for key. When there is none it returns false and how long until there is one.

func (*Limiter) Keys

func (l *Limiter) Keys() int

Keys is how many keys are tracked.

type Map

type Map[V any] struct {
	// contains filtered or unexported fields
}

Map is a goroutine-safe map from string keys to V holding at most max entries.

func NewMap

func NewMap[V any](max int) *Map[V]

NewMap returns a Map that keeps at most max entries (at least 1).

func (*Map[V]) Delete

func (m *Map[V]) Delete(key string)

Delete forgets key.

func (*Map[V]) Get

func (m *Map[V]) Get(key string) (v V, ok bool)

Get returns the value for key and marks it recently used.

func (*Map[V]) GetOrCreate

func (m *Map[V]) GetOrCreate(key string, mk func() V) V

GetOrCreate returns the value for key, storing mk() first when there is none. The oldest entry is evicted when the map is full.

func (*Map[V]) Len

func (m *Map[V]) Len() int

Len is the number of entries.

Jump to

Keyboard shortcuts

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