Documentation
¶
Index ¶
- Constants
- Variables
- func DurFromSecString(s string) (time.Duration, error)
- type Algorithm
- type GCRAResult
- func ExecGCRAAllowAtMost(ctx context.Context, client valkey.Client, key string, burst int64, rate int64, ...) (GCRAResult, error)
- func ExecGCRAAllowN(ctx context.Context, client valkey.Client, key string, burst int64, rate int64, ...) (GCRAResult, error)
- func ParseGCRAResponse(resp valkey.ValkeyResult) (GCRAResult, error)
- type RateLimitOption
- type RateLimiterClient
- type RateLimiterOption
- type Result
Constants ¶
const (
PlaceholderPrefix = "valkeylimiter"
)
Variables ¶
var ( ErrInvalidTokens = errors.New("number of tokens must be non-negative") ErrInvalidResponse = errors.New("invalid response from Valkey") ErrInvalidLimit = errors.New("limit must be positive") ErrInvalidWindow = errors.New("window must be positive") ErrNilBuilder = errors.New("client builder is required") )
Functions ¶
Types ¶
type Algorithm ¶ added in v1.0.79
type Algorithm int
Algorithm specifies the rate limiting algorithm to use.
type GCRAResult ¶ added in v1.0.79
type GCRAResult struct {
Allowed int64
Remaining int64
RetryAfter time.Duration
ResetAfter time.Duration
}
GCRAResult contains the raw numeric and timing results returned by the GCRA Lua engine.
func ExecGCRAAllowAtMost ¶ added in v1.0.79
func ExecGCRAAllowN ¶ added in v1.0.79
func ParseGCRAResponse ¶ added in v1.0.79
func ParseGCRAResponse(resp valkey.ValkeyResult) (GCRAResult, error)
ParseGCRAResponse parses the 4-element array response from the GCRA Lua scripts. The array format is: [0] allowed (int64): count of allowed tokens (0 if rejected, or cost if allowed) [1] remaining (int64): maximum permitted instantaneous tokens [2] retry_after (string): float seconds until next request permitted ("-1" if allowed) [3] reset_after (string): float seconds until full rate limit restoration
type RateLimitOption ¶ added in v1.0.51
type RateLimitOption struct {
// contains filtered or unexported fields
}
func WithAlgorithm ¶ added in v1.0.79
func WithAlgorithm(alg Algorithm) RateLimitOption
WithAlgorithm creates a RateLimitOption specifying the algorithm strategy.
func WithBurst ¶ added in v1.0.79
func WithBurst(burst int) RateLimitOption
WithBurst creates a RateLimitOption specifying a custom burst capacity.
func WithCustomRateLimit ¶ added in v1.0.51
func WithCustomRateLimit(limit int, window time.Duration) RateLimitOption
WithCustomRateLimit creates a RateLimitOption with custom limit and window. Burst defaults to limit.
func WithCustomRateLimitAndBurst ¶ added in v1.0.79
func WithCustomRateLimitAndBurst(limit int, window time.Duration, burst int) RateLimitOption
WithCustomRateLimitAndBurst creates a RateLimitOption with custom limit, window, and burst.
type RateLimiterClient ¶
type RateLimiterClient interface {
Check(ctx context.Context, identifier string, options ...RateLimitOption) (Result, error)
Allow(ctx context.Context, identifier string, options ...RateLimitOption) (Result, error)
AllowN(ctx context.Context, identifier string, n int64, options ...RateLimitOption) (Result, error)
AllowAtMost(ctx context.Context, identifier string, n int64, options ...RateLimitOption) (Result, error)
Reset(ctx context.Context, identifier string, options ...RateLimitOption) error
Limit() int
Close()
}
func NewRateLimiter ¶
func NewRateLimiter(option RateLimiterOption) (RateLimiterClient, error)