Documentation
¶
Overview ¶
Package lease provides time-bounded ownership with monotonic fencing tokens.
Index ¶
- Constants
- Variables
- func Wrap(err error, operation string) error
- type Backend
- type Client
- type ClientOptions
- type Clock
- type Event
- type FailureBehavior
- type Handle
- func (handle *Handle) AcquiredAt() time.Time
- func (handle *Handle) Deadline() time.Time
- func (handle *Handle) Owner() string
- func (handle *Handle) Release(ctx context.Context) error
- func (handle *Handle) Renew(ctx context.Context) error
- func (handle *Handle) Snapshot() Record
- func (handle *Handle) StartManaged(ctx context.Context) (*Managed, error)
- func (handle *Handle) State() State
- func (handle *Handle) Token() Token
- func (handle *Handle) Validate(ctx context.Context) error
- type Key
- type Loss
- type Managed
- type Observer
- type ObserverFunc
- type Operation
- type Outcome
- type OwnerSource
- type Policy
- func (policy Policy) FailureBehavior() FailureBehavior
- func (policy Policy) Jitter() time.Duration
- func (policy Policy) MaxAttempts() uint32
- func (policy Policy) OperationTimeout() time.Duration
- func (policy Policy) RenewEvery() time.Duration
- func (policy Policy) Retry() time.Duration
- func (policy Policy) SafetyMargin() time.Duration
- func (policy Policy) TTL() time.Duration
- func (policy Policy) Wait() time.Duration
- type PolicyOptions
- type Record
- type RetrySource
- type Sleeper
- type State
- type Token
Constants ¶
const ( // MaxClientWaiters is the largest configurable concurrent acquisition bound. MaxClientWaiters uint32 = 1_000_000 // MaxClientManaged is the largest configurable managed-renewal bound. MaxClientManaged uint32 = 100_000 )
const ( // MaxTTL bounds one remote lease lifetime. MaxTTL = 24 * time.Hour // MaxWait bounds acquisition wall-clock waiting. MaxWait = time.Hour // MaxAttempts bounds backend operations in one acquisition. MaxAttempts uint32 = 10_000 // MaxOperationTimeout bounds one backend call. MaxOperationTimeout = time.Minute )
const MaxKeyBytes = 256
MaxKeyBytes bounds a complete encoded lease key.
Variables ¶
var ( // ErrContended reports that another owner holds the requested lease. ErrContended = errors.New("lease: contended") // ErrTimeout reports that a bounded acquisition wait elapsed. ErrTimeout = errors.New("lease: timeout") // ErrCanceled reports that the caller canceled an operation. ErrCanceled = errors.New("lease: canceled") // ErrLost reports that a formerly owned lease is no longer safely usable. ErrLost = errors.New("lease: lost") // ErrStaleOwner reports an owner or fencing token that is no longer current. ErrStaleOwner = errors.New("lease: stale owner") ErrBackendUnavailable = errors.New("lease: backend unavailable") // ErrInvalidState reports invalid input or an invalid state transition. ErrInvalidState = errors.New("lease: invalid state") // ErrAmbiguousOutcome reports that a remote mutation may have committed. ErrAmbiguousOutcome = errors.New("lease: ambiguous outcome") )
Functions ¶
Types ¶
type Backend ¶
type Backend interface {
TryAcquire(context.Context, Key, string, time.Duration) (Record, error)
Renew(context.Context, Record, time.Duration) (Record, error)
Validate(context.Context, Record) (Record, error)
Release(context.Context, Record) error
}
Backend atomically persists one fenced lease record per key.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client acquires handles from one lease backend.
func NewClient ¶
func NewClient(backend Backend, options ClientOptions) (*Client, error)
NewClient constructs a lease client with cryptographic production defaults.
type ClientOptions ¶
type ClientOptions struct {
Clock Clock
Owners OwnerSource
Sleeper Sleeper
Retry RetrySource
MaxWaiters uint32
MaxManaged uint32
}
ClientOptions injects deterministic sources used by a lease client.
type FailureBehavior ¶
type FailureBehavior uint8
FailureBehavior defines ownership admission after backend failure.
const ( // FailureFailClosed denies admission after every uncertain operation. FailureFailClosed FailureBehavior = iota + 1 )
type Handle ¶
type Handle struct {
// contains filtered or unexported fields
}
Handle is a concurrency-safe local view of remotely fenced ownership.
func (*Handle) AcquiredAt ¶
AcquiredAt returns the backend-anchored acquisition instant.
func (*Handle) Snapshot ¶
Snapshot returns the latest backend-authenticated ownership record. It is intended for composing protected adapters such as Valkey ownership guards; callers must use State or Validate for local admission decisions.
func (*Handle) StartManaged ¶
StartManaged starts at most one caller-owned renewal goroutine.
type Key ¶
type Key struct {
// contains filtered or unexported fields
}
Key is a validated, namespaced lease identity.
type Managed ¶
type Managed struct {
// contains filtered or unexported fields
}
Managed owns one bounded renewal goroutine for a handle.
type Observer ¶
type Observer interface{ Observe(Event) }
Observer consumes a redacted best-effort event outside backend locks. At most one callback per observer runs at once; events are dropped while busy.
type ObserverFunc ¶
type ObserverFunc func(Event)
ObserverFunc adapts a function to Observer.
func (ObserverFunc) Observe ¶
func (observer ObserverFunc) Observe(event Event)
Observe invokes the adapted observer.
type Operation ¶
type Operation string
Operation is a bounded observation operation name.
const ( // OperationAcquire identifies one acquisition attempt. OperationAcquire Operation = "acquire" // OperationRenew identifies one renewal attempt. OperationRenew Operation = "renew" // OperationValidate identifies one validation attempt. OperationValidate Operation = "validate" // OperationRelease identifies one release attempt. OperationRelease Operation = "release" )
type Outcome ¶
type Outcome string
Outcome is a bounded, identifier-free observation result.
const ( // OutcomeSuccess reports a successful operation. OutcomeSuccess Outcome = "success" // OutcomeContended reports expected acquisition contention. OutcomeContended Outcome = "contended" // OutcomeStale reports a rejected stale owner. OutcomeStale Outcome = "stale" // OutcomeCanceled reports caller cancellation. OutcomeCanceled Outcome = "canceled" OutcomeUnavailable Outcome = "unavailable" // OutcomeAmbiguous reports an uncertain remote mutation. OutcomeAmbiguous Outcome = "ambiguous" // OutcomeInvalid reports invalid input or state. OutcomeInvalid Outcome = "invalid" )
type OwnerSource ¶
OwnerSource creates opaque owner identities.
type Policy ¶
type Policy struct {
// contains filtered or unexported fields
}
Policy is an immutable, bounded acquisition policy.
func NewPolicy ¶
func NewPolicy(options PolicyOptions) (Policy, error)
NewPolicy validates and copies acquisition options.
func (Policy) FailureBehavior ¶
func (policy Policy) FailureBehavior() FailureBehavior
FailureBehavior returns the immutable fail-closed admission policy.
func (Policy) MaxAttempts ¶
MaxAttempts returns the total acquisition attempt bound.
func (Policy) OperationTimeout ¶
OperationTimeout returns the maximum duration of one backend call.
func (Policy) RenewEvery ¶
RenewEvery returns the managed-renewal interval, or zero when disabled.
func (Policy) SafetyMargin ¶
SafetyMargin returns time reserved for delay and uncertainty.
type PolicyOptions ¶
type PolicyOptions struct {
TTL time.Duration
Wait time.Duration
Retry time.Duration
Jitter time.Duration
RenewEvery time.Duration
SafetyMargin time.Duration
MaxAttempts uint32
OperationTimeout time.Duration
FailureBehavior FailureBehavior
}
PolicyOptions configures immutable acquisition and renewal behavior.
type Record ¶
Record is the backend-authenticated ownership snapshot for a lease.
func (Record) SafeDeadline ¶
SafeDeadline returns the backend-clock expiry after reserving margin. Handle.Deadline is the authoritative local admission bound.
type RetrySource ¶
RetrySource supplies deterministic bounded acquisition jitter.
type State ¶
type State uint8
State is the local fail-closed lifecycle state of a lease handle.
const ( // StateActive permits admission before the safety deadline. StateActive State = iota + 1 // StateExpired means the local safety deadline has passed. StateExpired // StateLost means the backend proved ownership is stale. StateLost // StateUncertain means a remote operation had no reliable outcome. StateUncertain // StateReleased means compare-and-release completed successfully. StateReleased )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
postgres
command
Command postgres demonstrates acquiring a PostgreSQL-backed fenced lease.
|
Command postgres demonstrates acquiring a PostgreSQL-backed fenced lease. |
|
protectedwrite
command
Command protectedwrite demonstrates rejecting a stale fencing token.
|
Command protectedwrite demonstrates rejecting a stale fencing token. |
|
valkey
command
Command valkey demonstrates acquiring a Valkey-backed fenced lease.
|
Command valkey demonstrates acquiring a Valkey-backed fenced lease. |
|
internal
|
|
|
failure
Package failure preserves error identity without rendering sensitive causes.
|
Package failure preserves error identity without rendering sensitive causes. |
|
guard
Package guard runs callbacks under fail-closed managed lease ownership.
|
Package guard runs callbacks under fail-closed managed lease ownership. |
|
Package leasequeue integrates fenced leases with queue workers.
|
Package leasequeue integrates fenced leases with queue workers. |
|
Package leasescheduler provides on-one-server and non-overlap execution.
|
Package leasescheduler provides on-one-server and non-overlap execution. |
|
Package leaseservice integrates managed leases with service lifecycle hooks.
|
Package leaseservice integrates managed leases with service lifecycle hooks. |
|
Package leasetest provides deterministic lease conformance utilities.
|
Package leasetest provides deterministic lease conformance utilities. |
|
Package memory provides a deterministic process-local reference backend.
|
Package memory provides a deterministic process-local reference backend. |
|
Package postgres provides native durable fenced leases for PostgreSQL.
|
Package postgres provides native durable fenced leases for PostgreSQL. |
|
Package valkey provides native atomic fenced leases for Valkey.
|
Package valkey provides native atomic fenced leases for Valkey. |