ratelimiter

package
v0.0.0-...-6a15357 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2023 License: MPL-2.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 Config

type Config struct {
	IPRateLimitWindow          time.Duration
	AccountRateLimitWindow     time.Duration
	BackoffRateLimitWindow     time.Duration
	BackoffRateLimitMultiplier int
}

type Impl

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

Using sync.Map for safe concurrent access.

func (*Impl) Middleware

func (rl *Impl) Middleware(next http.Handler) http.Handler

func (*Impl) ShouldRateLimit

func (rl *Impl) ShouldRateLimit(r *http.Request) (bool, string, error)

shouldRateLimit returns true if the request should be rate limited, and the reason why. It returns false if the request should not be rate limited.

You should generally use it like so:

shouldRateLimit, reason := rateLimiter.shouldRateLimit(r)
if (shouldRateLimit) {
	w.WriteHeader(http.StatusTooManyRequests)
	w.Write([]byte("Rate limit exceeded. reason:" + reason))
	return
}
next.ServeHTTP(w, r)

The rate limiting logic is different per API since this server is for educational purposes.

type RateLimiter

type RateLimiter interface {
	Middleware(next http.Handler) http.Handler
	// This is an internal func, exporting just to make testing easy.
	ShouldRateLimit(r *http.Request) (bool, string, error)
}

func NewRateLimiter

func NewRateLimiter(
	logger zerolog.Logger,
	config Config,
) RateLimiter

Jump to

Keyboard shortcuts

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