Help us shape the future of Go! Take the 2026 Go Developer Survey

ratelimit

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ratelimit provides rate limiting for AuthSome endpoints.

Index

Constants

View Source
const DefaultMaxKeys = 100_000

DefaultMaxKeys bounds the memory limiter. A flood of distinct keys (one per spoofed address, say) would otherwise grow the map without limit; at the bound, idle windows are dropped before a new key is admitted.

Variables

This section is empty.

Functions

This section is empty.

Types

type KVLimiter

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

KVLimiter is a fixed-window limiter on the shared KV store, so a limit holds across every replica that shares the database.

Each key gets one counter per window, keyed by the window's start, and the counter expires with the window. A fixed window lets up to twice the limit through around a boundary; the limits it guards are small, and one increment per request is what keeps the shared store cheap.

func NewKVLimiter

func NewKVLimiter(kv store.KV) *KVLimiter

NewKVLimiter builds a limiter on kv.

func (*KVLimiter) Allow

func (l *KVLimiter) Allow(ctx context.Context, key string, limit int, dur time.Duration) (bool, error)

Allow counts the request against the current window and reports whether it fits under limit.

func (*KVLimiter) Remaining

func (l *KVLimiter) Remaining(ctx context.Context, key string, limit int, dur time.Duration) (int, error)

Remaining reports how many requests are left in the current window without counting one.

type Limiter

type Limiter interface {
	// Allow checks if a request identified by key is allowed within the given
	// limit and window. Returns true if allowed, false if rate limited.
	Allow(ctx context.Context, key string, limit int, window time.Duration) (bool, error)

	// Remaining returns the number of requests remaining for the given key.
	Remaining(ctx context.Context, key string, limit int, window time.Duration) (int, error)
}

Limiter is the interface for rate limiting.

type MemoryLimiter

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

MemoryLimiter is an in-memory sliding window rate limiter.

func NewMemoryLimiter

func NewMemoryLimiter() *MemoryLimiter

NewMemoryLimiter creates a new in-memory rate limiter.

func NewMemoryLimiterWithLimit

func NewMemoryLimiterWithLimit(maxKeys int) *MemoryLimiter

NewMemoryLimiterWithLimit creates a memory limiter bounded to maxKeys.

func (*MemoryLimiter) Allow

func (l *MemoryLimiter) Allow(_ context.Context, key string, limit int, dur time.Duration) (bool, error)

Allow checks if a request is allowed under the sliding window.

func (*MemoryLimiter) Len

func (l *MemoryLimiter) Len() int

Len reports how many keys the limiter tracks.

func (*MemoryLimiter) Remaining

func (l *MemoryLimiter) Remaining(_ context.Context, key string, limit int, dur time.Duration) (int, error)

Remaining returns how many requests are left in the current window.

type NoopLimiter

type NoopLimiter struct{}

NoopLimiter is a rate limiter that always allows requests.

func NewNoopLimiter

func NewNoopLimiter() *NoopLimiter

NewNoopLimiter creates a no-op rate limiter.

func (*NoopLimiter) Allow

Allow always returns true.

func (*NoopLimiter) Remaining

func (*NoopLimiter) Remaining(_ context.Context, _ string, limit int, _ time.Duration) (int, error)

Remaining always returns the full limit.

Jump to

Keyboard shortcuts

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