redisproto

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: MIT Imports: 10 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 redisproto is what the segment providers for a store that speaks the Redis protocol share: the layout of a segment in the store (a set of members, or one key per member), the default keys, the options of a segment, the failure classes of a call and the counters that observe it, the plan of the probes of Init, and the limits and error values of the snapshot reads (SSCAN and the version key).

It names no client library. The providers redissegment (go-redis) and valkeysegment (valkey-go) keep only what depends on their client: the commands, the replies they decode and the errors of the client that they class as transport errors. The segment admin tools use the layout and the default keys from here, so that what they write is what the providers read.

Index

Constants

View Source
const (
	// DefaultTimeout is the per-call timeout when Options.Timeout is zero.
	DefaultTimeout = 100 * time.Millisecond
	// DefaultKeyPrefix is the prefix of the keys of the segments when Options.KeyPrefix is empty.
	DefaultKeyPrefix = "semaphore:segment:"
	// DefaultMaxIDBytes is the longest id that is looked up when Options.MaxIDBytes is zero.
	DefaultMaxIDBytes = 512
	// MaxIDsPerCommand is the most ids that one SMISMEMBER (or script) carries: a segment with more ids in a call is
	// several commands in the same pipeline.
	MaxIDsPerCommand = 1000
)
View Source
const (
	// MinSocketTimeout is the least positive read, write or dial timeout of a go-redis client that is accepted.
	// go-redis runs the handshake that opens a connection (HELLO and the init pipeline) under the read and write
	// timeouts, and the dial, TLS handshake included, under the dial timeout, and has no separate handshake
	// timeout: the bound is one second so that a value that is accepted does not fail the connection on a link with
	// ordinary latency (TLS, another region, a loaded host).
	MinSocketTimeout = time.Second
	// MinConnWriteTimeout is the least ConnWriteTimeout of a valkey-go client that is accepted. valkey-go applies the
	// timeout to every command of a connection, the commands of its handshake (HELLO, CLIENT SETINFO) included, and
	// has no separate handshake timeout: the bound is one second so that a value that is accepted does not fail the
	// handshake on a link with ordinary latency (TLS, another region, a loaded host).
	MinConnWriteTimeout = time.Second
	// MaxConnWriteTimeout is the most ConnWriteTimeout that is accepted.
	MaxConnWriteTimeout = time.Minute
)

The bounds of the socket timeouts of the clients of the Redis-protocol providers. The bootstrap file checks its settings against them, so they are here and not in a provider.

View Source
const (
	CommandGet   = "GET"
	CommandSScan = "SSCAN"
)

Command names of the snapshot reads, as the providers send them.

View Source
const MaxInitProbes = 16

MaxInitProbes is the most namespaces that Init probes, the set and the keys layouts together: a provider with segments in more namespaces checks the first MaxInitProbes of them by name, so startup stays one small pipeline.

View Source
const MaxScanPages = 1 << 20

MaxScanPages is the most pages that one scan may take: a store that keeps answering pages (a cursor loop) fails the scan instead of running for ever. At a COUNT of 1000 it is a set of more than a billion members.

View Source
const ProbeID = "init-probe"

ProbeID is the id that the probes of Init look up.

View Source
const ScanCount = 1000

ScanCount is the COUNT hint of each SSCAN page of a snapshot read.

View Source
const ScanReplyLen = 2

ScanReplyLen is the length of the reply of SSCAN: the cursor and the list.

View Source
const VersionPrefix = "v:"

VersionPrefix is put before a value that was read from a version key, so that no stored value can be equal to [segment.AbsentVersion], the token of a version key that does not exist.

Variables

View Source
var (
	ErrScanShape    = errors.New("a scan reply is not a cursor and a list of strings")
	ErrScanCursor   = errors.New("the cursor of a scan reply is not a number")
	ErrCursorLoop   = errors.New("the scan returned the cursor that it was sent")
	ErrTooManyPages = errors.New("the scan took more pages than the limit")
)

The reasons that a page of a scan is refused. The providers wrap them in their ErrProtocol.

Functions

func CallerDeadline

func CallerDeadline(ctxErr error) bool

CallerDeadline tells whether the error of the context of the caller is a deadline that passed (and not a cancel). The class of such a call is still ClassCanceled; the error says which of the two it was.

func DefaultKey

func DefaultKey(prefix, name string, layout Layout) string

DefaultKey is the key of a segment whose definition names none: <prefix>{<name>} for a set (the name is a hash tag, so a temporary key with the same tag lands in the same cluster slot and RENAME works) and <prefix><name>: for the keys layout. An empty prefix means DefaultKeyPrefix. The providers and the tools that write members use it.

func IsTransportErr

func IsTransportErr(err error, extra ...error) bool

IsTransportErr tells whether err is the end of the connection or of the context, or a failure to get one: EOF, a reset, a closed or timed out socket, a dial error, a cancelled or expired context. The sentinels of the client library that mean the same (a closed client, a pool that is exhausted) are the extra values, which a provider passes: a parse error or any other protocol error is not a transport error.

func IsWrongTypeReply

func IsWrongTypeReply(text string) bool

IsWrongTypeReply reports whether the text of an error reply of the store says that the key holds another kind of value: an error about one set, not of the store (a loading store or a cluster that is down answers other errors).

func ProbeKeys

func ProbeKeys(segs map[string]SegmentOptions) ([]string, []string)

ProbeKeys returns the keys that Init probes. A namespace is the key of a set up to its hash tag, or the prefix of the member keys. Each distinct namespace is probed once, with the key of its first segment by name, and at most MaxInitProbes namespaces are probed (the first ones by namespace and name), sets and keys layouts each. The segments carry their layout and key already (no empty layout, no empty key).

Types

type AnsweredError

type AnsweredError string

AnsweredError is the text of a failure in which the store answered with an error about one set: the store works (see [segment.IsStoreAnswered]). The sentinels ErrServer and ErrProtocol of the providers are of this type.

func (AnsweredError) Error

func (e AnsweredError) Error() string

Error implements error.

func (AnsweredError) StoreAnswered

func (AnsweredError) StoreAnswered() bool

StoreAnswered implements the interface of [segment.IsStoreAnswered].

type Call

type Call struct {
	// Caller is the error of the context of the caller (nil while it runs).
	Caller error
	// Timer is the error of the context of the call itself, the one that holds the timeout of the provider.
	Timer error
	// Closed tells that the provider was shut down.
	Closed bool
}

Call is what a provider knows about the end of a call when it classifies an error.

type Class

type Class uint8

Class is the kind of failure of a call.

const (
	// ClassNone means no failure.
	ClassNone Class = iota
	// ClassCanceled means the call ended by the affair of its caller: its context was cancelled (the client is gone,
	// the process is shutting down) or its deadline passed, or the provider was shut down during the call. The store
	// did nothing wrong: it is neither a timeout nor a failure of the store, and it does not change the health of
	// the provider. The error satisfies [errors.Is] with [context.Canceled] for a cancel and with
	// [context.DeadlineExceeded] for a deadline of the caller.
	ClassCanceled
	// ClassTimeout means the store was too slow: the per-call timeout of the provider passed while the caller was
	// still waiting, or a network timeout. It counts as a failure of the store. A deadline that came from the
	// caller is [ClassCanceled]. The error satisfies [errors.Is] with [context.DeadlineExceeded].
	ClassTimeout
	// ClassUnavailable means the store could not be reached or the connection broke.
	ClassUnavailable
	// ClassServer means the store answered with an error (no permission, wrong key type, loading, cluster down).
	ClassServer
	// ClassProtocol means the store answered with a reply that is not the one the command returns.
	ClassProtocol
)

func Classes

func Classes() []Class

Classes lists the failure classes, ClassNone excluded.

func Classify

func Classify(err error, call Call, reading Reading) Class

Classify returns the class of the error of a call. The rule:

  • the context of the caller ended (cancelled, or its deadline passed, also when both happened), a context of the call was cancelled, or the error is context.Canceled, or the provider was shut down and the error is a lost connection: ClassCanceled. It is no timeout and no failure of the store;
  • a reply that is not the one of the command: ClassProtocol;
  • the timeout of the provider passed while the caller was still waiting, or a network timeout: ClassTimeout;
  • an error reply of the store: ClassServer;
  • anything else (a lost or refused connection): ClassUnavailable.

func (Class) String

func (c Class) String() string

String implements fmt.Stringer.

type Counters

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

Counters is an Observer that counts with atomics. The zero value is ready to use.

func (*Counters) Observe

func (c *Counters) Observe(obs Observation)

Observe implements Observer.

func (*Counters) Snapshot

func (c *Counters) Snapshot() Stats

Snapshot returns the current counts.

type Layout

type Layout string

Layout is how the members of a segment are stored.

const (
	// LayoutSet stores the members in one set. A lookup is SMISMEMBER (Redis 6.2, Valkey 7.2 and later). The
	// lookups of a segment in a call are one SMISMEMBER.
	LayoutSet Layout = "set"
	// LayoutKeys stores one key per member, named <prefix><id>. A lookup is EXISTS.
	LayoutKeys Layout = "keys"
)

type MissingKey

type MissingKey string

MissingKey is what a lookup of a set segment becomes when the key of the set does not exist.

const (
	// MissingUnknown answers Unknown: the segment is not loaded, and the engine applies the failure policy to it. It
	// is an opt-in, fail-closed. An empty set does not exist in Valkey, so an emptied segment is also Unknown.
	MissingUnknown MissingKey = "unknown"
	// MissingNotMember answers NotMember: a missing key is an empty segment. It is the default and saves the EXISTS
	// command.
	MissingNotMember MissingKey = "notMember"
)

type Observation

type Observation struct {
	// Lookups is the number of lookups of the call.
	Lookups int
	// Answered is the number of lookups set to Member or NotMember.
	Answered int
	// Oversized is the number of lookups left Unknown because the id is longer than MaxIDBytes.
	Oversized int
	// Unknown is the number of lookups left Unknown (a failure, an unconfigured segment, a missing key).
	Unknown int
	// RoundTrips is the number of pipelines the call sent: 0 when nothing was sent, 1 usually, 2 when the store
	// answered NOSCRIPT and the script was sent again with its text.
	RoundTrips int
	// Duration is the time of the call, read from the clock package.
	Duration time.Duration
	// Failure is the class of the failure, ClassNone when the call returned no error.
	Failure Class
}

Observation describes one call of Contains. It holds counts only, never an id or a key.

type Observer

type Observer interface {
	Observe(Observation)
}

Observer receives one Observation per call of Contains that reached the provider: a call with no lookup and a call after Shutdown return before it and are not observed. It is called on the goroutine of the caller, so it must be fast and safe for concurrent use. The relay proxy wires its metrics through it.

type Reading

type Reading struct {
	// Protocol: the reply of the store is not the one that the command returns.
	Protocol bool
	// Reply: the store answered with an error reply (no permission, wrong key type, loading, cluster down).
	Reply bool
}

Reading is what the provider found out about an error by looking at its client library.

type SegmentOptions

type SegmentOptions struct {
	// Layout is LayoutSet (default) or LayoutKeys.
	Layout Layout
	// Key is the key of the set (LayoutSet) or the prefix of the member keys (LayoutKeys). Empty means
	// <KeyPrefix>{<name>} for a set (the name is a hash tag, so a temporary key named with the same tag lands in
	// the same cluster slot and RENAME works) and <KeyPrefix><name>: for keys.
	Key string
	// VersionKey (optional) is the key that holds the version of the segment: a string that the writer of the members
	// changes whenever it changes them. The provider's Version reads it with GET; a missing key is the fixed version
	// [segment.AbsentVersion], not an error. Without it Version fails with the provider's ErrNoVersionKey.
	VersionKey string
	// Snapshot makes Init probe the commands of the snapshot mode on this segment: SSCAN of the set, and GET of the
	// version key when there is one. A user that may only run the lookup commands then fails at the start, with an
	// error that names the command, not at the first pull.
	Snapshot bool
}

SegmentOptions says where the members of one segment are.

type SnapshotProbe

type SnapshotProbe struct {
	Name string
	Key  string
}

SnapshotProbe is one probe of the snapshot mode: the command, its key and the name that an error of Init gives.

func PlanProbes

func PlanProbes(
	probeSets, probeKeys []string, segs map[string]SegmentOptions,
) ([]string, []string, []SnapshotProbe)

PlanProbes splits the cap of MaxInitProbes between the probes of Init: the snapshot probes come first, so a provider with many lookup namespaces cannot crowd out the permission check of a snapshot segment, and the lookup probes take what is left, at least one when there is a lookup namespace.

func SnapshotProbes

func SnapshotProbes(segs map[string]SegmentOptions, budget int) []SnapshotProbe

SnapshotProbes returns the probes of the snapshot mode, each key once, at most budget of them: SSCAN of the set of each segment that is configured for the snapshot, GET of its version key. The segments are taken by name.

type Stats

type Stats struct {
	// Failures counts the calls that failed by class: the store failures. A cancelled call is not among them.
	Failures map[string]uint64
	Calls    uint64
	// Canceled is the number of calls that ended because the caller left (see [ClassCanceled]).
	Canceled uint64
	Lookups  uint64
	Answered uint64
	Unknown  uint64
	// Oversized is the number of lookups left Unknown because the id was too long.
	Oversized  uint64
	RoundTrips uint64
	Duration   time.Duration
}

Stats is a snapshot of the counters of Counters.

Jump to

Keyboard shortcuts

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