redisdocker

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: 29 Imported by: ? modules, 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 redisdocker runs real Redis or Valkey containers as a replication group or a sharded cluster and manages their ACLs, TLS and RDB snapshots.

Index

Constants

View Source
const (
	EngineRedis  = "redis"
	EngineValkey = "valkey"
)

Engine names.

View Source
const AdminUser = "gs-admin"

AdminUser is the internal account the emulator uses for control traffic; it is never exposed to API users.

View Source
const TotalSlots = 16384

TotalSlots is the size of the Redis cluster hash-slot space.

Variables

View Source
var (
	// ErrShardRange is returned for a shard index the group does not have.
	ErrShardRange = errors.New("shard index out of range")
	// ErrNoReplica is returned when a failover has no replica to promote.
	ErrNoReplica = errors.New("shard has no replica to promote")
	// ErrGroupClosed is returned for operations on a closed group.
	ErrGroupClosed = errors.New("redis group is closed")
)
View Source
var ErrACL = errors.New("invalid ACL user")

ErrACL is returned for an unusable ACL definition.

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

ErrBadSnapshot is returned when snapshot data cannot be read.

View Source
var ErrNoCA = errors.New("redis group needs a TLS certificate authority")

ErrNoCA is returned when TLS is requested but the engine has no certificate authority.

View Source
var ErrNotConverged = errors.New("redis group did not converge")

ErrNotConverged is returned when a group does not reach its target state before the start timeout.

View Source
var ErrShape = errors.New("snapshot shards do not match the cluster")

ErrShape is returned when snapshot data cannot be spread over the requested shards.

View Source
var ErrSlots = errors.New("invalid slot assignment")

ErrSlots is returned when a slot assignment is malformed or does not cover the hash-slot space exactly once.

View Source
var ErrSpec = errors.New("invalid redis group spec")

ErrSpec is returned when a launch request is inconsistent.

Functions

func DecodeBundle

func DecodeBundle(blob []byte) ([][]byte, bool)

DecodeBundle unpacks EncodeBundle output; ok is false when blob is not a bundle.

func EncodeBundle

func EncodeBundle(shards [][]byte) []byte

EncodeBundle packs one RDB per shard into a single blob.

func EvenSlots

func EvenSlots(n int) [][]SlotRange

EvenSlots splits the hash-slot space across n shards; the last shard takes the remainder.

func ImageFor

func ImageFor(engine, version string) (string, string)

ImageFor maps an engine and API version to a container image. Versions below 7 run on 7.0, whose cluster hostname support the group relies on; odd minors map to the next published image.

func IsRDB

func IsRDB(blob []byte) bool

IsRDB reports whether blob is a raw RDB file.

func LoadEmbedded

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

LoadEmbedded loads a snapshot blob produced by either engine mode into an embedded server.

func SeedFromBlob

func SeedFromBlob(blob []byte) ([]byte, error)

SeedFromBlob converts any snapshot payload this module produces (a raw RDB or an embedded keyspace dump) to an RDB.

func SeedsFor

func SeedsFor(blob []byte, shards int) ([][]byte, error)

SeedsFor turns a snapshot blob (bundle, raw RDB or embedded dump) into per-shard RDB seeds for shards shards.

func SetUserArgs

func SetUserArgs(u ACLUser) ([]any, error)

SetUserArgs translates u into an ACL SETUSER command. The user is reset first so repeated syncs converge, then gets its passwords and the rules of its access string ("on ~app:* +@all").

func ValidateSlotMap

func ValidateSlotMap(shards [][]SlotRange) error

ValidateSlotMap checks that the shards together cover every hash slot exactly once.

Types

type ACLPolicy

type ACLPolicy struct {
	// DefaultPassword protects the "default" account when no listed user is named "default".
	DefaultPassword string
	Users           []ACLUser
	// OpenDefault leaves the "default" account passwordless and unrestricted when no listed user is named "default".
	OpenDefault bool
}

ACLPolicy is the full set of accounts a group serves.

type ACLUser

type ACLUser struct {
	Name         string
	AccessString string
	Passwords    []string
	// NoPassword lets the user in without a password.
	NoPassword bool
}

ACLUser is one Redis ACL account derived from an API user.

type Admin

type Admin interface {
	Do(ctx context.Context, args ...any) (any, error)
	Close() error
}

Admin sends commands to one node.

type Config

type Config struct {
	Runtime Runtime
	Ports   *portalloc.Allocator
	// CA signs node certificates; TLS groups cannot launch without it.
	CA *devtls.CA
	// Dial opens a control connection; defaults to go-redis.
	Dial Dialer
	// FetchRDB reads a node's dataset; defaults to a SYNC handshake.
	FetchRDB func(ctx context.Context, t Target) ([]byte, error)
	// Host is the address clients are told to use; defaults to 127.0.0.1.
	Host string
	// Label names the port reservations, e.g. "memorydb".
	Label string
	// TempDir holds per-group mount directories; defaults to the OS temp dir.
	TempDir      string
	StartTimeout time.Duration
	PollInterval time.Duration
}

Config configures an Engine.

type Dialer

type Dialer func(t Target) Admin

Dialer opens an Admin for t; it does not connect until the first command.

type Endpoint

type Endpoint struct {
	Host string
	Port int
}

Endpoint is a client-reachable node address.

func (Endpoint) Addr

func (e Endpoint) Addr() string

Addr formats the endpoint as host:port.

type Engine

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

Engine launches groups of Redis containers.

func New

func New(cfg Config) *Engine

New returns an Engine; zero Config fields take their defaults.

func (*Engine) CAPEM

func (e *Engine) CAPEM() []byte

CAPEM returns the PEM of the CA that signs node certificates, or nil without one.

func (*Engine) Host

func (e *Engine) Host() string

Host is the address nodes are advertised on.

func (*Engine) Launch

func (e *Engine) Launch(ctx context.Context, spec Spec) (*Group, error)

Launch starts the containers of spec, wires replication or the cluster, and returns once the group serves traffic.

type Group

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

Group is a launched set of containers.

func (*Group) ApplyACL

func (g *Group) ApplyACL(ctx context.Context, policy ACLPolicy) error

ApplyACL syncs policy onto every node; accounts dropped since the previous sync are deleted.

func (*Group) Close

func (g *Group) Close()

Close removes the containers, frees their ports and deletes the mount directory. It is idempotent.

func (*Group) ClusterMode

func (g *Group) ClusterMode() bool

ClusterMode reports whether the group runs Redis Cluster.

func (*Group) Endpoints

func (g *Group) Endpoints() [][]Endpoint

Endpoints lists each shard's nodes in launch order; Primary names the current primary.

func (*Group) Failover

func (g *Group) Failover(ctx context.Context, shard int) error

Failover promotes the shard's first replica to primary and makes the old primary follow it.

func (*Group) Primary

func (g *Group) Primary(shard int) (Endpoint, error)

Primary returns the endpoint currently serving as the shard's primary.

func (*Group) SetReplicas

func (g *Group) SetReplicas(ctx context.Context, shard, replicas int) error

SetReplicas grows or shrinks one shard to the given replica count; extra replicas are the highest numbered.

func (*Group) Snapshot

func (g *Group) Snapshot(ctx context.Context) ([][]byte, error)

Snapshot reads each shard primary's dataset as an RDB file.

func (*Group) TLS

func (g *Group) TLS() bool

TLS reports whether nodes serve TLS.

type Runtime

type Runtime interface {
	CreateAndStart(ctx context.Context, spec container.Spec) (string, error)
	StopAndRemove(ctx context.Context, containerID string) error
	Exec(ctx context.Context, containerID string, cmd []string) (container.ExecResult, error)
}

Runtime is the slice of the container runtime a group needs.

type ShardSpec

type ShardSpec struct {
	// Seed is an RDB file the primary starts with; empty starts blank.
	Seed []byte
	// Slots are the hash slots served by the shard; ignored unless the group is in cluster mode.
	Slots []SlotRange
	// Replicas is the number of replicas of the shard's primary.
	Replicas int
}

ShardSpec describes one shard: its slots, replica count and optional RDB to start from.

type SlotRange

type SlotRange struct {
	Start int
	End   int
}

SlotRange is an inclusive range of hash slots.

func ParseSlots

func ParseSlots(s string) ([]SlotRange, error)

ParseSlots parses "0-5461" or a comma or space separated list such as "0-100,200-300,500".

func (SlotRange) String

func (r SlotRange) String() string

String formats r as "start-end".

type Spec

type Spec struct {
	// Engine is "redis" (default) or "valkey".
	Engine string
	// Version is the engine version ("7.1", "7.2.4"); only major.minor selects the image.
	Version string
	// TLSNames are extra certificate names beyond localhost and the advertised host.
	TLSNames []string
	Shards   []ShardSpec
	// ClusterMode runs Redis Cluster; without it the group is one primary and its replicas.
	ClusterMode bool
	// TLS serves TLS only (plaintext port disabled).
	TLS bool
}

Spec describes a group to launch.

type Target

type Target struct {
	TLS      *tls.Config
	Addr     string
	Username string
	Password string
}

Target addresses one node's control endpoint.

Directories

Path Synopsis
Package redisdockertest provides an in-memory container runtime and control connection for exercising redisdocker groups without Docker.
Package redisdockertest provides an in-memory container runtime and control connection for exercising redisdocker groups without Docker.

Jump to

Keyboard shortcuts

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