ratelimiter

package
v1.47.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultSourceIPLimitPerSecond is the limit per second that rate.Limiter
	// needs to generate tokens every second.
	// The default value is 20 requests per second.
	DefaultSourceIPLimitPerSecond = 20.0
	// DefaultSourceIPBurstSize is the maximum burst allowed per rate limiter.
	// E.g. The first 100 requests within 1s will succeed, but the 101st will fail.
	DefaultSourceIPBurstSize = 100
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*RateLimiter)

Option function to configure a RateLimiter

func WithNow

func WithNow(now func() time.Time) Option

WithNow replaces the RateLimiter now function

func WithSourceIPBurstSize

func WithSourceIPBurstSize(burst int) Option

WithSourceIPBurstSize configures burst per source IP for the RateLimiter

func WithSourceIPLimitPerSecond

func WithSourceIPLimitPerSecond(limit float64) Option

WithSourceIPLimitPerSecond allows configuring per source IP limit per second for RateLimiter

type RateLimiter

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

RateLimiter holds an LRU cache of elements to be rate limited. Currently, it supports a sourceIPCache and each item returns a rate.Limiter. It uses "golang.org/x/time/rate" as its Token Bucket rate limiter per source IP entry. See example https://www.fatalerrors.org/a/design-and-implementation-of-time-rate-limiter-for-golang-standard-library.html It also holds a now function that can be mocked in unit tests.

func New

func New(blockCountMetric, cachedEntriesMetric *prometheus.GaugeVec, cacheRequestsMetric *prometheus.CounterVec, opts ...Option) *RateLimiter

New creates a new RateLimiter with default values that can be configured via Option functions

func (*RateLimiter) SourceIPAllowed

func (rl *RateLimiter) SourceIPAllowed(sourceIP string) bool

SourceIPAllowed checks that the real remote IP address is allowed to perform an operation

func (*RateLimiter) SourceIPLimiter

func (rl *RateLimiter) SourceIPLimiter(handler http.Handler) http.Handler

SourceIPLimiter returns middleware for rate-limiting clients based on their IP

Jump to

Keyboard shortcuts

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