ratelimit

package
v1.6.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

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

Limiter manages rate limiting state and cleanup for multiple keys. It uses a token bucket algorithm to enforce per-key rate limits. Limiter is safe for concurrent use.

func NewLimiter

func NewLimiter(requestsPerMinute int, opts ...Option) *Limiter

NewLimiter creates a new rate limiter. requestsPerMinute: number of requests allowed per minute per key (e.g., 1000 = ~16.67 req/sec)

The limiter: - Uses token bucket algorithm allowing bursts up to the full minute limit - Periodically cleans up limiters for keys inactive for more than staleAge - Is safe for concurrent use

Important: Call Stop() when done to prevent goroutine leaks.

func (*Limiter) Allow

func (l *Limiter) Allow(key string) bool

Allow checks if a request with the given key is allowed under the rate limit. Returns true if allowed, false if rate limit exceeded. key is typically an IP address, user ID, API key, etc.

func (*Limiter) Stop

func (l *Limiter) Stop() error

Stop gracefully stops the cleanup goroutine and releases resources. It is safe to call Stop multiple times.

type Option

type Option func(*Limiter)

Option is a functional option for configuring a Limiter

func WithCleanupInterval

func WithCleanupInterval(interval time.Duration) Option

WithCleanupInterval sets how often to clean up stale limiters. Default: 10 minutes

func WithStaleAge

func WithStaleAge(age time.Duration) Option

WithStaleAge sets how long a limiter can be inactive before being removed. Default: 1 hour

Jump to

Keyboard shortcuts

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