lock

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: GPL-3.0 Imports: 5 Imported by: 1

Documentation

Overview

Package lock makes callers in one process take turns at a record, so one does not undo another's change.

Two edits of one record at once lose one of them: each reads the whole record and sends it back, and the later undoes the earlier. An MCP client runs a turn's calls together, so eight calls each adding a tag left one. Holding the record from the read to the write keeps them apart:

unlock := lock.By(playlist)
defer unlock()

By takes any mix in one call: a record, lock.ID[Item](id) for one known only by id, lock.Field(me, "bookmarks") for a piece of one, lock.String for a thing with no record. A record is locked by its type and id, never by the Go value, so a lockable type says which record it is with LockID() string. All locks everything, for a change that rewrites a whole collection.

By, ByString and All share one set for the process. A process holding several worlds, two servers that each have an item 12, gives each a set of its own (NewSet), or puts the world in the key. Lock once, naming everything needed in that call: the keys are taken in one order so two callers cannot each hold what the other waits for, which a second call while holding the first gives up. These locks hold back nothing outside the process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All() (unlock func())

All locks everything there is or could be: it waits for every other lock to go, and none is granted until it is unlocked.

func By added in v0.5.0

func By(things ...Thing) (unlock func())

By locks every thing named until unlock is called. They are taken in one order, each once, so two callers with overlapping things cannot wait on each other for ever. Naming nothing locks nothing.

func ByString added in v0.5.0

func ByString(key string) (unlock func())

ByString locks the thing known by this string: By(String(key)).

Types

type Key added in v0.5.0

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

Key describes a thing to lock when there is no value in hand: a record by id (ID), a piece of a thing (Field), or a string (String).

func Field added in v0.5.0

func Field(of Thing, name string) Key

Field is a named piece of a thing changed on its own, an account's bookmarks; a separate lock from the thing and its other fields.

func ID added in v0.5.0

func ID[T any](id string) Key

ID is the record of type T with this id, for a caller without the value: the same lock as By(item) when item.LockID() is id.

func String added in v0.5.0

func String(s string) Key

String is a thing known only by a string of the caller's making; no record's lock, whatever it says.

func (Key) LockID added in v0.5.0

func (k Key) LockID() string

LockID is what the key names, written out; a Key is itself a Thing.

type Set added in v0.5.0

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

Set is a set of locks of its own: what is locked in one holds nobody up in another, All included. The zero value is ready to use. A lock is kept only while held or waited for, so a long-running server does not keep one for every record it ever touched.

func NewSet added in v0.5.0

func NewSet() *Set

NewSet returns a set of locks that shares nothing with any other.

func (*Set) All added in v0.5.0

func (s *Set) All() (unlock func())

All locks everything in this set (see All).

func (*Set) By added in v0.5.0

func (s *Set) By(things ...Thing) (unlock func())

By locks every thing named in this set until unlock is called (see By).

func (*Set) ByString added in v0.5.0

func (s *Set) ByString(key string) (unlock func())

ByString locks the thing known by this string, in this set.

func (*Set) Idle added in v0.5.0

func (s *Set) Idle() bool

Idle reports whether nothing is locked or waited for, for a test to see that no call left a lock behind.

type Thing added in v0.5.0

type Thing interface {
	// LockID is the record's id, compared as written: a type whose server reads one id in several spellings folds them to one here.
	LockID() string
}

Thing is what By locks: a value that says which record it is. The lock is on the type and the id, so two copies of one record are one thing and a playlist and a collection both numbered 12 are two.

Jump to

Keyboard shortcuts

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