redisstore

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package redisstore provides a Redis-backed ratelimit.Store (token-bucket) built on the native celeris driver/redis client.

The token-bucket state is stored as a Redis hash {tokens, last} per key; atomic updates are performed by an embedded Lua script loaded via SCRIPT LOAD at New time. EVALSHA is used on the hot path; on NOSCRIPT (script flushed from server cache) the script is reloaded once and the call is retried.

Atomicity is per-key: a single key's bucket is updated atomically. Cross-key operations are independent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// KeyPrefix is prepended to every rate limit key. Default: "rl:".
	KeyPrefix string

	// RPS is the refill rate in tokens/second. Required (> 0).
	RPS float64

	// Burst is the bucket capacity (maximum tokens). Required (> 0).
	Burst int

	// TTL is the Redis expiry applied to each bucket key. Default:
	// 2 * (Burst / RPS), minimum 1 minute.
	TTL time.Duration
}

Options configure the Redis-backed rate limit store.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store implements ratelimit.Store and ratelimit.StoreUndo.

func New

func New(ctx context.Context, client *redis.Client, opts Options) (*Store, error)

New constructs a Store and preloads the Lua scripts. Returns an error if script loading fails.

func (*Store) Allow

func (s *Store) Allow(key string) (bool, int, time.Time, error)

Allow implements ratelimit.Store. A passed key is prefixed with the configured KeyPrefix before hitting Redis.

func (*Store) Undo

func (s *Store) Undo(key string) error

Undo implements ratelimit.StoreUndo — returns one token to the bucket, capped at Burst. Called when a request was permitted but ultimately should not count (e.g. SkipFailedRequests semantics).

Jump to

Keyboard shortcuts

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