limiter

package
v4.17.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUnexpectedScriptResult = errors.New("rateLimiter: unexpected backend result")

Functions

func NewMockClock

func NewMockClock(start time.Time) *mockClock

Types

type Clock

type Clock interface {
	Now() time.Time
	Sleep(d time.Duration)
}

type Decision

type Decision struct {
	Allowed      bool
	RetryAfter   time.Duration
	BlockedUntil time.Time
	Remaining    int64
	ResetAt      time.Time
}

fixed window rate limiter

type FixedWindowBackend

type FixedWindowBackend interface {
	IncrWindow(ctx context.Context, baseKey string, window time.Duration, now time.Time) (count int64, resetAt time.Time, err error)
}

fixed window rate limiter

func NewMemoryStorage

func NewMemoryStorage() FixedWindowBackend

func NewMemoryStorageWithShards

func NewMemoryStorageWithShards(shards int) FixedWindowBackend

func NewRedisStorage

func NewRedisStorage(client *redis.Client) FixedWindowBackend

type FixedWindowConfig

type FixedWindowConfig struct {
	Window    time.Duration
	Limit     int64
	KeyPrefix string
}

fixed window rate limiter

type FixedWindowLimiter

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

func (*FixedWindowLimiter) Allow

func (f *FixedWindowLimiter) Allow(ctx context.Context, key string) (Decision, error)

type MemoryStorage

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

func (*MemoryStorage) IncrWindow

func (m *MemoryStorage) IncrWindow(ctx context.Context, baseKey string, window time.Duration, now time.Time) (int64, time.Time, error)

type RateLimiter

type RateLimiter interface {
	Allow(ctx context.Context, key string) (Decision, error)
}

fixed window rate limiter

func NewFixedWindowLimiter

func NewFixedWindowLimiter(storage FixedWindowBackend, cfg FixedWindowConfig) RateLimiter

type RedisStorage

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

func (*RedisStorage) IncrWindow

func (r *RedisStorage) IncrWindow(ctx context.Context, baseKey string, window time.Duration, now time.Time) (int64, time.Time, error)

Jump to

Keyboard shortcuts

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