ratelimiter

package
v1.49.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 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

	// based on an avg ~4,000 unique IPs per minute
	// https://log.gprd.gitlab.net/app/lens#/edit/f7110d00-2013-11ec-8c8e-ed83b5469915?_g=h@e78830b
	DefaultSourceIPCacheSize = 5000
)

Variables

This section is empty.

Functions

This section is empty.

Types

type KeyFunc added in v1.49.0

type KeyFunc func(*http.Request) string

KeyFunc returns unique identifier for the subject of rate limit(e.g. client IP or domain)

type Option

type Option func(*RateLimiter)

Option function to configure a RateLimiter

func WithBlockedCountMetric added in v1.49.0

func WithBlockedCountMetric(m *prometheus.GaugeVec) Option

WithBlockedCountMetric configures metric reporting how many requests were blocked

func WithBurstSize added in v1.49.0

func WithBurstSize(burst int) Option

WithBurstSize configures burst per key for the RateLimiter

func WithCacheMaxSize added in v1.49.0

func WithCacheMaxSize(size int64) Option

WithCacheMaxSize configures cache size for ratelimiter

func WithCachedEntriesMetric added in v1.49.0

func WithCachedEntriesMetric(m *prometheus.GaugeVec) Option

WithCachedEntriesMetric configures metric reporting how many keys are currently stored in the rate-limiter cache

func WithCachedRequestsMetric added in v1.49.0

func WithCachedRequestsMetric(m *prometheus.CounterVec) Option

WithCachedRequestsMetric configures metric for how many times we ask key cache

func WithLimitPerSecond added in v1.49.0

func WithLimitPerSecond(limit float64) Option

WithLimitPerSecond allows configuring limit per second for RateLimiter

func WithNow

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

WithNow replaces the RateLimiter now function

type RateLimiter

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

RateLimiter holds an LRU cache of elements to be rate limited. 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(name string, opts ...Option) *RateLimiter

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

func (*RateLimiter) Middleware added in v1.49.0

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

Middleware returns middleware for rate-limiting clients

func (*RateLimiter) RequestAllowed added in v1.49.0

func (rl *RateLimiter) RequestAllowed(r *http.Request) bool

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

Jump to

Keyboard shortcuts

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