Reference / Locks API for Go
On this page

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

go
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

go
func (f Fence) Less(other Fence) bool

Less says f came before other.

Fence.String

go
func (f Fence) String() string

String is the fence in decimal, as the store writes it.

HoldOption

go
type HoldOption func(*asked)

HoldOption is how a hold asks for its key.

InBackground

go
func InBackground() HoldOption

InBackground 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

go
func Timeout(d time.Duration) HoldOption

Timeout 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

go
func Wait(d time.Duration) HoldOption

Wait is how long a hold waits in line for its key, 10 s unless given: a key still held past it is ErrInUse.

KeyState

go
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

go
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

go
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

go
type Lock struct {
	// contains filtered or unexported fields
}

Lock is keys held one holder at a time.

OpenLock

go
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

go
func (l *Lock) Hold(ctx context.Context, key any, fn func(ctx context.Context, lease *Lease) error, opts ...HoldOption) error

Hold 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

go
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

go
type Option func(*keys)

Option is how a lock or a semaphore opens.

Background

go
func Background(n int) Option

Background 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

go
func Breaker(failures int, pause time.Duration) Option

Breaker 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

go
func Limit(n int) Option

Limit is how many may hold a semaphore's key at once, 1 to 1000.

MaxHold

go
func MaxHold(d time.Duration) Option

MaxHold 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

go
func TTL(d time.Duration) Option

TTL 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

go
type Semaphore struct {
	// contains filtered or unexported fields
}

Semaphore is keys held by up to their limit at a time.

OpenSemaphore

go
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

go
func (s *Semaphore) Hold(ctx context.Context, key any, fn func(ctx context.Context, lease *Lease) error, opts ...HoldOption) error

Hold 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

go
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

go
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.

Generated from the source Updated 27 November 2026

esc
↑↓ navigate ↵ open ⇥ filter 0 results