memcachedstore

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: 8 Imported by: 0

Documentation

Overview

Package memcachedstore provides a memcached-backed ratelimit.Store (token-bucket) built on the native celeris driver/memcached client. Drop-in rival to middleware/ratelimit/redisstore for deployments that prefer memcached.

Memcached does not support server-side scripting (no Lua EVALSHA), so the atomic token-bucket update is implemented via a CAS loop: Gets retrieves state + CAS token, the client computes new state, then CAS writes it back. On CAS conflict (another goroutine won the race) we retry up to [maxCASRetries] times. In the uncontended common case this is a single Gets + CAS round trip.

Atomicity is per-key: each key's bucket is updated atomically via its own CAS loop. 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 per second. Required (> 0).
	RPS float64

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

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

Options configure the memcached-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(client *celmc.Client, opts Options) (*Store, error)

New constructs a Store. Returns an error on invalid options.

func (*Store) Allow

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

Allow implements ratelimit.Store.

func (*Store) RetriesTotal

func (s *Store) RetriesTotal() uint64

RetriesTotal returns the number of CAS retries observed since construction. Useful for monitoring contention.

func (*Store) Undo

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

Undo implements ratelimit.StoreUndo. Returns one token to the bucket, capped at Burst. Uses the same CAS loop as Allow.

Jump to

Keyboard shortcuts

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