Documentation
¶
Overview ¶
Package ratelimit implements a rate limiting middleware
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RateLimiterWithConfig ¶
func RateLimiterWithConfig(conf *Config) echo.MiddlewareFunc
RateLimiterWithConfig returns a middleware function for rate limiting requests with a supplied config.
Types ¶
type Config ¶
type Config struct {
Enabled bool `json:"enabled" koanf:"enabled" default:"false"`
// Options enables configuring multiple concurrent rate windows that must all pass.
Options []RateOption `json:"options" koanf:"options"`
// Headers determines which headers are inspected to determine the origin IP.
// Defaults to X-Forwarded-For, True-Client-IP, RemoteAddr.
Headers []string `json:"headers" koanf:"headers" default:"[True-Client-IP]"`
// ForwardedIndexFromBehind selects which IP from X-Forwarded-For should be used.
// 0 means the closest client, 1 the proxy behind it, etc.
ForwardedIndexFromBehind int `json:"forwardedindexfrombehind" koanf:"forwardedindexfrombehind" default:"0"`
// IncludePath appends the request path to the limiter key when true.
IncludePath bool `json:"includepath" koanf:"includepath" default:"false"`
// IncludeMethod appends the request method to the limiter key when true.
IncludeMethod bool `json:"includemethod" koanf:"includemethod" default:"false"`
// KeyPrefix allows scoping the limiter key space with a static prefix.
KeyPrefix string `json:"keyprefix" koanf:"keyprefix"`
// DenyStatus overrides the HTTP status code returned when a rate limit is exceeded.
DenyStatus int `json:"denystatus" koanf:"denystatus" default:"429"`
// DenyMessage customises the error payload when a rate limit is exceeded.
DenyMessage string `json:"denymessage" koanf:"denymessage" default:"Too many requests"`
// SendRetryAfterHeader toggles whether the Retry-After header should be added when available.
SendRetryAfterHeader bool `json:"sendretryafterheader" koanf:"sendretryafterheader" default:"true"`
// DryRun enables logging rate limit decisions without blocking requests.
DryRun bool `json:"dryrun" koanf:"dryrun" default:"true"`
}
Config defines the configuration settings for the rate limiter middleware.
type LimitStatus ¶ added in v0.43.0
type LimitStatus struct {
// IsLimited is true when a given key should be rate-limited
IsLimited bool
// LimitDuration is the time for which a given key should be blocked
LimitDuration *time.Duration
// CurrentRate is approximated current requests rate per window size
CurrentRate float64
}
LimitStatus represents current status of limitation for a given key
type LimitStore ¶ added in v0.43.0
type LimitStore interface {
// Inc increments current window limit counter for key
Inc(key string, window time.Time) error
// Get gets value of previous window counter and current window counter for key
Get(key string, previousWindow, currentWindow time.Time) (prevValue int64, currValue int64, err error)
}
LimitStore is the interface that represents limiter internal data store
type MapLimitStore ¶ added in v0.43.0
type MapLimitStore struct {
// contains filtered or unexported fields
}
MapLimitStore represents a data structure for in-memory storage of ratelimiter information
func NewMapLimitStore ¶ added in v0.43.0
func NewMapLimitStore(expirationTime time.Duration, flushInterval time.Duration) (m *MapLimitStore)
NewMapLimitStore creates new in-memory data store for internal limiter data
func (*MapLimitStore) Get ¶ added in v0.43.0
func (m *MapLimitStore) Get(key string, previousWindow, currentWindow time.Time) (prevValue int64, currValue int64, err error)
Get gets value of previous window counter and current window counter
func (*MapLimitStore) Inc ¶ added in v0.43.0
func (m *MapLimitStore) Inc(key string, window time.Time) error
Inc increments current window limit counter
func (*MapLimitStore) Size ¶ added in v0.43.0
func (m *MapLimitStore) Size() int
Size returns current length of data map
type RateLimiter ¶ added in v0.43.0
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is defining the structure of a rate limiter object
func New ¶ added in v0.43.0
func New(dataStore LimitStore, requestsLimit int64, windowSize time.Duration) *RateLimiter
New creates new rate limiter
func (*RateLimiter) Check ¶ added in v0.43.0
func (r *RateLimiter) Check(key string) (limitStatus *LimitStatus, err error)
Check checks status of the rate limit key
func (*RateLimiter) Inc ¶ added in v0.43.0
func (r *RateLimiter) Inc(key string) error
Inc increments the limit counter for a given key
func (*RateLimiter) RequestsLimit ¶ added in v0.43.0
func (r *RateLimiter) RequestsLimit() int64
RequestsLimit returns the maximum number of requests allowed within the window.
func (*RateLimiter) WindowSize ¶ added in v0.43.0
func (r *RateLimiter) WindowSize() time.Duration
WindowSize returns the duration of the configured window.
type RateOption ¶ added in v0.43.0
type RateOption struct {
// Requests is the number of requests allowed within the configured window.
Requests int64 `json:"requests" koanf:"requests" default:"500" example:"500"`
// Window is the duration of the sliding window.
Window time.Duration `json:"window" koanf:"window" default:"1m"`
// Expiration controls how long counters are retained before eviction.
// When unset, Expiration defaults to twice the Window duration.
Expiration time.Duration `json:"expiration" koanf:"expiration"`
// FlushInterval defines how frequently expired values are purged from memory.
// When unset, FlushInterval defaults to 10s.
FlushInterval time.Duration `json:"flushinterval" koanf:"flushinterval"`
}
RateOption defines a distinct rate limiting window and request allowance.