Documentation
¶
Index ¶
Constants ¶
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 WithSourceIPBurstSize ¶
WithSourceIPBurstSize configures burst per source IP for the RateLimiter
func WithSourceIPLimitPerSecond ¶
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