persist

package
v0.1.113 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 14 Imported by: 1 module, 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 persist holds the rules for the records a mod keeps for each player through the host's persist service: which record types a manifest may declare, and how an update's field operations change a record. A host that serves the service applies updates with Apply, so every host agrees on what an update does and what it refuses.

Index

Constants

View Source
const (
	// LockFile is the exclusive process lock in a store directory.
	LockFile = "lock"
	// HolderFile names the process holding LockFile, without arguments.
	HolderFile = "holder"
)
View Source
const (
	// TypeLimit bounds the record types one mod declares.
	TypeLimit = 8
	// FieldLimit bounds the fields of one record type.
	FieldLimit = 32
	// RecordBytes bounds one record's encoded values.
	RecordBytes = 8 << 10
	// OpLimit bounds the operations of one update.
	OpLimit = 32
	// ManyLimit bounds the players one GetMany reads.
	ManyLimit = 16
	// RecentKeys is how many update keys a record remembers.
	RecentKeys = 64
	// UpdateKeyBytes bounds one update key.
	UpdateKeyBytes = 64
	// TagLimit bounds the members of one tags field.
	TagLimit = 256
	// TagBytes bounds one tag.
	TagBytes = 32
	// StringBytes is a string field's bound when it declares none.
	StringBytes = 64
	// StringLimit bounds what a string field may declare.
	StringLimit = 256
)
View Source
const Dir = ".modlock/persist"

Dir is the directory of a mod project that a local host keeps the mod's records in, one file per player.

View Source
const FileBytes = 6 * (TypeLimit*(RecordBytes+RecentKeys*(UpdateKeyBytes+2)) + 16<<10)

FileBytes bounds one player's JSON file: every type's binary record bound, its recent keys and envelope, allowing six JSON bytes for each source byte when strings and keys contain control characters.

View Source
const Service = "persist"

Service is the name mods call the persist service by.

Variables

This section is empty.

Functions

func Answer

func Answer(store *Store, manifest *wasm.Manifest, players Players, method string, payload []byte) ([]byte, error)

Answer answers one call of the persist service from the mod manifest describes: its record types, its players and the store of its records. It returns the reply's bytes, or a Refusal for a call that breaks a rule or a bound. A call reaches a player's record only when it names the generation of the connection in the slot, so a Player a mod kept after its player left cannot reach the next player in the slot.

func Apply

func Apply(recordType *persistpb.RecordType, stored *persistpb.StoredRecord, ops []*persistpb.Op, updateKey string) (*persistpb.StoredRecord, bool, error)

Apply returns stored, a record of recordType as a host keeps it or nil for one never written, changed by ops. An update whose key is among the record's recent keys is left out, and Apply reports it repeated with the record unchanged. It refuses the whole update, changing nothing, when an op breaks its field's rules or the result passes a bound. stored is not modified.

func CheckKey

func CheckKey(key string) error

CheckKey refuses a record key: each player has one record of each type until hosts set a bound for keyed records.

func Find

func Find(manifest *wasm.Manifest, name string) *persistpb.RecordType

Find returns the record type named name that manifest declares, or nil.

func Validate

func Validate(manifest *wasm.Manifest) error

Validate checks the record types manifest declares, and that it names the persist service exactly when it declares record types.

Types

type Players

type Players func(slot int32) (steamID uint64, generation uint32, ok bool)

Players resolves a server slot to the SteamID64 of the player in it and the generation of their connection, and reports false for an empty slot. A host that does not learn generations reports zero, and every call naming that slot is refused: without the slot's generation a request made for a player who left would reach the next one.

type Refusal

type Refusal string

Refusal is the error of a call a host refuses: the call broke a rule of its record type or a bound, and the record is unchanged. Its text tells the mod's author which.

func (Refusal) Error

func (r Refusal) Error() string

Error returns the refusal's text.

type Store

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

Store keeps one mod's records in a directory, one file per player, so they survive a restart of the host. Each update reads the player's file, applies its ops and replaces the file whole, one update at a time, so two updates of one record at once both land.

func Open

func Open(dir string) (*Store, error)

Open returns the store of the records kept in dir, creating dir, and holds dir's lock until this process exits. Within one process each directory has one store. It refuses a directory another process holds, naming that process, so two hosts never write one mod's records at once.

func (*Store) Dir

func (s *Store) Dir() string

Dir returns the directory the store keeps its files in.

func (*Store) Get

func (s *Store) Get(steamID uint64, recordType *persistpb.RecordType) (*persistpb.Record, error)

Get returns the player steamID's record of recordType, empty for one never written.

func (*Store) Update

func (s *Store) Update(steamID uint64, recordType *persistpb.RecordType, ops []*persistpb.Op, updateKey string) (*persistpb.Record, bool, error)

Update applies ops to the player steamID's record of recordType, as Apply does, and keeps the result. It returns the record after the update and whether the update's key made it a repeat.

Jump to

Keyboard shortcuts

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