ratelimit

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	IncrExpire(ctx context.Context, key string, value int64, expiration time.Duration) (int64, error)
	WindowAdd(ctx context.Context, key string, score int64, member string, expiration time.Duration) error
	WindowRemove(ctx context.Context, key string, cutoff int64) error
	WindowCount(ctx context.Context, key string, min, max int64) (int, error)
	WindowMembers(ctx context.Context, key string, start, stop int64) ([]string, error)
	TTL(ctx context.Context, key string) (time.Duration, error)
	Close() error
}

func NewMemoryBackend

func NewMemoryBackend() Backend

func NewMemoryBackendWithCleanup

func NewMemoryBackendWithCleanup(ttl, interval time.Duration) Backend

NewMemoryBackendWithCleanup overrides the stale-entry eviction thresholds, primarily for tests. ttl is how long an idle entry is retained before being purged; interval is how often the background sweep runs (lazily, on access).

func NewRedisBackend

func NewRedisBackend(client *redis.Client) Backend

type Option

type Option func(*Service)

func WithCleanup

func WithCleanup(ttl, interval time.Duration) Option

type Service

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

func NewWithBackend

func NewWithBackend(log *logger.Logger, backend Backend, opts ...Option) *Service

func NewWithMemory

func NewWithMemory(log *logger.Logger, opts ...Option) *Service

func NewWithRedisClient

func NewWithRedisClient(log *logger.Logger, client *redis.Client, opts ...Option) *Service

func (*Service) AddToWindow

func (s *Service) AddToWindow(ctx context.Context, key string, timestamp int64, member string, expiration time.Duration) error

func (*Service) Allow

func (s *Service) Allow(key string, rate float64, burst int) bool

func (*Service) CountInWindow

func (s *Service) CountInWindow(ctx context.Context, key string, min, max int64) (int, error)

func (*Service) GetTTL

func (s *Service) GetTTL(ctx context.Context, key string) (time.Duration, error)

func (*Service) GetWindowEntries

func (s *Service) GetWindowEntries(ctx context.Context, key string, start, stop int) ([]string, error)

func (*Service) Increment

func (s *Service) Increment(ctx context.Context, key string, value int64, expiration time.Duration) (int64, error)

func (*Service) LocalBucketCount

func (s *Service) LocalBucketCount() int

func (*Service) RemoveFromWindow

func (s *Service) RemoveFromWindow(ctx context.Context, key string, cutoff int64) error

func (*Service) Start

func (s *Service) Start() error

func (*Service) Stop

func (s *Service) Stop() error

Jump to

Keyboard shortcuts

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