snapshot

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package snapshot provides typed state snapshots + dirty-field tracking for delta-encoded replication. Producers track per-consumer baselines; each Append yields only the fields changed since that consumer's last acknowledged frame.

Also ships Ring[S], a fixed-size circular buffer indexed by frame number for lag-compensation lookups.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Encoder

type Encoder[S any] interface {
	Encode(state, baseline S, mask Mask, out *bytes.Buffer)
	Decode(in *bytes.Reader, baseline S) (S, Mask, error)
	// Diff returns the mask of fields where state differs from baseline.
	Diff(state, baseline S) Mask
}

Encoder writes a delta of `state` against `baseline` filtered by `mask` into out. Implementations are user-provided per state type.

type Mask

type Mask uint64

Mask is a bitset of dirty fields. Up to 64 fields per state struct.

type Ring

type Ring[S any] struct {
	// contains filtered or unexported fields
}

Ring is a fixed-size circular buffer of state snapshots keyed by frame number. Used by lag compensation for "what did entity look like N frames ago?" queries.

func NewRing

func NewRing[S any](size int) *Ring[S]

NewRing returns a Ring sized to `size` entries. Push wraps around once the buffer is full.

func (*Ring[S]) At

func (r *Ring[S]) At(frame uint32) (S, bool)

At returns the snapshot recorded at the given frame, if present and not overwritten by a later frame in the same slot.

func (*Ring[S]) Push

func (r *Ring[S]) Push(frame uint32, state S)

Push records a state snapshot at the given frame, overwriting any existing entry at the same ring slot.

func (*Ring[S]) Size

func (r *Ring[S]) Size() int

Size returns the ring capacity.

type Tracker

type Tracker[S any] struct {
	// contains filtered or unexported fields
}

Tracker holds per-consumer baselines + reusable encode buffers and emits per-consumer deltas with zero allocations on the steady-state path (the buffer grows once to fit the max delta then is reused).

func NewTracker

func NewTracker[S any](enc Encoder[S]) *Tracker[S]

NewTracker constructs a Tracker.

func (*Tracker[S]) Ack

func (t *Tracker[S]) Ack(consumerID uint64, frame uint32)

Ack records that the consumer has acknowledged a frame. This is a caller-controlled hook; in a strict net-LOD protocol the baseline only advances on Ack, not on Append. The simple Tracker here advances on Append; consumers needing strict Ack-only advancement should add a frame-keyed history layer.

func (*Tracker[S]) Append

func (t *Tracker[S]) Append(consumerID uint64, current S) []byte

Append computes the delta for consumerID and returns the encoded bytes from a per-consumer reusable buffer. The returned slice is valid until the next Append for the same consumerID — callers that need to retain the bytes across calls must copy them.

The current state becomes the new pending baseline; call Ack(consumerID, frame) to commit it.

func (*Tracker[S]) Reset

func (t *Tracker[S]) Reset(consumerID uint64)

Reset clears a consumer's baseline (e.g. on reconnect).

Jump to

Keyboard shortcuts

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