ratelimit

package
v1.30.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ratelimit provides rate limiting functionality to control the rate at which requests are processed over time.

The package implements a generic RequestProcessor that can be used with both HTTP and gRPC requests to enforce rate limits with optional backlog queuing. When rate limits are exceeded, requests can be queued in a backlog with configurable timeout, or rejected immediately if the backlog is full.

Key features:

  • Token bucket and sliding window rate limiting algorithms
  • Configurable rate limits per key or globally
  • Optional backlog queuing with timeout and retry-after headers
  • LRU-based key management for memory efficiency
  • Integration with external rate limiters via Limiter interface

Index

Constants

View Source
const DefaultRateLimitBacklogTimeout = time.Second * 5

DefaultRateLimitBacklogTimeout determines the default timeout for backlog processing.

Variables

This section is empty.

Functions

This section is empty.

Types

type BacklogParams

type BacklogParams struct {
	MaxKeys int
	Limit   int
	Timeout time.Duration
}

BacklogParams defines parameters for the backlog processing.

type LeakyBucketLimiter

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

LeakyBucketLimiter implements GCRA (Generic Cell Rate Algorithm). It's a leaky bucket variant algorithm. More details and good explanation of this alg is provided here: https://brandur.org/rate-limiting#gcra.

func NewLeakyBucketLimiter

func NewLeakyBucketLimiter(maxRate Rate, maxBurst, maxKeys int) (*LeakyBucketLimiter, error)

NewLeakyBucketLimiter creates a new leaky bucket rate limiter.

func (*LeakyBucketLimiter) Allow

func (l *LeakyBucketLimiter) Allow(ctx context.Context, key string) (allow bool, retryAfter time.Duration, err error)

Allow checks if the request should be allowed based on the rate limit.

type Limiter

type Limiter interface {
	Allow(ctx context.Context, key string) (allow bool, retryAfter time.Duration, err error)
}

Limiter interface defines the rate limiting contract.

type Params

type Params struct {
	Key                 string
	RequestBacklogged   bool
	EstimatedRetryAfter time.Duration
}

Params contains common data that relates to the rate limiting procedure.

type Rate

type Rate struct {
	Count    int
	Duration time.Duration
}

Rate describes the frequency of requests.

type RequestHandler

type RequestHandler interface {
	// GetContext returns the request context.
	GetContext() context.Context

	// GetKey extracts the rate limiting key from the request.
	// Returns key, bypass (whether to bypass rate limiting), and error.
	GetKey() (string, bool, error)

	// Execute processes the actual request.
	Execute() error

	// OnReject handles request rejection when rate limit is exceeded.
	OnReject(params Params) error

	// OnError handles errors that occur during rate limiting.
	OnError(params Params, err error) error
}

RequestHandler abstracts the common operations for both HTTP and gRPC requests.

type RequestProcessor

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

RequestProcessor handles the common rate limiting logic for any request type.

func NewRequestProcessor

func NewRequestProcessor(limiter Limiter, backlogParams BacklogParams) (*RequestProcessor, error)

NewRequestProcessor creates a new generic request processor.

func (*RequestProcessor) ProcessRequest

func (p *RequestProcessor) ProcessRequest(rh RequestHandler) error

ProcessRequest contains the shared rate limiting logic.

type SlidingWindowLimiter

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

SlidingWindowLimiter implements sliding window rate limiting algorithm.

func NewSlidingWindowLimiter

func NewSlidingWindowLimiter(maxRate Rate, maxKeys int) (*SlidingWindowLimiter, error)

NewSlidingWindowLimiter creates a new sliding window rate limiter.

func (*SlidingWindowLimiter) Allow

func (l *SlidingWindowLimiter) Allow(_ context.Context, key string) (allow bool, retryAfter time.Duration, err error)

Allow checks if the request should be allowed based on the rate limit.

Jump to

Keyboard shortcuts

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