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

ratelimit

package
v1.15.5 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	// GCInterval is the interval at which per-key limiters are garbage
	// collected. Keys not accessed within two GC intervals are reclaimed.
	GCInterval = time.Minute
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter interface {
	AllowN(now time.Time, n int) bool
	WaitN(ctx context.Context, n int) error
	Limit() rate.Limit
	Burst() int
}

Limiter covers both blocking (WaitN) and non-blocking (AllowN) rate limiting. *rate.Limiter satisfies this interface.

type Option

type Option func(*PerKeyLimiter)

Option configures a PerKeyLimiter.

func WithClock

func WithClock(c clock.Clock) Option

WithClock sets the clock used for time-based operations. Useful for testing.

type PerKeyLimiter

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

PerKeyLimiter maintains per-key rate limiters using a two-generation garbage collection pattern. Keys that are not accessed within two GC intervals have their limiters reclaimed.

func NewPerKeyLimiter

func NewPerKeyLimiter(newLimiter func() Limiter, opts ...Option) *PerKeyLimiter

NewPerKeyLimiter creates a new PerKeyLimiter with the given limiter factory and options.

func (*PerKeyLimiter) GetLimiter

func (l *PerKeyLimiter) GetLimiter(key string) Limiter

GetLimiter returns the rate limiter for the given key, creating one if necessary and performing garbage collection as needed.

func (*PerKeyLimiter) Now

func (l *PerKeyLimiter) Now() time.Time

Now returns the current time according to the configured clock.

Jump to

Keyboard shortcuts

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