ratelimit

package
v1.7.3 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRateLimitExceeded is returned when rate limit is exceeded
	ErrRateLimitExceeded = errors.New("rate limit exceeded")
)

Functions

This section is empty.

Types

type APIKeyExtractor

type APIKeyExtractor struct{}

APIKeyExtractor extracts API key from header or query parameter

func (*APIKeyExtractor) Extract

func (e *APIKeyExtractor) Extract(ctx *zoox.Context, req *http.Request) (string, error)

type Algorithm

type Algorithm interface {
	// Allow checks if a request is allowed
	// Returns:
	//   - allowed: whether the request is allowed
	//   - remaining: remaining requests/tokens
	//   - resetTime: when the rate limit will reset
	//   - err: any error that occurred
	Allow(ctx context.Context, storage Storage, key string, limit int64, window time.Duration, burst int64) (allowed bool, remaining int64, resetTime time.Time, err error)
}

Algorithm defines the interface for rate limiting algorithms

type AlgorithmFactory

type AlgorithmFactory struct{}

AlgorithmFactory creates algorithm instances

func (*AlgorithmFactory) NewAlgorithm

func (f *AlgorithmFactory) NewAlgorithm(algorithmType string) Algorithm

NewAlgorithm creates an algorithm instance based on algorithm type

type ClientIDExtractor

type ClientIDExtractor struct{}

ClientIDExtractor extracts a client identifier from X-Client-ID or query client_id.

func (*ClientIDExtractor) Extract

func (e *ClientIDExtractor) Extract(ctx *zoox.Context, req *http.Request) (string, error)

type ExtractorFactory

type ExtractorFactory struct{}

ExtractorFactory creates key extractors based on configuration

func (*ExtractorFactory) NewExtractor

func (f *ExtractorFactory) NewExtractor(keyType, keyHeader string) KeyExtractor

NewExtractor creates a key extractor based on key type

type FixedWindowAlgorithm

type FixedWindowAlgorithm struct{}

FixedWindowAlgorithm implements fixed window rate limiting

func (*FixedWindowAlgorithm) Allow

func (a *FixedWindowAlgorithm) Allow(ctx context.Context, storage Storage, key string, limit int64, window time.Duration, burst int64) (bool, int64, time.Time, error)

Allow checks if a request is allowed using fixed window algorithm

type HeaderExtractor

type HeaderExtractor struct {
	HeaderName string
}

HeaderExtractor extracts value from a custom header

func (*HeaderExtractor) Extract

func (e *HeaderExtractor) Extract(ctx *zoox.Context, req *http.Request) (string, error)

type IPExtractor

type IPExtractor struct{}

IPExtractor extracts the client IP address

func (*IPExtractor) Extract

func (e *IPExtractor) Extract(ctx *zoox.Context, req *http.Request) (string, error)

type KeyExtractor

type KeyExtractor interface {
	Extract(ctx *zoox.Context, req *http.Request) (string, error)
}

KeyExtractor extracts a key from the request for rate limiting

type LeakyBucketAlgorithm

type LeakyBucketAlgorithm struct{}

LeakyBucketAlgorithm implements leaky bucket rate limiting backed by Storage (Application.Cache).

func (*LeakyBucketAlgorithm) Allow

func (a *LeakyBucketAlgorithm) Allow(ctx context.Context, storage Storage, key string, limit int64, window time.Duration, burst int64) (bool, int64, time.Time, error)

Allow checks if a request is allowed using leaky bucket logic.

type RateLimit

type RateLimit struct {
	plugin.Plugin
	// contains filtered or unexported fields
}

RateLimit implements rate limiting plugin

func New

func New() *RateLimit

New creates a new rate limit plugin

func (*RateLimit) OnRequest

func (r *RateLimit) OnRequest(ctx *zoox.Context, req *http.Request) error

OnRequest checks rate limit before forwarding request

func (*RateLimit) OnResponse

func (r *RateLimit) OnResponse(ctx *zoox.Context, res *http.Response) error

OnResponse is called after receiving response

func (*RateLimit) Prepare

func (r *RateLimit) Prepare(app *zoox.Application, cfg *config.Config) error

Prepare initializes the rate limit plugin

type Storage

type Storage interface {
	Allow(ctx context.Context, key string, limit int64, window time.Duration) (allowed bool, remaining int64, resetTime time.Time, err error)
	Reset(ctx context.Context, key string) error
	Close() error

	// LoadState / SaveState persist token-bucket and leaky-bucket state in Application.Cache().
	// kind is "tb" or "lb"; key is the same logical client key used by extractors.
	LoadState(ctx context.Context, kind, key string, dest interface{}) error
	SaveState(ctx context.Context, kind, key string, src interface{}, ttl time.Duration) error
}

Storage is the counter backend for rate-limit algorithms.

type TokenBucketAlgorithm

type TokenBucketAlgorithm struct{}

TokenBucketAlgorithm implements token bucket rate limiting backed by Storage (Application.Cache).

func (*TokenBucketAlgorithm) Allow

func (a *TokenBucketAlgorithm) Allow(ctx context.Context, storage Storage, key string, limit int64, window time.Duration, burst int64) (bool, int64, time.Time, error)

Allow checks if a request is allowed using token bucket logic with refill rate limit/window.

type UserExtractor

type UserExtractor struct{}

UserExtractor extracts user ID from JWT token or session

func (*UserExtractor) Extract

func (e *UserExtractor) Extract(ctx *zoox.Context, req *http.Request) (string, error)

Jump to

Keyboard shortcuts

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