ratelimit

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateBaseKey

func GenerateBaseKey(ip string, path string) string

GenerateBaseKey creates a base rate limit key without time component This is used for sliding window algorithm which manages time slots internally

func GenerateKey

func GenerateKey(ip string, path string) string

GenerateKey creates a rate limit key from IP address and path

func GenerateKeyWithWindow

func GenerateKeyWithWindow(ip string, path string, window int) string

GenerateKeyWithWindow creates a rate limit key that includes the time window This ensures the key is unique per time window (for fixed window algorithm)

Types

type Limiter

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

Limiter provides rate limiting functionality using secondary storage

func NewLimiter

func NewLimiter(secondary storage.SecondaryStorage) *Limiter

NewLimiter creates a new rate limiter

func (*Limiter) Check

func (l *Limiter) Check(ctx context.Context, key string, window int, max int) (remaining int, allowed bool, err error)

Check checks if a request should be allowed based on rate limits Returns remaining requests and whether the request should be allowed

func (*Limiter) CheckWithAlgorithm

func (l *Limiter) CheckWithAlgorithm(ctx context.Context, key string, window int, max int, algorithm string) (remaining int, allowed bool, err error)

CheckWithAlgorithm checks if a request should be allowed based on rate limits using the specified algorithm Returns remaining requests and whether the request should be allowed

func (*Limiter) GetCount

func (l *Limiter) GetCount(ctx context.Context, key string) (int, error)

GetCount returns the current count for a given key

func (*Limiter) Reset

func (l *Limiter) Reset(ctx context.Context, key string) error

Reset resets the rate limit counter for a given key

Jump to

Keyboard shortcuts

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