ratelimit

package
v0.61.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Listener

type Listener struct {
	net.Listener
	RateLimiter *RateLimiter
	Logger      zerolog.Logger
}

Listener is a network listener that enforces rate limiting on all incoming connections.

func NewListener

func NewListener(l net.Listener, rl *RateLimiter, logger zerolog.Logger) *Listener

NewListener creates a new rate-limiting listener.

func (*Listener) Accept

func (l *Listener) Accept() (net.Conn, error)

Accept waits for and returns the next connection to the listener. It checks each connection's source IP against the RateLimiter and closes it if the limit is exceeded.

type Option added in v0.61.0

type Option func(*rateLimiterOptions)

Option is a functional option for configuring a RateLimiter.

func WithRateLimiterCleanupConfig added in v0.61.0

func WithRateLimiterCleanupConfig(cleanupInterval, staleTTL time.Duration) Option

WithRateLimiterCleanupConfig sets the cleanup interval and stale-entry TTL.

func WithRateLimiterContext added in v0.61.0

func WithRateLimiterContext(ctx context.Context) Option

WithRateLimiterContext sets a parent context whose cancellation stops the background cleanup goroutine.

func WithRateLimiterLogger added in v0.61.0

func WithRateLimiterLogger(logger zerolog.Logger) Option

WithRateLimiterLogger sets the logger used by the rate limiter.

func WithRateLimiterSource added in v0.61.0

func WithRateLimiterSource(s RateLimitSource) Option

WithRateLimiterSource sets a dynamic RateLimitSource. When set, per-principal limits from the source take precedence over the static limits.

func WithRateLimiterStaticLimits added in v0.61.0

func WithRateLimiterStaticLimits(rps float64, burst int) Option

WithRateLimiterStaticLimits sets the static requests-per-second and burst values.

type RateLimitSource

type RateLimitSource interface {
	// GetLimit returns the rate limits to apply for a principal.
	// If it returns ok=false, the default limits of the RateLimiter will be used.
	GetLimit(principal string) (requestsPerSecond float64, burst int, ok bool)
}

RateLimitSource defines the interface for determining rate limits.

type RateLimiter

type RateLimiter struct {

	// LimitSource provides dynamic rate limits per principal.
	LimitSource RateLimitSource
	// contains filtered or unexported fields
}

RateLimiter provides per-principal rate limiting. It tracks last-used time for each principal and cleans up stale limiters.

func NewRateLimiter deprecated

func NewRateLimiter(requestsPerSecond float64, burst int, logger zerolog.Logger) *RateLimiter

NewRateLimiter creates a new rate limiter with static limits.

Deprecated: Use NewRateLimiterWithOptions with WithRateLimiterStaticLimits and WithRateLimiterLogger instead.

func NewRateLimiterWithConfig deprecated

func NewRateLimiterWithConfig(requestsPerSecond float64, burst int, cleanupInterval, staleTTL time.Duration, logger zerolog.Logger) *RateLimiter

NewRateLimiterWithConfig creates a new rate limiter with custom configuration.

Deprecated: Use NewRateLimiterWithOptions with WithRateLimiterStaticLimits, WithRateLimiterCleanupConfig, and WithRateLimiterLogger instead.

func NewRateLimiterWithContext deprecated

func NewRateLimiterWithContext(ctx context.Context, requestsPerSecond float64, burst int, logger zerolog.Logger) *RateLimiter

NewRateLimiterWithContext creates a new rate limiter with static limits and a context.

Deprecated: Use NewRateLimiterWithOptions with WithRateLimiterContext, WithRateLimiterStaticLimits, and WithRateLimiterLogger instead.

func NewRateLimiterWithContextAndConfig deprecated

func NewRateLimiterWithContextAndConfig(ctx context.Context, requestsPerSecond float64, burst int, cleanupInterval, staleTTL time.Duration, logger zerolog.Logger) *RateLimiter

NewRateLimiterWithContextAndConfig creates a new rate limiter with custom configuration and a context.

Deprecated: Use NewRateLimiterWithOptions with WithRateLimiterContext, WithRateLimiterStaticLimits, WithRateLimiterCleanupConfig, and WithRateLimiterLogger instead.

func NewRateLimiterWithOptions added in v0.61.0

func NewRateLimiterWithOptions(opts ...Option) *RateLimiter

NewRateLimiterWithOptions creates a RateLimiter using functional options. This is the preferred constructor; the positional constructors are deprecated.

func NewRateLimiterWithSource deprecated

func NewRateLimiterWithSource(source RateLimitSource, logger zerolog.Logger) *RateLimiter

NewRateLimiterWithSource creates a new rate limiter with a custom rate limit source.

Deprecated: Use NewRateLimiterWithOptions with WithRateLimiterSource and WithRateLimiterLogger instead.

func NewRateLimiterWithSourceAndConfig deprecated

func NewRateLimiterWithSourceAndConfig(source RateLimitSource, cleanupInterval, staleTTL time.Duration, logger zerolog.Logger) *RateLimiter

NewRateLimiterWithSourceAndConfig creates a new rate limiter with a custom rate limit source and configuration.

Deprecated: Use NewRateLimiterWithOptions with WithRateLimiterSource, WithRateLimiterCleanupConfig, and WithRateLimiterLogger instead.

func NewRateLimiterWithSourceAndContext deprecated

func NewRateLimiterWithSourceAndContext(ctx context.Context, source RateLimitSource, logger zerolog.Logger) *RateLimiter

NewRateLimiterWithSourceAndContext creates a new rate limiter with a custom rate limit source and a context.

Deprecated: Use NewRateLimiterWithOptions with WithRateLimiterContext, WithRateLimiterSource, and WithRateLimiterLogger instead.

func NewRateLimiterWithSourceContextAndConfig deprecated

func NewRateLimiterWithSourceContextAndConfig(ctx context.Context, source RateLimitSource, cleanupInterval, staleTTL time.Duration, logger zerolog.Logger) *RateLimiter

NewRateLimiterWithSourceContextAndConfig creates a new rate limiter with a custom rate limit source, context, and configuration.

Deprecated: Use NewRateLimiterWithOptions with WithRateLimiterContext, WithRateLimiterSource, WithRateLimiterCleanupConfig, and WithRateLimiterLogger instead.

func (*RateLimiter) Allow

func (rl *RateLimiter) Allow(principal string) bool

Allow checks if a request from the given principal is allowed.

func (*RateLimiter) RetryAfter

func (rl *RateLimiter) RetryAfter(principal string) time.Duration

RetryAfter returns the duration until the next request would be allowed for the given principal. This can be used to set the Retry-After header in HTTP responses. If the principal has no limiter entry (first request), it returns 0. Note: This method uses RLock because it only reads from the limiters map. The Reserve/Cancel calls on the underlying rate.Limiter are thread-safe due to rate.Limiter's internal mutex. This method is typically called immediately after Allow() returns false, so the limiter entry will exist. If rate limits change between calls, the returned duration reflects the current limits at the time of the Reserve() call, which is acceptable for advisory Retry-After headers.

func (*RateLimiter) Stop

func (rl *RateLimiter) Stop()

Stop gracefully stops the background cleanup goroutine. It should be called when the RateLimiter is no longer needed. Stop can be safely called multiple times. Note: When using context-based constructors (NewRateLimiterWithContext, etc.), calling Stop is not necessary as cleanup happens automatically when the context is cancelled. However, calling Stop after context cancellation is safe and will wait for cleanup to complete.

type StaticRateLimitSource

type StaticRateLimitSource struct {
	RequestsPerSecond float64
	Burst             int
}

StaticRateLimitSource is a simple implementation of RateLimitSource that returns fixed limits.

func (*StaticRateLimitSource) GetLimit

func (s *StaticRateLimitSource) GetLimit(principal string) (float64, int, bool)

GetLimit returns the fixed limits.

Jump to

Keyboard shortcuts

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