limitbase

package
v0.1.0-alpha.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 2, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package limitbase provides shared helpers for the rate-limit plugin family: limit-req, limit-conn, limit-count, and graphql-limit-count.

Index

Constants

This section is empty.

Variables

View Source
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.

View Source
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

func RedisInt(value any) (int64, bool)

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

func ResolveVars(tpl string, lookup func(string) string) (string, int)

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

type ConnectionResult struct {
	Allowed bool
	Current int64
}

ConnectionResult is the APISIX limit-conn active reservation result.

type FixedWindowResult

type FixedWindowResult struct {
	Allowed   bool
	Remaining int64
	Reset     time.Duration
}

FixedWindowResult is the APISIX limit-count local-store result.

type FixedWindowState

type FixedWindowState struct {
	Exists    bool
	Remaining int64
	Reset     time.Duration
}

type LeakyBucketResult

type LeakyBucketResult struct {
	Allowed bool
	Delay   time.Duration
}

type QuotaHeaders

type QuotaHeaders struct {
	Limit     string
	Remaining string
	Reset     string
}

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 NewState

func NewState() *State

func NewStateWithClock

func NewStateWithClock(now func() time.Time) *State

func (*State) AcquireConnection

func (state *State) AcquireConnection(key string, limit int64) ConnectionResult

func (*State) AdjustFixedWindow

func (state *State) AdjustFixedWindow(
	key string,
	limit int64,
	delta int64,
	window time.Duration,
	create bool,
) FixedWindowState

func (*State) Close

func (state *State) Close()

func (*State) ConnectionCount

func (state *State) ConnectionCount(key string) int64

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 *State) FixedWindowCount() int

func (*State) FixedWindowSnapshot

func (state *State) FixedWindowSnapshot(
	key string,
	limit int64,
	window time.Duration,
) FixedWindowState

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.

func (*State) ReleaseConnection

func (state *State) ReleaseConnection(key string)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL