Documentation
¶
Index ¶
- type Limiter
- func (l *Limiter) AdjustRateByState(state LimiterState)
- func (l *Limiter) CurrentRPM() int
- func (l *Limiter) WaitForAllowance(ctx context.Context) error
- func (l *Limiter) WaitUntilReset(ctx context.Context, reset *time.Time) error
- func (l *Limiter) WithClock(clock utils.Clock) *Limiter
- func (l *Limiter) WithLogger(logger Logger) *Limiter
- type LimiterConfig
- type LimiterState
- type Logger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Limiter ¶
type Limiter struct {
// contains filtered or unexported fields
}
Limiter provides token-bucket rate limiting with optional dynamic adjustment based on upstream rate limit state.
Thread-safe: all public methods are safe for concurrent use.
func NewLimiter ¶
func NewLimiter(cfg *LimiterConfig) *Limiter
NewLimiter constructs a Limiter and initializes the internal rate limiter. If cfg is nil, DefaultLimiterConfig() is used.
func (*Limiter) AdjustRateByState ¶
func (l *Limiter) AdjustRateByState(state LimiterState)
AdjustRateByState adjusts the limiter rate based on upstream limit state.
The goal is to avoid exhausting the upstream window by computing a "safe" rate derived from RemainingLimit and time until ResetTime, then clamping to [MinRequestsPerMinute, RequestsLimitPerMinute].
It is throttled by cfg.AdjustEveryMillis (default 10s) to avoid thrashing.
func (*Limiter) CurrentRPM ¶
CurrentRPM returns the currently configured rate limit in requests per minute.
func (*Limiter) WaitForAllowance ¶
WaitForAllowance blocks until a token is available or ctx is done.
This should be called before making an upstream request.
func (*Limiter) WaitUntilReset ¶
WaitUntilReset performs a hard wait until the provided reset time. This is useful when upstream returns a definitive "rate limited until X".
If reset is nil, returns nil. If reset is in the past, returns nil. Respect ctx cancellation.
func (*Limiter) WithLogger ¶
WithLogger replaces the logger.
type LimiterConfig ¶
type LimiterConfig struct {
// RequestsLimitPerMinute is the maximum steady-state rate this limiter will
// allow under nominal conditions.
RequestsLimitPerMinute int `json:"requests_limit_per_minute" yaml:"requests_limit_per_minute" mapstructure:"requests_limit_per_minute"`
// MinRequestsPerMinute is a safety floor when dynamically adjusting rate.
// If <= 0, defaults to 1.
MinRequestsPerMinute int `json:"min_requests_per_minute" yaml:"min_requests_per_minute" mapstructure:"min_requests_per_minute"`
// Burst is the token bucket burst size. Lower bursts avoid spiky traffic.
// If <= 0, a conservative default is used (1).
Burst int `json:"burst" yaml:"burst" mapstructure:"burst"`
// AdjustEvery is a minimum interval between adjustments to avoid thrashing.
// If zero, defaults to 10 seconds.
AdjustEveryMillis int `json:"adjust_every_millis" yaml:"adjust_every_millis" mapstructure:"adjust_every_millis"`
}
func DefaultLimiterConfig ¶
func DefaultLimiterConfig() LimiterConfig
func (*LimiterConfig) Ref ¶
func (c *LimiterConfig) Ref() string
func (*LimiterConfig) VarMerge ¶
func (c *LimiterConfig) VarMerge(namespace string)
func (*LimiterConfig) WithRequestsLimitPerMinute ¶
func (c *LimiterConfig) WithRequestsLimitPerMinute(limit int) *LimiterConfig
type LimiterState ¶
LimiterState describes the upstream rate limit situation.
TotalLimit and RemainingLimit are counts for the current rate-limit window. ResetTime is when the window resets (upstream-provided time). Status is a coarse state string used by callers/adapters: - "nominal": normal operation - "limited": upstream says we are limited (e.g., 429/403 rate limit) - "waiting": we should wait until reset time - "error": upstream error parsing/unknown - "passive": no upstream info available