ratelimiter

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRateLimitExceeded = errors.New("rate limit exceeded, please try again later")

Functions

This section is empty.

Types

type Options

type Options struct {
	UnauthenticatedRateLimit int `usage:"Rate limit for unauthenticated requests (req/sec)" default:"100"`
	AuthenticatedRateLimit   int `usage:"Rate limit for authenticated non-admin requests (req/sec)" default:"200"`
}

type RateLimiter

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

RateLimiter limits the number of HTTP requests per second a user can make. It tracks limits for unauthenticated and authenticated users separately: - Authenticated requests are tracked by user ID or name. - Unauthenticated requests are tracked by IP address. - Admins are exempt from rate limiting.

func New

func New(opts Options) (*RateLimiter, error)

func (*RateLimiter) ApplyLimit

func (l *RateLimiter) ApplyLimit(u user.Info, rw http.ResponseWriter, req *http.Request) error

ApplyLimit applies the user's rate limit to the request, sets the rate limit headers, and returns a ErrRateLimitExceeded error if the limit has been exceeded. It returns nil if the user is exempt from rate limiting or if the user has not exceeded their limit.

Jump to

Keyboard shortcuts

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