Documentation
¶
Overview ¶
Package traefikratelimiter provides a URL-level, in-memory rate limiting middleware for Traefik with access-log friendly response headers.
Index ¶
Constants ¶
const HeaderKey = "X-RateLimit-Key"
HeaderKey exposes the internal rate-limit key used for this request, formatted as "{ruleID}|{ip}|{realPath}". Useful for debugging and access log correlation.
const HeaderLimit = "X-RateLimit-Limit"
HeaderLimit exposes the configured request budget for the window.
const HeaderRemaining = "X-RateLimit-Remaining"
HeaderRemaining exposes the remaining quota, formatted as "<remaining>/<period>".
const HeaderReset = "X-RateLimit-Reset"
HeaderReset exposes the absolute reset time as a unix timestamp.
const HeaderRetryAfter = "X-RateLimit-RetryAfter"
HeaderRetryAfter exposes the seconds until the window resets, formatted with a trailing "s" (e.g. "0s").
const HeaderUsed = "X-RateLimit-Used"
HeaderUsed exposes the in-window used count, formatted as "<count>/<period>".
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
IPStrategy IPStrategyConfig `json:"ipStrategy,omitempty" yaml:"ipStrategy,omitempty" toml:"ipStrategy,omitempty"`
Default LimitConfig `json:"default,omitempty" yaml:"default,omitempty" toml:"default,omitempty"`
Rules []RuleConfig `json:"rules,omitempty" yaml:"rules,omitempty" toml:"rules,omitempty"`
// AddHeaders controls whether rate-limit response headers are written.
// Defaults to true. Set to false to suppress all X-RateLimit-* headers.
AddHeaders *bool `json:"addHeaders,omitempty" yaml:"addHeaders,omitempty" toml:"addHeaders,omitempty"`
// Redis enables the Redis-backed counter store when Addr is set.
// When nil or Addr is empty, the in-process memory store is used.
Redis *RedisConfig `json:"redis,omitempty" yaml:"redis,omitempty" toml:"redis,omitempty"`
}
Config is the root configuration consumed by the plugin.
func CreateConfig ¶
func CreateConfig() *Config
CreateConfig returns a Config populated with sensible defaults.
type IPStrategyConfig ¶
type IPStrategyConfig struct {
// Header is the primary header name to read the client IP from. When
// empty, "X-Forwarded-For" is used.
Header string `json:"header,omitempty" yaml:"header,omitempty" toml:"header,omitempty"`
// Depth controls which entry of a comma separated header value is used.
// 0 (default) means the left-most entry (original client). A positive
// value counts from the right (1 = right-most).
Depth int `json:"depth,omitempty" yaml:"depth,omitempty" toml:"depth,omitempty"`
// TrustedHeaders is an ordered list of additional headers tried after
// Header. The first non-empty header wins.
TrustedHeaders []string `json:"trustedHeaders,omitempty" yaml:"trustedHeaders,omitempty" toml:"trustedHeaders,omitempty"`
}
IPStrategyConfig defines how the client IP is extracted from the request.
type LimitConfig ¶
type LimitConfig struct {
// Requests is the maximum number of requests allowed during the window.
Requests int64 `json:"requests,omitempty" yaml:"requests,omitempty" toml:"requests,omitempty"`
// Period is the size of the fixed window. Examples: "10s", "1m", "2h", "1d".
Period string `json:"period,omitempty" yaml:"period,omitempty" toml:"period,omitempty"`
}
LimitConfig is the limit definition shared by the default block and individual rules.
type MatchType ¶
type MatchType string
MatchType describes how a rule path is compared against the request URL.
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter is the Traefik middleware implementation.
func (*RateLimiter) ServeHTTP ¶
func (rl *RateLimiter) ServeHTTP(rw http.ResponseWriter, req *http.Request)
type RedisConfig ¶ added in v0.3.0
type RedisConfig struct {
// Addr is the Redis server address in "host:port" form (default "127.0.0.1:6379").
Addr string `json:"addr,omitempty" yaml:"addr,omitempty" toml:"addr,omitempty"`
// Password is sent via AUTH. Leave empty when Redis has no password.
Password string `json:"password,omitempty" yaml:"password,omitempty" toml:"password,omitempty"`
// DB selects the logical database (default 0).
DB int `json:"db,omitempty" yaml:"db,omitempty" toml:"db,omitempty"`
// KeyPrefix is prepended to every Redis key (e.g. "rl:").
KeyPrefix string `json:"keyPrefix,omitempty" yaml:"keyPrefix,omitempty" toml:"keyPrefix,omitempty"`
}
RedisConfig holds connection settings for the optional Redis-backed store. When Addr is non-empty, the middleware uses Redis instead of the in-process memory store, which allows rate-limit state to be shared across multiple Traefik replicas.
type RuleConfig ¶
type RuleConfig struct {
Requests int64 `json:"requests,omitempty" yaml:"requests,omitempty" toml:"requests,omitempty"`
Period string `json:"period,omitempty" yaml:"period,omitempty" toml:"period,omitempty"`
// Name is optional and is included in the internal counter key for
// readability; defaults to "r{index}".
Name string `json:"name,omitempty" yaml:"name,omitempty" toml:"name,omitempty"`
Path string `json:"path,omitempty" yaml:"path,omitempty" toml:"path,omitempty"`
MatchType string `json:"matchType,omitempty" yaml:"matchType,omitempty" toml:"matchType,omitempty"`
Methods []string `json:"methods,omitempty" yaml:"methods,omitempty" toml:"methods,omitempty"`
}
RuleConfig describes a single per-path rule.