redisengine

package
v1.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 28 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 redisengine runs an in-process Redis-protocol server (miniredis) on an allocator-provided or ephemeral port.

Index

Constants

View Source
const (
	PolicyNoEviction     = "noeviction"
	PolicyAllKeysLRU     = "allkeys-lru"
	PolicyVolatileLRU    = "volatile-lru"
	PolicyAllKeysLFU     = "allkeys-lfu"
	PolicyVolatileLFU    = "volatile-lfu"
	PolicyAllKeysRandom  = "allkeys-random"
	PolicyVolatileRandom = "volatile-random"
	PolicyVolatileTTL    = "volatile-ttl"
)

Eviction policies accepted by maxmemory-policy.

Variables

View Source
var ErrBadRDB = errors.New("invalid RDB data")

ErrBadRDB is returned when RDB data is malformed.

View Source
var ErrBadSnapshot = errors.New("invalid engine snapshot data")

ErrBadSnapshot is returned when snapshot data cannot be decoded.

View Source
var ErrInvalidSettings = errors.New("invalid engine settings")

ErrInvalidSettings is returned by Tune for values Redis would reject.

View Source
var ErrUnsupportedRDB = errors.New("unsupported RDB content")

ErrUnsupportedRDB is returned for RDB content the engine cannot represent.

Functions

func DecodeRDB

func DecodeRDB(mr *miniredis.Miniredis, data []byte) error

DecodeRDB is ImportRDB for a bare miniredis server.

func DumpKeyspace

func DumpKeyspace(mr *miniredis.Miniredis) ([]byte, error)

DumpKeyspace is Snapshot for a bare miniredis server. A nil server yields nil.

func EncodeRDB

func EncodeRDB(mr *miniredis.Miniredis) ([]byte, error)

EncodeRDB is ExportRDB for a bare miniredis server.

func LoadKeyspace

func LoadKeyspace(mr *miniredis.Miniredis, data []byte) error

LoadKeyspace is Restore for a bare miniredis server.

func ValidateAccessString

func ValidateAccessString(s string) error

ValidateAccessString reports whether SetUserAccess can enforce the access string.

Types

type Engine

type Engine struct {
	Mini *miniredis.Miniredis

	// Front relays clients to Mini behind an admit check; nil for ungated engines.
	Front *sgfilter.Proxy
	// Gate decides Front's admissions; deny-all until Set.
	Gate *Gate

	Host          string
	Port          int
	AllocatedPort int
	// contains filtered or unexported fields
}

Engine is a running in-process Redis-protocol server.

func Start

func Start(alloc *portalloc.Allocator, label string) (*Engine, error)

Start launches a server. With a non-nil allocator the port is acquired from it under label and bound on all interfaces; otherwise an ephemeral loopback port is used.

func StartGated

func StartGated(alloc *portalloc.Allocator, label string) (*Engine, error)

StartGated is Start with the client-facing port served by a relay in front of a loopback-only server, so Gate can refuse clients. The allocator port (or an ephemeral loopback one) is the relay's.

func StartTLS

func StartTLS(alloc *portalloc.Allocator, label string, cfg *tls.Config) (*Engine, error)

StartTLS is Start serving TLS with cfg; a nil cfg serves plaintext.

func (*Engine) Addr

func (e *Engine) Addr() string

Addr returns the connectable host:port.

func (*Engine) Close

func (e *Engine) Close()

Close stops the server and releases its allocated port. Nil-safe and idempotent.

func (*Engine) ExportRDB

func (e *Engine) ExportRDB() ([]byte, error)

ExportRDB serialises the keyspace as a Redis RDB file. Plain data is written as version 9; streams (with their consumer groups and pending entries) raise it to 11 and per-field hash TTLs to 12, the first versions that can carry them. Streams are cut into listpack nodes following stream-node-max-bytes and stream-node-max-entries.

func (*Engine) ImportRDB

func (e *Engine) ImportRDB(data []byte) error

ImportRDB replaces the keyspace with the contents of an RDB file. Strings, lists, sets, sorted sets and hashes in their plain encodings are supported (with LZF and integer string encodings); compact ziplist, listpack and intset encodings, streams and modules fail with ErrUnsupportedRDB. The keyspace is left untouched on error.

func (*Engine) RequireAuth

func (e *Engine) RequireAuth(password string)

RequireAuth makes the server demand password and remembers it so in-process operations (snapshots, RDB import and export) can authenticate.

func (*Engine) Restore

func (e *Engine) Restore(data []byte) error

Restore loads a blob produced by Snapshot into the engine, overwriting existing keys of the same name. Empty data is a no-op.

func (*Engine) SetUserAccess

func (e *Engine) SetUserAccess(users []UserAccess) error

SetUserAccess enforces the users' access strings: connections authenticate with AUTH, and each command, key and channel is checked against the authenticated user's rules (NOPERM on denial). Connections that never authenticate act as user "default", which is unrestricted unless listed. It installs the server's pre-command hook, so it cannot be combined with Tune. An empty list removes enforcement.

func (*Engine) Settings

func (e *Engine) Settings() Settings

Settings reports what Tune last applied, or DefaultSettings for an untuned engine.

func (*Engine) Snapshot

func (e *Engine) Snapshot() ([]byte, error)

Snapshot serialises the keyspace (strings, hashes, lists, sets, sorted sets, streams, key and hash-field TTLs) into a gzip'd opaque blob. Returns nil when the engine holds no keys. HyperLogLog keys and stream consumer groups are not captured.

func (*Engine) Tune

func (e *Engine) Tune(s Settings) error

Tune applies s to the running server and replaces any earlier settings. It installs the server's pre-command hook, so engines whose owner installs another hook must not use it. Without a call the server behaves like a bare miniredis.

type Gate

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

Gate holds a connection check installed after the engine starts, once its owner can name the target; until then every client is refused.

func (*Gate) Admit

func (g *Gate) Admit(remote net.Addr) bool

Admit implements sgfilter.Admitter.

func (*Gate) Set

func (g *Gate) Set(a sgfilter.Admitter)

Set installs the check.

type Settings

type Settings struct {
	RedisVersion         string
	Policy               string
	NotifyKeyspaceEvents string
	MaxMemory            int64
	Timeout              time.Duration
	SlowlogLogSlowerThan time.Duration
	LFULogFactor         int
	LFUDecayTime         int
	StreamNodeMaxBytes   int
	StreamNodeMaxEntries int
	Databases            int
	SlowlogMaxLen        int
	MaxMemoryClients     int
	ActiveDefrag         bool
	LazyfreeLazyEviction bool
	LazyfreeLazyExpire   bool
	LazyfreeLazyUserDel  bool
	LazyfreeLazyUserFlsh bool
}

Settings are the runtime parameters Tune applies to an engine; DefaultSettings holds the Redis defaults.

func DefaultSettings

func DefaultSettings() Settings

DefaultSettings returns Redis's stock configuration: no memory limit, noeviction, no keyspace events.

type UserAccess

type UserAccess struct {
	Name         string
	Password     string
	AccessString string
}

UserAccess is one ACL user: the AUTH password and the Redis ACL rule string (for example "on ~app:* &* +@read") enforced on that user's connections.

Jump to

Keyboard shortcuts

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