Reference / Locks API for Go
Locks API for Go
Every public type, function and constant of the Locks engine in tinyshed.org/tinystore/locks, generated from its source. The Locks guide explains how to use them, and the Bun and Node, Python and Rust pages list the same API.
Fence
type Fence struct {
// contains filtered or unexported fields
}Fence grows with every lease the store gives a lock, 128 bits: hand it to whatever must refuse a late holder, which keeps the greatest it saw.
Fence.Less
func (f Fence) Less(other Fence) boolLess says f came before other.
Fence.String
func (f Fence) String() stringString is the fence in decimal, as the store writes it.
HoldOption
type HoldOption func(*asked)HoldOption is how a hold asks for its key.
InBackground
func InBackground() HoldOptionInBackground asks for a semaphore's place in the background: behind every urgent hold of the key, and among no more than the semaphore's Background places. Nothing promises it a place: while urgent holds keep coming and take the room, it waits, its Wait at most.
Timeout
func Timeout(d time.Duration) HoldOptionTimeout is how long a semaphore's hold may keep its place, from its grant: past it the place goes to the next in line, fn's ctx ends and the hold is ErrLost, as at MaxHold, the shorter of the two counting.
Wait
func Wait(d time.Duration) HoldOptionWait is how long a hold waits in line for its key, 10 s unless given: a key still held past it is ErrInUse.
KeyState
type KeyState struct {
Up bool
DownSince time.Time
LastFailure time.Time
Waiting int
Holding int
Failures int
}KeyState is how a semaphore's key stands now: Up is false from the failure that put the key down until a hold after the pause succeeds, and always true without a Breaker; DownSince and LastFailure are zero when there are none, the last failure kept until a success after it. Failures is the failed holds in a row the breaker counts now, toward its failures: reset by a success, and by a pause with no failure; while the key is down, those that put it down; 0 without a Breaker.
Lease
type Lease struct {
Fence Fence
Waited time.Duration
// contains filtered or unexported fields
}Lease is a holding of a key: a lock's Fence, none for a semaphore's, and how long the hold Waited for it, from its call to the grant, the round trip included.
Lease.Healthy
func (l *Lease) Healthy()Healthy says the service a semaphore's hold guards is healthy, so that its Breaker does not count the hold a failure though fn then returns an error: a 404 that is the caller's, not the service's.
Lock
type Lock struct {
// contains filtered or unexported fields
}Lock is keys held one holder at a time.
OpenLock
func OpenLock(store *tinystore.Store, name string, opts ...Option) (*Lock, error)OpenLock opens a lock: keys held one holder at a time, across every program of the store.
Lock.Hold
func (l *Lock) Hold(ctx context.Context, key any, fn func(ctx context.Context, lease *Lease) error, opts ...HoldOption) errorHold waits for key in line, holds it while fn runs and lets it go. A key still held past the wait is ErrInUse; a lease lost while fn ran ends its ctx, and Hold is ErrLost though fn succeeded, since another holder may have run beside it.
Lock.TryHold
func (l *Lock) TryHold(ctx context.Context, key any, fn func(ctx context.Context, lease *Lease) error) (bool, error)TryHold holds key while fn runs if nobody holds it, and says whether it did.
Option
type Option func(*keys)Option is how a lock or a semaphore opens.
Background
func Background(n int) OptionBackground is how many of a semaphore's key's places background holds may take at once, 1 to its Limit, so that urgent holds always find the rest; every place unless given.
Breaker
func Breaker(failures int, pause time.Duration) OptionBreaker puts a semaphore's key down for pause once failures holds of it failed in a row: every hold is then ErrUnavailable at once. A hold failed when fn returned an error, unless its lease said Healthy, or when it ran past its Timeout or MaxHold; a success resets the count. Once the pause is over, one hold goes through: its success puts the key up, its failure pauses it again. The store's process keeps it in memory, so a restart of that process puts every key up.
Limit
func Limit(n int) OptionLimit is how many may hold a semaphore's key at once, 1 to 1000.
MaxHold
func MaxHold(d time.Duration) OptionMaxHold is how long a lease lasts from its grant, however it is renewed: past it the next in line holds the key, and the holder's ctx ends.
TTL
func TTL(d time.Duration) OptionTTL is how long a lease lasts from its last renewal, 1 s to 1 hour, 10 s unless given; this program renews it at a third of it while the function runs.
Semaphore
type Semaphore struct {
// contains filtered or unexported fields
}Semaphore is keys held by up to their limit at a time.
OpenSemaphore
func OpenSemaphore(store *tinystore.Store, name string, opts ...Option) (*Semaphore, error)OpenSemaphore opens a semaphore: each key held by up to Limit holders at once.
Semaphore.Hold
func (s *Semaphore) Hold(ctx context.Context, key any, fn func(ctx context.Context, lease *Lease) error, opts ...HoldOption) errorHold waits for one of key's places in line and holds it while fn runs, as a lock's Hold does; InBackground and Timeout say in which line it waits and how long it may keep its place:
err = cabinet.Hold(ctx, "requests", sync, locks.InBackground(), locks.Timeout(15*time.Second))Semaphore.State
func (s *Semaphore) State(ctx context.Context, key any) (KeyState, error)State says how key stands now: up or down and since when, its last failed hold, and how many wait for it and hold it.
Semaphore.TryHold
func (s *Semaphore) TryHold(ctx context.Context, key any, fn func(ctx context.Context, lease *Lease) error, opts ...HoldOption) (bool, error)TryHold holds one of key's places while fn runs if one is free, and says whether it did; a Wait among opts is passed by, since it waits for none.