ratelimit

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Init added in v0.9.0

func Init() error

Init registers the rate_limit middleware.

func PutAllowResult added in v0.4.0

func PutAllowResult(result *AllowResult)

PutAllowResult returns an AllowResult to the pool.

Types

type AllowResult

type AllowResult struct {
	ResetTime time.Time
	Limit     uint64
	Remaining uint64
	Allow     bool
}

AllowResult contains the result of a rate limit check.

func GetAllowResult added in v0.4.0

func GetAllowResult() *AllowResult

GetAllowResult gets an AllowResult from the pool.

type Limiter

type Limiter interface {
	Allow(ctx context.Context, key string) *AllowResult
}

Limiter defines the interface for different rate limiting strategies.

type LocalLimiter

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

LocalLimiter implements rate limiting using local memory.

func NewLocalLimiter

func NewLocalLimiter(options Options) *LocalLimiter

NewLocalLimiter creates a new LocalLimiter instance.

func (*LocalLimiter) Allow

func (l *LocalLimiter) Allow(_ context.Context, key string) *AllowResult

Allow checks if the given key is allowed to proceed based on rate limits.

type Options

type Options struct {
	Strategy                 StrategyMode  `mapstructure:"strategy"`
	LimitBy                  string        `mapstructure:"limit_by"`
	HeaderLimit              string        `mapstructure:"header_limit"`
	HeaderRemaining          string        `mapstructure:"header_remaining"`
	HeaderReset              string        `mapstructure:"header_reset"`
	RejectedHTTPContentType  string        `mapstructure:"rejected_http_content_type"`
	RejectedHTTPResponseBody string        `mapstructure:"rejected_http_response_body"`
	RedisID                  string        `mapstructure:"redis_id"`
	Limit                    uint64        `mapstructure:"limit"`
	WindowSize               time.Duration `mapstructure:"window_size"`
	RejectedHTTPStatusCode   int           `mapstructure:"rejected_http_status_code"`
}

Options defines the configuration for the rate limiting middleware.

type RateLimitingMiddleware

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

RateLimitingMiddleware is a middleware that performs rate limiting.

func NewMiddleware

func NewMiddleware(options Options) (*RateLimitingMiddleware, error)

NewMiddleware creates a new RateLimitingMiddleware instance.

func (*RateLimitingMiddleware) ServeHTTP

type RedisLimiter

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

RedisLimiter implements rate limiting using Redis.

func NewRedisLimiter

func NewRedisLimiter(client redis.UniversalClient, options Options) *RedisLimiter

NewRedisLimiter creates a new RedisLimiter instance.

func (*RedisLimiter) Allow

func (l *RedisLimiter) Allow(ctx context.Context, key string) *AllowResult

Allow checks if the given key is allowed to proceed based on rate limits in Redis.

type StrategyMode

type StrategyMode string

StrategyMode defines the rate limiting strategy to use.

const (
	// Local strategy use local memory for rate limiting.
	Local StrategyMode = "local"
	// Redis strategy use redis for rate limiting.
	Redis StrategyMode = "redis"
	// LocalAsyncRedis strategy use local memory for rate limiting and sync to redis asynchronously.
	LocalAsyncRedis StrategyMode = "local_async_redis" // #nosec G101
)

Jump to

Keyboard shortcuts

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