Help us shape the future of Go! Take the 2026 Go Developer Survey

flocklease

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package flocklease is the single-host port.SessionLease: cross-process single-writer enforcement for sessions sharing one machine (one store directory), backed by gofrs/flock advisory locks plus a small per-session record file. It is the lease analogue of the memory store's flock discipline (internal/adapter/memory/store.go), kept a SEPARATE package on purpose — the memory store's "one *Store per dir" invariant is about ONE sentinel guarding ONE document, whereas a per-session lease needs ONE lock file PER session id, so the two do not share a sentinel.

flock gives single-host exclusion AND free crash recovery: when a holder process dies, the OS releases its flock, so a survivor can take over without waiting for the TTL. flock has no TTL of its own, so the lease's owner/token/expiry semantics live in a JSON record file written under the held lock; the TTL covers the cross-HOST case the flock cannot (two machines over a shared NFS/EFS mount, where flock semantics are unreliable). Single-host is the honest guarantee; the multi-host story is the k8s/driver backends.

Layout per session id <safeID> under the lease dir:

  • <safeID>.lock — the STABLE flock sentinel, never renamed.
  • <safeID>.lease.json — {owner, token, expiry}, rewritten atomically under the lock (temp + rename).

<safeID> is a collision-free encoding (a sanitized prefix + a hash suffix) so two distinct ids never share a lock file and thus never falsely contend.

INVARIANT — at most ONE *Lease per dir per process (the composition discipline), mirroring the memory store. Per-id sentinels mean distinct sessions never cross-contend; the per-id flock fd is created per operation and closed before return, so there is no long-lived handle to self-deadlock.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Lease

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

Lease is a single-host port.SessionLease over a lease directory. Each session id gets its own flock sentinel and record file; the per-id flock is acquired and released within a single Acquire/Renew/Release call.

func New

func New(dir string, ttl time.Duration, clock port.Clock) (*Lease, error)

New constructs a single-host lease rooted at dir, creating dir (and parents) if absent. ttl is the lease lifetime (the cross-host fallback bound; flock covers the single-host crash case for free). clock is the wall clock the expiry is computed against. A non-positive ttl defaults to 30s.

func (*Lease) Acquire

func (l *Lease) Acquire(ctx context.Context, id session.SessionID, owner string) (port.Lease, error)

Acquire grants the lease when free/expired/same-owner, writing a fresh record under the exclusive flock; otherwise it returns ErrLeaseHeld. A takeover bumps the token strictly past the prior record's; a same-owner re-acquire keeps it.

func (*Lease) Release

func (l *Lease) Release(ctx context.Context, in port.Lease) error

Release drops the caller's own hold (owner + token match) by writing a tombstone record under the lock; idempotent (a mismatch or absent record is a no-op success). The tombstone keeps the fencing token monotone across release.

func (*Lease) Renew

func (l *Lease) Renew(ctx context.Context, in port.Lease) (port.Lease, error)

Renew extends a held lease (owner + token match, unexpired) under the lock, keeping the token and refreshing the expiry; else ErrLeaseHeld (loss signal).

Jump to

Keyboard shortcuts

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