awareness

package
v1.0.24 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package awareness implements bounded, ephemeral presence state for a collaboration group.

Awareness is deliberately separate from durable CRDT state: it must never be checkpointed, included in a replica.Frontier, or used as an authorization decision. Each actor owns a monotonically increasing clock. A removal keeps its clock as an in-memory tombstone so a delayed older update cannot bring a disconnected actor back online.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidOptions = errors.New("awareness: invalid options")
	ErrInvalidActor   = errors.New("awareness: invalid actor")
	ErrInvalidState   = errors.New("awareness: invalid state")
	ErrInvalidUpdate  = errors.New("awareness: invalid update")
	ErrResourceLimit  = errors.New("awareness: resource limit exceeded")
	ErrStateConflict  = errors.New("awareness: conflicting update clock")
	ErrClockExhausted = errors.New("awareness: actor clock exhausted")
)

Functions

This section is empty.

Types

type Options

type Options struct {
	MaxActors     int
	MaxActorBytes int
	MaxStateBytes int
	Timeout       time.Duration
}

Options sets the resource and liveness boundaries for one awareness group. MaxStateBytes covers a single JSON object, not the aggregate application session. Timeout is evaluated by ActiveAt; applications should publish a strictly newer heartbeat before it elapses.

func DefaultOptions

func DefaultOptions() Options

DefaultOptions returns conservative limits for UI presence such as names, colours, selections, and small cursor metadata.

type Store

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

Store accepts locally created and remotely received updates. It is safe for concurrent UI, timer, and transport goroutines. Its state is intentionally process-local and must not be persisted with CRDT data.

func NewStore

func NewStore(options Options) (*Store, error)

NewStore constructs an empty bounded awareness store.

func (*Store) ActiveAt

func (store *Store) ActiveAt(now time.Time) []Update

ActiveAt returns sorted, owned copies of the currently live presence states. Offline tombstones remain retained internally until the application drops the whole ephemeral Store, preventing old packets from reviving an actor.

func (*Store) Apply

func (store *Store) Apply(update Update, now time.Time) (changed bool, err error)

Apply installs a newer remote update. Duplicates and stale updates are harmless and return changed=false. A different payload at an equal clock is rejected so arrival order cannot determine a user's displayed presence.

func (*Store) Options

func (store *Store) Options() Options

Options returns the immutable store configuration.

func (*Store) Remove

func (store *Store) Remove(actor string, now time.Time) (Update, error)

Remove creates and installs the next removal update for actor. It is useful on a graceful disconnect; abrupt disconnects are handled by ActiveAt's TTL.

func (*Store) Set

func (store *Store) Set(actor string, state []byte, now time.Time) (Update, error)

Set creates and installs the next online update for actor. The caller should publish the returned update and periodically call Set again as a heartbeat.

type Update

type Update struct {
	Actor string
	Clock uint64
	State []byte
}

Update is one actor's complete ephemeral state at Clock. A nil State is a removal. State is an opaque canonical JSON object so the owning application can define fields without making presence a durable document schema.

func Normalize

func Normalize(update Update, options Options) (Update, error)

Normalize returns a copied update whose online JSON state has a deterministic representation. It reserves nil for a removal and rejects every other JSON top-level value, keeping presence fields namespaced beneath an object.

func UnmarshalUpdate

func UnmarshalUpdate(data []byte, options Options) (Update, error)

UnmarshalUpdate decodes one exact, bounded awareness-v1 update. It performs all limits and JSON validation before allocating retained state.

func (Update) MarshalBinary

func (update Update) MarshalBinary() ([]byte, error)

MarshalBinary serializes a self-contained awareness-v1 update using bounded canonical varints. It authenticates nothing; transports must authorize the actor against their authenticated peer before relaying it.

func (Update) MarshalBinaryWithOptions

func (update Update) MarshalBinaryWithOptions(options Options) ([]byte, error)

MarshalBinaryWithOptions serializes update after validating and canonicalizing its actor and optional JSON object.

func (Update) Online

func (update Update) Online() bool

Online reports whether update carries a presence object rather than a removal tombstone.

Jump to

Keyboard shortcuts

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