Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseFullConfig ¶ added in v0.19.0
func ParseFullConfig(path string) (Agents, TierLimits, error)
ParseFullConfig interprets the configuration file and returns both agent limits and tier limits.
Types ¶
type AgentConfig ¶
type AgentConfig struct {
Agent string `yaml:"agent"`
Schedule string `yaml:"schedule"`
Duration time.Duration `yaml:"duration"`
}
AgentConfig holds the limit configuration for a user agent.
type Cron ¶
type Cron struct {
*cronexpr.Expression
// contains filtered or unexported fields
}
Cron infers time limits based on a cron schedule.
type FullConfig ¶ added in v0.19.0
type FullConfig struct {
Agents []AgentConfig `yaml:"agents"`
Tiers map[int]TierLimitConfig `yaml:"tiers"`
}
FullConfig holds the complete limit configuration including both agent-based and tier-based limits.
type LimitConfig ¶ added in v0.17.4
type LimitConfig struct {
// Interval defines the duration of the sliding window
Interval time.Duration
// MaxEvents defines the maximum number of events allowed in the interval
MaxEvents int
}
LimitConfig holds the configuration for a single rate limit type
type LimitStatus ¶ added in v0.17.4
type LimitStatus struct {
// IsLimited indicates if the request should be rate limited
IsLimited bool
// LimitType indicates which limit was exceeded ("ip" or "ipua" or "")
LimitType string
}
LimitStatus indicates the result of a rate limit check
type RateLimitConfig ¶ added in v0.17.0
type RateLimitConfig struct {
// IPConfig defines the rate limiting configuration for IP-only checks
IPConfig LimitConfig
// IPUAConfig defines the rate limiting configuration for IP+UA checks
IPUAConfig LimitConfig
// KeyPrefix is the prefix for Redis keys
KeyPrefix string
}
RateLimitConfig holds the configuration for both IP-only and IP+UA rate limiting.
type RateLimiter ¶ added in v0.17.0
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter implements a distributed rate limiter using Redis sorted sets (ZSET). It maintains sliding windows for both IP-only and IP+UA combinations, where:
- Each event is stored in a ZSET with the timestamp as score
- Old events (outside the window) are automatically removed
- Keys automatically expire after the configured interval
The limiter considers a request to be rate-limited if the number of events in either window exceeds their respective MaxEvents.
func NewRateLimiter ¶ added in v0.17.0
func NewRateLimiter(pool *redis.Pool, config RateLimitConfig) *RateLimiter
NewRateLimiter creates a new rate limiter.
func (*RateLimiter) IsLimited ¶ added in v0.17.0
func (rl *RateLimiter) IsLimited(ip, ua string) (LimitStatus, error)
IsLimited checks if the given IP and User-Agent combination should be rate limited. It first checks the IP-only limit, then the IP+UA limit if the IP-only check passes.
func (*RateLimiter) IsLimitedWithTier ¶ added in v0.19.0
func (rl *RateLimiter) IsLimitedWithTier(org, ip string, tierConfig LimitConfig) (LimitStatus, error)
IsLimitedWithTier checks if the given organization+IP combination should be rate limited based on the tier-specific configuration. This is used for /v2/priority/nearest endpoint where different tiers have different rate limits.
type TierLimitConfig ¶ added in v0.19.0
TierLimitConfig holds the rate limit configuration for a single tier.
type TierLimits ¶ added in v0.19.0
type TierLimits map[int]LimitConfig
TierLimits maps tier numbers to their LimitConfig.