kv

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2026 License: Apache-2.0 Imports: 15 Imported by: 1 module, 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 kv is the key-value store for short-lived operational state (counters, breaker state, locks, sessions), with an in-memory backend and a Redis-protocol backend behind the same Store interface. Persistent data belongs in Postgres.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("state: key not found")

Functions

This section is empty.

Types

type BatchScripter added in v0.9.8

type BatchScripter interface {
	RunScriptBatch(ctx context.Context, calls []ScriptCall) []ScriptResult
}

BatchScripter is an optional interface implemented by stores that can run several independent scripts in a single network round trip (a Redis pipeline). Consumers type-assert to opt in.

Atomicity: each call is individually atomic on its own Cluster slot; the batch provides NO cross-call atomicity. Keys of DIFFERENT calls may live on different slots (each call's own keys must still share a hash tag). Use it only for independent operations — e.g. committing two rate-limit reservations that live under different hash tags and therefore cannot share one CROSSSLOT-safe script.

Results are positional (one per input call, same order); a per-call failure is carried in ScriptResult.Err rather than aborting the batch.

type Entry

type Entry struct {
	Key   string
	Value []byte
}

type Mem

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

Mem is an in-memory Store backed by sync.Map.

func NewMem

func NewMem() *Mem

NewMem constructs a Mem and starts the TTL janitor.

func (*Mem) Close

func (m *Mem) Close() error

func (*Mem) Del

func (m *Mem) Del(_ context.Context, key string) error

func (*Mem) Expire

func (m *Mem) Expire(_ context.Context, key string, ttl time.Duration) error

func (*Mem) Get

func (m *Mem) Get(_ context.Context, key string) ([]byte, error)

func (*Mem) HGet

func (m *Mem) HGet(_ context.Context, key, field string) ([]byte, error)

HGet reads one field of a hash stored as synthetic keys "key\x00field". Returns ErrNotFound when either the hash key or the field is absent.

func (*Mem) HSet

func (m *Mem) HSet(_ context.Context, key, field string, value []byte, ttl time.Duration) error

HSet writes one field of a hash stored as synthetic keys "key\x00field". When ttl > 0 the field entry gets that TTL; ttl == 0 preserves existing TTL.

func (*Mem) Incr

func (m *Mem) Incr(_ context.Context, key string, delta int64) (int64, error)

func (*Mem) Range

func (m *Mem) Range(_ context.Context, prefix string) ([]Entry, error)

func (*Mem) RegisterScript

func (m *Mem) RegisterScript(name string, fn ScriptImpl)

RegisterScript registers a Go emulator for a named script.

func (*Mem) RunScript

func (m *Mem) RunScript(ctx context.Context, name, _ string, keys []string, args ...any) ([]byte, error)

RunScript looks up the named emulator and invokes it.

func (*Mem) RunScriptBatch added in v0.9.8

func (m *Mem) RunScriptBatch(ctx context.Context, calls []ScriptCall) []ScriptResult

RunScriptBatch runs each call sequentially. Mem is in-process, so there is no round-trip to batch away; running the emulators in order is semantically identical to a Redis pipeline of independent single-slot scripts.

func (*Mem) Set

func (m *Mem) Set(_ context.Context, key string, value []byte, ttl time.Duration) error

func (*Mem) WithLock

func (m *Mem) WithLock(ctx context.Context, keys []string, fn func(context.Context) error) error

type Redis

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

Redis implements Store (and Scripter) backed by Redis/Valkey.

func NewRedis

func NewRedis(ctx context.Context, cfg RedisConfig) (*Redis, error)

NewRedis constructs a Redis and pings the server. Precedence: ClusterAddrs > Sentinel > Addr (single-node).

func (*Redis) Close

func (r *Redis) Close() error

func (*Redis) Del

func (r *Redis) Del(ctx context.Context, key string) error

func (*Redis) Expire

func (r *Redis) Expire(ctx context.Context, key string, ttl time.Duration) error

func (*Redis) Get

func (r *Redis) Get(ctx context.Context, key string) ([]byte, error)

func (*Redis) Incr

func (r *Redis) Incr(ctx context.Context, key string, delta int64) (int64, error)

func (*Redis) Ping

func (r *Redis) Ping(ctx context.Context) error

Ping checks the connection.

func (*Redis) Range

func (r *Redis) Range(ctx context.Context, prefix string) ([]Entry, error)

TODO(kv): Cluster-unsafe — SCAN only covers one shard.

func (*Redis) RunScript

func (r *Redis) RunScript(ctx context.Context, name, script string, keys []string, args ...any) ([]byte, error)

RunScript implements Scripter.

func (*Redis) RunScriptBatch added in v0.9.8

func (r *Redis) RunScriptBatch(ctx context.Context, calls []ScriptCall) []ScriptResult

RunScriptBatch implements BatchScripter: it issues every call in a single pipeline (one network round trip on single-node; one per involved node on Cluster, dispatched together). Each call is an independent EVALSHA — the batch is NOT a transaction, so a per-call failure does not roll back the others. Keys within one call must share a hash tag; keys ACROSS calls may differ (that is the point — it batches away sequential round trips for operations that cannot share a CROSSSLOT-safe script).

func (*Redis) Set

func (r *Redis) Set(ctx context.Context, key string, value []byte, ttl time.Duration) error

func (*Redis) WithLock

func (r *Redis) WithLock(ctx context.Context, keys []string, fn func(context.Context) error) error

WithLock implements the blocking Store contract over SET NX PX: acquisition is retried with jittered backoff (5-25ms) until it succeeds or ctx is done, matching Mem's block-until-acquired semantics. The all-or-nothing Lua acquire (partial holds are rolled back before returning 0) keeps opposite key orders deadlock-free while polling. Cluster safety: all keys must share the same hash tag, else CROSSSLOT.

type RedisConfig

type RedisConfig struct {
	Addr         string
	Sentinel     *SentinelConfig
	ClusterAddrs []string // non-empty → Cluster mode (redis.NewClusterClient)
	DB           int
	Password     string
	PoolSize     int
	MinIdleConns int
}

RedisConfig configures a Redis store. Exactly one of Addr, Sentinel, or ClusterAddrs should be set. When ClusterAddrs is non-empty, a redis.ClusterClient is used and all multi-key operations (RunScript, WithLock) require keys to share the same hash tag to avoid CROSSSLOT errors.

type ScriptCall added in v0.9.8

type ScriptCall struct {
	Name   string
	Script string
	Keys   []string
	Args   []any
}

ScriptCall describes one script invocation for a batch.

type ScriptImpl

type ScriptImpl func(ctx context.Context, store *Mem, keys []string, args []any) ([]byte, error)

ScriptImpl is the Go-emulator function registered on MemStore. It receives the store so it can call Get/Set/Incr etc. directly.

type ScriptResult added in v0.9.8

type ScriptResult struct {
	Value []byte
	Err   error
}

ScriptResult is the positional result of one ScriptCall in a batch.

type Scripter

type Scripter interface {
	RunScript(ctx context.Context, name, script string, keys []string, args ...any) ([]byte, error)
}

ScriptRunner is an optional interface implemented by stores that support named Lua (or equivalent) scripts. Consumers type-assert to opt in.

type SentinelConfig

type SentinelConfig struct {
	MasterName       string
	SentinelAddrs    []string
	SentinelPassword string
}

SentinelConfig configures Sentinel-mode failover.

type Store

type Store interface {
	Get(ctx context.Context, key string) ([]byte, error)
	Set(ctx context.Context, key string, value []byte, ttl time.Duration) error
	// Del removes a key. It is a no-op (no error) when the key does not exist.
	Del(ctx context.Context, key string) error
	Incr(ctx context.Context, key string, delta int64) (int64, error)
	Expire(ctx context.Context, key string, ttl time.Duration) error
	Range(ctx context.Context, prefix string) ([]Entry, error)
	// WithLock runs fn while holding an advisory lock over all keys.
	// BLOCKING contract: under contention the call waits (retrying with
	// backoff on distributed backends) until the lock is acquired — every
	// contender eventually runs fn, or returns the ctx error if ctx is
	// cancelled/expires while waiting. It never returns a "busy" error
	// without running fn. Mutual exclusion holds across all keys for the
	// duration of fn.
	WithLock(ctx context.Context, keys []string, fn func(context.Context) error) error
	Close() error
}

Store is the operational-state adapter. M1 ships MemStore; M5 will add a Redis-backed implementation.

Directories

Path Synopsis
Package kvtest holds kv fixtures for tests: a Recording store that pins the scripts a caller runs, and (integration builds) a Redis/Valkey keyspace of its own per test on the server named by RELAY_TEST_REDIS_ADDR, so test packages run in parallel against one server despite fixed key names and prefix scans.
Package kvtest holds kv fixtures for tests: a Recording store that pins the scripts a caller runs, and (integration builds) a Redis/Valkey keyspace of its own per test on the server named by RELAY_TEST_REDIS_ADDR, so test packages run in parallel against one server despite fixed key names and prefix scans.

Jump to

Keyboard shortcuts

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