ratelimiter

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrInvalidRateLimit       = utils.Error("rate limit must be positive")
	ErrInvalidBurst           = utils.Error("burst must be positive")
	ErrInvalidTTL             = utils.Error("TTL must be positive")
	ErrInvalidCleanupInterval = utils.Error("cleanup interval must be positive")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	RateLimit       rate.Limit `json:"rateLimit"`
	Burst           int        `json:"burst"`
	TTL             int        `json:"ttl"`             // seconds
	CleanupInterval int        `json:"cleanupInterval"` // seconds
}

func NewConfig

func NewConfig() *Config

func (*Config) Validate

func (c *Config) Validate() error

Validate checks if config values are valid

type RateLimiter

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

RateLimiter manages rate limiters with expiration

func NewRateLimiter

func NewRateLimiter(cfg *Config) (*RateLimiter, error)

NewRateLimiter creates a rate limiter with TTL and cleanup

func (*RateLimiter) Allow

func (r *RateLimiter) Allow(key string) bool

Allow checks if the key can perform an action now

func (*RateLimiter) GetLimiter

func (r *RateLimiter) GetLimiter(key string) *rate.Limiter

GetLimiter returns or creates a rate limiter for a key

func (*RateLimiter) Shutdown

func (r *RateLimiter) Shutdown()

Shutdown stops the background cleanup goroutine (safe to call multiple times)

func (*RateLimiter) ShutdownWithContext

func (r *RateLimiter) ShutdownWithContext(ctx context.Context) error

ShutdownWithContext stops cleanup goroutine with timeout

func (*RateLimiter) Start

func (r *RateLimiter) Start()

Start begins the cleanup goroutine (safe to call multiple times)

Jump to

Keyboard shortcuts

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