fixedwindow

package
v0.1.24 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLimiter

func NewLimiter(config limiter.Config) (limiter.Limiter, error)

NewLimiter creates a fixed window rate limiter based on the provided configuration

Types

type MemoryLimiter

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

MemoryLimiter implements fixed window rate limiting with in-memory storage

func NewMemoryLimiter

func NewMemoryLimiter(policy *Policy, cleanupInterval time.Duration) *MemoryLimiter

NewMemoryLimiter creates a new in-memory fixed window rate limiter policy: Rate limit policy defining limit and window duration cleanupInterval: How often expired entries are removed (0 to disable, recommended: 5 minutes)

func (*MemoryLimiter) Allow

func (m *MemoryLimiter) Allow(ctx context.Context, key string) (*limiter.Result, error)

Allow checks if a single request is allowed for the given key

func (*MemoryLimiter) AllowN

func (m *MemoryLimiter) AllowN(ctx context.Context, key string, n int64) (*limiter.Result, error)

AllowN checks if N requests are allowed for the given key Atomically consumes N request tokens if allowed

func (*MemoryLimiter) Close

func (m *MemoryLimiter) Close() error

Close stops the cleanup goroutine and releases resources Safe to call multiple times

func (*MemoryLimiter) GetAvailable added in v0.1.22

func (m *MemoryLimiter) GetAvailable(ctx context.Context, key string) (int64, error)

GetAvailable returns the available tokens for the given key without consuming

func (*MemoryLimiter) WithClock

func (m *MemoryLimiter) WithClock(clock limiter.Clock) *MemoryLimiter

WithClock sets a custom clock (for testing)

type MultiLimiter

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

MultiLimiter supports multiple concurrent rate limit policies It checks all limiters and returns the most restrictive result

func NewMultiLimiter

func NewMultiLimiter(limiters ...limiter.Limiter) *MultiLimiter

NewMultiLimiter creates a limiter that enforces multiple policies Each policy is checked independently, and the most restrictive result is returned Example: Combine a short-term (10/second) and long-term (1000/hour) rate limit

func (*MultiLimiter) Allow

func (m *MultiLimiter) Allow(ctx context.Context, key string) (*limiter.Result, error)

Allow checks if a single request is allowed against all policies Returns the most restrictive result (fail-fast on first denial)

func (*MultiLimiter) AllowN

func (m *MultiLimiter) AllowN(ctx context.Context, key string, n int64) (*limiter.Result, error)

AllowN checks if N requests are allowed against all policies Returns the most restrictive result (fail-fast on first denial)

func (*MultiLimiter) Close

func (m *MultiLimiter) Close() error

Close closes all limiters Safe to call multiple times

func (*MultiLimiter) GetAvailable added in v0.1.22

func (m *MultiLimiter) GetAvailable(ctx context.Context, key string) (int64, error)

GetAvailable returns the minimum available tokens across all policies

type Policy

type Policy struct {
	// Limit is the maximum number of requests allowed per window
	Limit int64

	// Duration is the window duration (e.g., 1 second, 1 minute, 1 hour)
	Duration time.Duration
}

Policy defines a fixed window rate limit policy Fixed window divides time into fixed-duration intervals and counts requests per interval

func NewPolicy

func NewPolicy(limit int64, duration time.Duration) *Policy

NewPolicy creates a new fixed window rate limit policy limit: maximum number of requests allowed in the window duration: time window duration

func PerDay

func PerDay(limit int64) *Policy

PerDay creates a rate limit policy for requests per day

func PerHour

func PerHour(limit int64) *Policy

PerHour creates a rate limit policy for requests per hour

func PerMinute

func PerMinute(limit int64) *Policy

PerMinute creates a rate limit policy for requests per minute

func PerSecond

func PerSecond(limit int64) *Policy

PerSecond creates a rate limit policy for requests per second

func (*Policy) WindowEnd

func (p *Policy) WindowEnd(now time.Time) time.Time

WindowEnd returns the end time of the current window for the given timestamp

func (*Policy) WindowStart

func (p *Policy) WindowStart(now time.Time) time.Time

WindowStart returns the start time of the current window for the given timestamp Windows are aligned to Unix epoch (truncated to duration boundary)

type RedisLimiter

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

RedisLimiter implements fixed window rate limiting with Redis backend

func NewRedisLimiter

func NewRedisLimiter(client redis.UniversalClient, policy *Policy, keyPrefix string) *RedisLimiter

NewRedisLimiter creates a new Redis-backed fixed window rate limiter client: Redis client for storage policy: Rate limit policy defining limit and window duration keyPrefix: Prefix for all Redis keys (e.g., "ratelimit:v1:")

func (*RedisLimiter) Allow

func (r *RedisLimiter) Allow(ctx context.Context, key string) (*limiter.Result, error)

Allow checks if a single request is allowed for the given key

func (*RedisLimiter) AllowN

func (r *RedisLimiter) AllowN(ctx context.Context, key string, n int64) (*limiter.Result, error)

AllowN checks if N requests are allowed for the given key Uses atomic INCRBY in Redis - no Lua script needed

func (*RedisLimiter) Close

func (r *RedisLimiter) Close() error

Close releases resources (no-op for Redis as connections are managed externally) Safe to call multiple times

func (*RedisLimiter) GetAvailable added in v0.1.22

func (r *RedisLimiter) GetAvailable(ctx context.Context, key string) (int64, error)

GetAvailable returns the available tokens for the given key without consuming

func (*RedisLimiter) WithClock

func (r *RedisLimiter) WithClock(clock limiter.Clock) *RedisLimiter

WithClock sets a custom clock (for testing)

Jump to

Keyboard shortcuts

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