snapshot

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 8 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 snapshot holds the member set of a segment of the snapshot mode: it pulls the members through a segment.SnapshotSource, keeps them in memory behind an atomic pointer and swaps them when the version changes.

Index

Constants

This section is empty.

Variables

View Source
var ErrNilSource = errors.New("snapshot: nil source")

ErrNilSource is returned by New for a nil source.

Functions

This section is empty.

Types

type Class

type Class string

Class is the class of the last failure of a holder. It never holds a member id.

const (
	// ClassNone means the last refresh succeeded.
	ClassNone Class = ""
	// ClassVersion means the version of the segment could not be read.
	ClassVersion Class = "version"
	// ClassRead means the members could not be read.
	ClassRead Class = "read"
	// ClassMissing means the store does not have the segment.
	ClassMissing Class = "missing"
	// ClassTooLarge means the segment is over the memory guard.
	ClassTooLarge Class = "too-large"
	// ClassSet means the store answered with an error about this set (a key of the wrong type, a reply that is not
	// the one of the command): the store works, the set cannot be read.
	ClassSet Class = "set"
	// ClassCanceled means the context ended during the refresh.
	ClassCanceled Class = "canceled"
)

type Holder

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

Holder keeps the member set of one segment. Reads are lock-free; Refresh is driven by the caller (it starts no goroutine and no timer) and calls are serialised.

func New

func New(name string, src segment.SnapshotSource, opts Options) (*Holder, error)

New returns a holder of the segment name, empty until the first successful Refresh.

func (*Holder) Answer

func (h *Holder) Answer(id string) (segment.State, segment.Status)

Answer implements segment.Live: the membership from the held set with the status of the answer, and no store call. A set that was never loaded answers Unknown with a store error; a set older than it should be (the last refresh failed, or the set is over the guard) answers with a stale status; a segment that is missing is an answer, healthy. It does not allocate and does not lock.

func (*Holder) Contains

func (h *Holder) Contains(ctx context.Context, lookups []segment.Lookup) error

Contains implements segment.Provider for the lookups of this segment; others are left alone. It does not allocate.

func (*Holder) Lookup

func (h *Holder) Lookup(id string) segment.State

Lookup answers a membership question from the held set. It does not allocate and does not lock. Before the first pull the answer is Unknown, never NotMember.

func (*Holder) Refresh

func (h *Holder) Refresh(ctx context.Context) error

Refresh reads the version of the segment and, only when it differs from the held one, pulls the members and swaps them in. On an error (the store is down, a timeout, a stream that broke, the memory guard, a malformed reply) the held set stays and keeps answering, and the class is in Status. A pull that completes and finds the segment missing is an answer, not an error: the held set is dropped and the lookups answer the policy (NotMember, or Unknown with MissingUnknown), and Status shows ClassMissing with the version. While the segment is missing, every Refresh whose Version works pulls the members again (the token cannot tell that the segment came back); a Version that reports it missing again pulls nothing. A writer that deletes or replaces a set must change the version: a deletion under an unchanged token is not seen. Refresh returns the missing error once, when it learns it. The version is read before the members, so a write that lands between the two makes the next Refresh pull again.

func (*Holder) SetOverdue

func (h *Holder) SetOverdue(overdue bool)

SetOverdue tells the holder that its set could not be refreshed for longer than the caller allows: the answers are stale (the set is still used) until the caller clears it, which it does when a refresh works. The holder has no clock of its own for this, and the lookups never read one.

func (*Holder) Stale

func (h *Holder) Stale() bool

Stale reports whether the held set is served after a failed refresh: the last known good set, older than it should be. It implements segment.StaleReporter. A segment that is missing is an answer, not a stale set.

func (*Holder) Status

func (h *Holder) Status() Status

Status returns the state of the holder.

type Options

type Options struct {
	// Missing is what a segment that does not exist means: the default (empty) is [segment.MissingNotMember], an empty
	// set; [segment.MissingUnknown] drops the set and answers Unknown.
	Missing segment.MissingKey
	// MaxMembers is the most distinct members; more refuses the pull and keeps the last good set.
	MaxMembers int
	// MaxBytes is the most bytes of the distinct members (the length of the ids), same rule.
	MaxBytes int64
}

Options bounds the memory of one holder. A zero field means the default of the segment package.

type Status

type Status struct {
	// Version is the version of the held set, empty before the first pull.
	Version string
	// LastError is the class of the failure of the last refresh, [ClassNone] when it succeeded.
	LastError Class
	// Members is the size of the held set.
	Members int
	// Age is the time since the held set was pulled.
	Age time.Duration
	// Pulls is the number of member sets pulled and swapped in.
	Pulls uint64
	// Loaded is true once a set is held.
	Loaded bool
}

Status is what a holder reports. It holds no member id.

Jump to

Keyboard shortcuts

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