ratelimit

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2025 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdaptiveConfig

type AdaptiveConfig struct {
	BaseConfig         RateLimitConfig
	MinLoadFactor      float64       // Minimum multiplier (e.g., 0.5 = 50% of base)
	MaxLoadFactor      float64       // Maximum multiplier (e.g., 2.0 = 200% of base)
	AdjustmentInterval time.Duration // How often to adjust
	TargetSuccessRate  float64       // Target success rate (e.g., 0.95)
}

AdaptiveConfig configures adaptive rate limiting.

func DefaultAdaptiveConfig

func DefaultAdaptiveConfig() AdaptiveConfig

DefaultAdaptiveConfig returns default adaptive configuration.

type RateLimit

type RateLimit struct {
	Requests int           // Number of requests
	Window   time.Duration // Time window
	Burst    int           // Burst allowance
}

RateLimit defines a rate limit rule.

type RateLimitConfig

type RateLimitConfig struct {
	// Per-user limits
	MessagesPerSecond int
	MessagesPerMinute int
	BurstSize         int

	// Per-room limits
	RoomMessagesPerSecond int

	// Connection limits
	ConnectionsPerUser int
	ConnectionsPerIP   int

	// Action-specific limits
	ActionLimits map[string]RateLimit
}

RateLimitConfig configures rate limiting.

func DefaultRateLimitConfig

func DefaultRateLimitConfig() RateLimitConfig

DefaultRateLimitConfig returns default configuration.

type RateLimitStatus

type RateLimitStatus struct {
	Allowed   bool
	Remaining int
	Limit     int
	ResetAt   time.Time
	RetryIn   time.Duration
}

RateLimitStatus represents current rate limit state.

func (*RateLimitStatus) Format

func (rls *RateLimitStatus) Format() string

Format formats rate limit status.

type RateLimiter

type RateLimiter interface {
	// Allow checks if action is allowed for key
	Allow(ctx context.Context, key string, action string) (bool, error)

	// AllowN checks if N actions are allowed
	AllowN(ctx context.Context, key string, action string, n int) (bool, error)

	// GetStatus returns current rate limit status
	GetStatus(ctx context.Context, key string, action string) (*RateLimitStatus, error)

	// Reset resets rate limit for key
	Reset(ctx context.Context, key string, action string) error
}

RateLimiter provides rate limiting functionality.

func NewAdaptiveRateLimiter

func NewAdaptiveRateLimiter(config AdaptiveConfig, underlying RateLimiter) RateLimiter

NewAdaptiveRateLimiter creates an adaptive rate limiter.

func NewSlidingWindow

func NewSlidingWindow(config RateLimitConfig, store Store) RateLimiter

NewSlidingWindow creates a sliding window rate limiter.

func NewTokenBucket

func NewTokenBucket(config RateLimitConfig, store Store) RateLimiter

NewTokenBucket creates a token bucket rate limiter.

type Store

type Store interface {
	// Get retrieves rate limit data
	Get(ctx context.Context, key string) (*StoreData, error)

	// Set stores rate limit data
	Set(ctx context.Context, key string, data *StoreData, ttl time.Duration) error

	// Increment atomically increments counter
	Increment(ctx context.Context, key string, window time.Duration) (int64, error)

	// Delete removes rate limit data
	Delete(ctx context.Context, key string) error
}

Store provides persistence for rate limit data.

func NewLocalStore

func NewLocalStore() Store

NewLocalStore creates an in-memory store.

type StoreData

type StoreData struct {
	Tokens      int
	LastRefill  time.Time
	Count       int64
	WindowStart time.Time
}

StoreData represents stored rate limit data.

Jump to

Keyboard shortcuts

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