Documentation
¶
Overview ¶
Package limitbase provides shared helpers for the rate-limit plugin family: limit-req, limit-conn, limit-count, and graphql-limit-count.
Index ¶
- Variables
- func RedisInt(value any) (int64, bool)
- func ResolveVars(tpl string, lookup func(string) string) (string, int)
- func ResolveVarsWithLookup(tpl string, lookup func(string) (string, bool)) (string, int)
- type ConnectionResult
- type FixedWindowResult
- type FixedWindowState
- type LeakyBucketResult
- type QuotaHeaders
- type State
- func (state *State) AcquireConnection(key string, limit int64) ConnectionResult
- func (state *State) AdjustFixedWindow(key string, limit int64, delta int64, window time.Duration, create bool) FixedWindowState
- func (state *State) Close()
- func (state *State) ConnectionCount(key string) int64
- func (state *State) FixedWindow(key string, limit int64, cost int64, window time.Duration, commit bool) FixedWindowResult
- func (state *State) FixedWindowCount() int
- func (state *State) FixedWindowSnapshot(key string, limit int64, window time.Duration) FixedWindowState
- func (state *State) LeakyBucket(key string, rate float64, burst float64) LeakyBucketResult
- func (state *State) ReleaseConnection(key string)
Constants ¶
This section is empty.
Variables ¶
var DefaultVarPattern = regexp.MustCompile(`^\$\{\s*([0-9A-Za-z_]+)\s*\?\?\s*([^{}]+?)\s*\}$`)
DefaultVarPattern matches ${name ?? default} references with an inline fallback value used by limit-conn and limit-count.
var VarPattern = regexp.MustCompile(`\$\{([0-9A-Za-z_]+)\}|\$([0-9A-Za-z_]+)`)
VarPattern matches $name and ${name} variable references in key and value expressions used by limit-req and limit-conn.
Functions ¶
func RedisInt ¶
RedisInt converts a Redis wire value to int64. Accepted types are int, int64, uint64 within int64 range, and decimal strings. Every other type and an overflowing uint64 is rejected.
func ResolveVars ¶
ResolveVars expands APISIX variable expressions in tpl. It supports $name, ${name}, dotted names, and ${name ?? default}; an escaped dollar is left untouched. The returned count includes only variables that resolved to a value or used a default, matching APISIX core.utils.resolve_var.
Types ¶
type ConnectionResult ¶
ConnectionResult is the APISIX limit-conn active reservation result.
type FixedWindowResult ¶
FixedWindowResult is the APISIX limit-count local-store result.
type FixedWindowState ¶
type LeakyBucketResult ¶
type QuotaHeaders ¶
QuotaHeaders names the rate-limit response headers.
func DefaultQuotaHeaders ¶
func DefaultQuotaHeaders(limitHeader, remainingHeader, resetHeader string) QuotaHeaders
DefaultQuotaHeaders returns the default quota header names, substituting the X-RateLimit-* names for empty fields.
func RuleQuotaHeaders ¶
func RuleQuotaHeaders(prefix string, index int) QuotaHeaders
RuleQuotaHeaders returns per-rule quota header names, numbering the rule index plus one when no header prefix is configured.
type State ¶
type State struct {
// contains filtered or unexported fields
}
State owns the process-scoped local state shared by APISIX rate-limit plugin instances across immutable generations.
func NewStateWithClock ¶
func (*State) AcquireConnection ¶
func (state *State) AcquireConnection(key string, limit int64) ConnectionResult
func (*State) AdjustFixedWindow ¶
func (*State) ConnectionCount ¶
func (*State) FixedWindow ¶
func (state *State) FixedWindow( key string, limit int64, cost int64, window time.Duration, commit bool, ) FixedWindowResult
FixedWindow checks or commits one cost against a fixed window. A rejected committed cost remains visible until the window expires, matching APISIX's count store and Redis script behavior.
func (*State) FixedWindowCount ¶
func (*State) FixedWindowSnapshot ¶
func (*State) LeakyBucket ¶
func (state *State) LeakyBucket(key string, rate float64, burst float64) LeakyBucketResult
LeakyBucket applies the APISIX limit-req local-policy algorithm. State is process-owned so an unchanged effective configuration survives immutable generation replacement, like APISIX's plugin-limit-req shared dictionary.