coord

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 9 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 coord coordinates dockhand processes that share one database (docs/design-v3.md §11): sessions, fenced leases, one serve leader with standbys, and the event journal observers tail.

Every process that runs or watches work opens a Session. A session's death is known directly, since every process is on one Mac: a PID that no longer exists, or that names a process started at another time, is a dead session at once. A heartbeat covers a process that is alive but hung. It is judged only by a coordinator that has itself been awake, so a laptop waking from sleep does not declare every peer dead.

A lease on a resource names its session and a generation. Taking a lease from a dead holder raises the generation, and every write made under a lease checks it first, so a holder that was paused and resumes cannot write on a claim it lost. External calls it makes after resuming are guarded by the providers' idempotency and reconciliation, not by leases.

Index

Constants

View Source
const DefaultHeartbeat = 5 * time.Second

DefaultHeartbeat is how often a session records that it is alive, which the store's migration guard watches for, to tell a process working on the database from one working on the original of a copy.

View Source
const DefaultHungAfter = 2 * time.Minute

DefaultHungAfter is how long a live process may go without a heartbeat before another dockhand may take over its work, and so how recent a heartbeat says a process is working: the one window, which the store's migration guard takes (the architecture review of 5897a82, R3-19).

View Source
const LeaderResource = "leader"

LeaderResource is the lease a repository's leading serve holds.

Variables

View Source
var ErrHeld = errors.New("held by another session")

ErrHeld reports a lease another live session holds.

Functions

This section is empty.

Types

type Coordinator

type Coordinator struct {
	Store      store.Store
	Repository model.RepositoryID
	// Liveness defaults to what the operating system reports.
	Liveness Liveness
	// Now defaults to time.Now.
	Now func() time.Time
	// Heartbeat is how often a session records that it is alive;
	// DefaultHeartbeat when zero.
	Heartbeat time.Duration
	// HungAfter is how long a live process may go without a heartbeat
	// before its leases may be taken; two minutes by default.
	HungAfter time.Duration
}

Coordinator binds coordination to one repository in one store.

func (*Coordinator) Start

func (c *Coordinator) Start(ctx context.Context, kind model.SessionKind, version string) (*Session, error)

Start opens a session for the running process.

func (*Coordinator) StartFor

func (c *Coordinator) StartFor(ctx context.Context, p Process, kind model.SessionKind, version string) (*Session, error)

StartFor opens a session for the given process; Start is the usual way, and this form serves tests and supervisors.

type HeldError

type HeldError struct {
	Resource string
	Holder   model.Session
}

HeldError names the session that holds a lease.

func (*HeldError) Error

func (e *HeldError) Error() string

func (*HeldError) Unwrap

func (e *HeldError) Unwrap() error

type Liveness

type Liveness = procid.Liveness

Process, Liveness, Self, and System are procid's: a process by its PID and start time, which a provider's prefix ownership records too.

type Process

type Process = procid.Process

Process, Liveness, Self, and System are procid's: a process by its PID and start time, which a provider's prefix ownership records too.

func Self

func Self() (Process, error)

Self identifies the running process.

type Session

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

Session is this process's session.

func (*Session) Acquire

func (s *Session) Acquire(ctx context.Context, resource string) (model.Lease, error)

Acquire takes the lease on a resource, from its holder when that holder is dead, and refuses with a HeldError when a live session holds it.

func (*Session) AcquireIn added in v0.8.0

func (s *Session) AcquireIn(tx store.Tx, resource string) (model.Lease, error)

AcquireIn is Acquire in the caller's transaction, so taking a lease and what decides it are one: a check's slot (the engine's reserve).

func (*Session) Beat

func (s *Session) Beat(ctx context.Context) error

Beat records that the session is alive.

func (*Session) Emit

func (s *Session) Emit(tx store.Tx, event model.Event) (int64, error)

Emit appends an event from this session within a transaction, stamping its session and time.

func (*Session) End

func (s *Session) End(ctx context.Context) error

End closes the session cleanly. Leases it still holds become free to take, since an ended session is dead to every judge.

It stops the heartbeat whichever started first, and returns only once a running one has returned, so the caller may close the store after it (#252).

func (*Session) Fenced

func (s *Session) Fenced(ctx context.Context, lease model.Lease, fn func(store.Tx) error) error

Fenced runs fn in a write transaction only while the lease is still current; a lost lease returns store.ErrStale and writes nothing.

func (*Session) Holder

func (s *Session) Holder(ctx context.Context, resource string) (*model.Session, error)

Holder reports the live session holding a resource, without taking it: nil when no one holds it, or its holder is dead.

func (*Session) Holders added in v0.4.0

func (s *Session) Holders(r store.Reader, resources ...string) (map[string]*model.Session, error)

Holders are the live sessions holding resources, read in the caller's transaction, so a list of runs and their holders is one consistent read rather than a View each (SQL-O4); a resource no live session holds has no entry. The liveness judgment is Judge's, as Holder's.

func (*Session) ID

func (s *Session) ID() model.SessionID

ID is the session's identity.

func (*Session) Judge

func (s *Session) Judge(other model.Session) (Verdict, error)

Judge decides whether another session is dead: ended, its process gone or replaced, or, when this session is awake to see it, silent for longer than HungAfter.

func (*Session) KeepAlive

func (s *Session) KeepAlive(ctx context.Context)

KeepAlive beats until ctx is done. A beat that fails is tried again at the next, rather than ending the heartbeat: one write that waited past the store's timeouts once stopped it for good, and two minutes later another session could judge this process hung and take its leases, serve's lead among them (the limits sweep, 2026-10-01). A run of failures is said once, as is the beat that ends it. It ends with the session: bump's check ended its session while the process went on to submit, and the next beat, refused for an ended session, was said as a heartbeat lost (field testing, 2026-10-02).

func (*Session) Lead

func (s *Session) Lead(ctx context.Context) (model.Lease, error)

Lead takes the repository's leadership, or reports the live leader it stands by for.

func (*Session) Record

func (s *Session) Record() model.Session

Record is the session as last written.

func (*Session) Release

func (s *Session) Release(ctx context.Context, lease model.Lease) error

Release frees a lease the session still holds.

func (*Session) TakeIfUnattended

func (s *Session) TakeIfUnattended(ctx context.Context, resource string) (lease model.Lease, holder *model.Session, err error)

TakeIfUnattended takes a resource's lease only when no live session holds it, reporting who does otherwise. This is how a control is applied by whoever can: a cancel whose run has a live holder is left to that holder, and one whose holder is dead is applied by the canceller.

type System

type System = procid.System

Process, Liveness, Self, and System are procid's: a process by its PID and start time, which a provider's prefix ownership records too.

type Verdict

type Verdict struct {
	Dead   bool
	Reason string
}

Verdict is a judgment of another session.

Jump to

Keyboard shortcuts

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