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.
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
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
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.
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
By locks every thing named in this set until unlock is called (see By).
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.