state

package
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: Apache-2.0 Imports: 15 Imported by: 0 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 state is the engine's one writer for a repo's store: every change to ideas, pursuits, plans, intents and timers is a command applied one at a time in one transaction that appends its event. Reads come from the state tables; events are never replayed to answer one. Outside work runs as an intent off the loop, and its kind's Record stores what it found with its intent.done; a timer kind can be ensured on open.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrClosed is returned by Submit once the State is closed.
	ErrClosed = errors.New("the repo's state is closed")
	// ErrNoEvent refuses a command that changed state rows and appended no event.
	ErrNoEvent = errors.New("the command changed state and appended no event; nothing was written")
)

Functions

This section is empty.

Types

type Command

type Command func(tx *Tx) error

Command is one state change, run on the loop inside one write transaction. It uses only its Tx: calling Submit, Events or Close would wait on itself.

type Event

type Event struct {
	At            time.Time
	Kind, Subject string
	SubjectID     int64
	Person        string
	Data          json.RawMessage
}

Event is one row of the events table.

type Intent

type Intent struct {
	ID       int64
	Kind     string
	Payload  json.RawMessage
	Attempts int
}

Intent is one pending intent, as its kind's Do sees it.

type IntentKind

type IntentKind struct {
	Name   string
	Repeat string // required
	Check  func(payload json.RawMessage) error
	Do     func(ctx context.Context, in Intent) (result string, err error)
	Record func(tx *Tx, in Intent, result string) error
}

IntentKind names a kind of intent. Repeat says, in one sentence, why carrying it out twice is safe. Check, if set, refuses a bad payload when the intent is written. Do runs off the loop, under a ctx that Close ends. Record, if set, stores what a successful Do found, in the transaction that marks the intent done; if it fails, nothing is kept and Do counts as failed.

type Options

type Options struct {
	StoreDir string
	Now      func() time.Time
	Timers   []TimerKind
	Intents  []IntentKind
}

Options configures a State. Now defaults to time.Now. Timers and Intents hold extra kinds (a TimerKind's Key is ensured on open; an IntentKind's Record keeps what Do found); retention and log.append are always present.

type State

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

State is the one writer for a store's engine tables: a loop that applies commands and fires due timers one at a time, and a worker for intents.

func Open

func Open(st *store.Store, o Options) (*State, error)

Open ensures the keyed timers, loads the next due time and the pending count, and starts the loop and the worker. Close it before the store.

func (*State) Close

func (s *State) Close(ctx context.Context) error

Close stops taking commands and cuts short the intent in hand; it returns once both have stopped, or with ctx's error.

func (*State) Events

func (s *State) Events(ctx context.Context, limit int) ([]Event, error)

Events returns the newest events first (by id). limit is clamped to 1..500; 0 is 50.

func (*State) NextTimer

func (s *State) NextTimer() (time.Time, bool)

NextTimer returns the earliest due time of the timers this engine can fire, from memory. Timers of a kind it does not register are left out.

func (*State) PendingIntents

func (s *State) PendingIntents() int

PendingIntents counts, from memory, the pending intents of registered kinds.

func (*State) Submit

func (s *State) Submit(ctx context.Context, c Command) error

Submit hands c to the loop and waits for its commit. A command error or panic rolls back and is returned. After Close, or if ctx ends before the loop took c, nothing is applied; once taken, c is applied.

type Timer

type Timer struct {
	ID        int64
	Kind, Key string
	Due       time.Time
	Payload   json.RawMessage
}

Timer is one row of the timers table, as its kind's Fire sees it.

type TimerKind

type TimerKind struct {
	Name  string
	Fire  func(tx *Tx, t Timer) error
	Key   string
	Delay time.Duration
}

TimerKind names a kind of timer and what the loop does when one is due. Fire runs as a command: it may set the same (kind, key) again with tx.SetTimer; if it does not, the row is deleted in the same transaction. With a Key, Open ensures that timer, due within Delay (0 is at once).

type Tx

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

Tx is a command's view of its transaction: state rows, the event, intents, timers and the clock. It is refused once the command has returned.

func (*Tx) DeleteTimer

func (tx *Tx) DeleteTimer(kind, key string) error

DeleteTimer removes the timer (kind, key), if it exists.

func (*Tx) Event

func (tx *Tx) Event(kind, subject string, subjectID int64, person string, data any) error

Event appends the command's event, at the command's time. A subjectID of 0 and a person of "" are stored as NULL. data is a JSON object; nil is {}.

func (*Tx) Exec

func (tx *Tx) Exec(query string, args ...any) (sql.Result, error)

Exec runs a statement. A write that changed a row needs an Event.

func (*Tx) Intent

func (tx *Tx) Intent(kind string, payload any) (id int64, err error)

Intent writes a pending intent, due now, in the command's transaction. The kind must be registered and its Check must accept the payload; nil is {}.

func (*Tx) Now

func (tx *Tx) Now() time.Time

Now is the command's time, one instant for the transaction.

func (*Tx) Query

func (tx *Tx) Query(query string, args ...any) (*sql.Rows, error)

Query reads rows. A write given to it (RETURNING) counts as a state change.

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...any) *sql.Row

QueryRow reads one row, with Query's rule for a write.

func (*Tx) SetTimer

func (tx *Tx) SetTimer(kind, key string, due time.Time, payload any) error

SetTimer sets the timer (kind, key) to fire at due, replacing its due time and payload. The kind must be registered. A nil payload is {}.

func (*Tx) StoreDir

func (tx *Tx) StoreDir() string

Directories

Path Synopsis
Package evidence is the engine's record of engineering evidence: a durable poll that observes version control, and the forge when there is one, and keeps each fact once in the store, with the facts' retention and their reads.
Package evidence is the engine's record of engineering evidence: a durable poll that observes version control, and the forge when there is one, and keeps each fact once in the store, with the facts' retention and their reads.
Package plan is the engine's commands and reads on a pursuit's plan and its progress: each revision kept with its reason, which change a session works on, tokens spent added and checked against the appetite in the same transaction, the circuit breaker, and the Needs you items they raise; each change one command with its event.
Package plan is the engine's commands and reads on a pursuit's plan and its progress: each revision kept with its reason, which change a session works on, tokens spent added and checked against the appetite in the same transaction, the circuit breaker, and the Needs you items they raise; each change one command with its event.
Package work is the engine's commands and reads on ideas and pursuits: each change one command in one transaction with its event.
Package work is the engine's commands and reads on ideas and pursuits: each change one command in one transaction with its event.

Jump to

Keyboard shortcuts

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