lease
import "github.com/barevoidco/zelk/core/lease"Package lease gives one driver at a time the right to write a session.
zelk never assumes it is the only process: exclusivity comes from a Lease the host supplies. A Grant carries a Token, whose fencing number grows with every new holder, so a store can refuse writes from a holder that has since lost the lease, and a TTL, after which another holder may take the lease over unless it was renewed. The rules are in docs/design/durability.md (“Leases and stores”).
Local coordinates drivers within one process; lease/file coordinates processes on one machine. Every implementation passes lease/leasetest.
Constants
Section titled “Constants”const DefaultTTL = 30 * time.SecondDefaultTTL is how long a grant lasts when a lease isn’t told otherwise.
Variables
Section titled “Variables”var ( // ErrHeld is returned by Acquire when another holder has the lease. ErrHeld = errors.New("lease held by another driver") // ErrLost is returned when a token no longer holds its lease: it was // released, it expired, or a newer holder has taken over. ErrLost = errors.New("lease lost"))type Grant struct { Token Token TTL time.Duration}Grant is a lease granted or renewed: the token, and how long the grant lasts from when the lease recorded it. A holder times the grant on its own clock, from just before it asked (docs/design/durability.md, L3), so the two clocks never need to agree.
HeldError
Section titled “HeldError”type HeldError struct { Session string Expires time.Time}HeldError is the error Acquire returns when another holder has the lease. It wraps ErrHeld, and says when the holder’s grant ends unless renewed, so a host can tell the user the session is open elsewhere and for how long. Expires is read from the lease’s own records and clock: a holder that keeps renewing keeps moving it.
HeldError.Error
Section titled “HeldError.Error”func (e *HeldError) Error() stringHeldError.Unwrap
Section titled “HeldError.Unwrap”func (e *HeldError) Unwrap() errorUnwrap returns ErrHeld.
type Lease interface { // Acquire takes the session's lease, or returns a *HeldError, which // wraps ErrHeld, if another holder's grant hasn't expired. Every grant to a new holder gets a // fence greater than any the session has had. Acquire(ctx context.Context, session string) (Grant, error) // Renew extends the grant t holds, or returns ErrLost if t no longer // holds the lease: released, expired or taken over. Renew(ctx context.Context, t Token) (Grant, error) // Release gives up the lease held by t, or returns ErrLost if t no // longer holds it. Release(ctx context.Context, t Token) error}Lease grants exclusive write access to sessions. Implementations must be safe for concurrent use, and pass lease/leasetest.
type Local struct { // contains filtered or unexported fields}Local is a Lease that coordinates drivers within one process. It knows nothing of other processes, so two processes that each use a Local can both hold the same session; use it only when one process owns the session’s storage, or use lease/file.
A grant lasts its TTL from when it was made or last renewed; once it has expired, another holder may take the lease over. Local accepts any non-empty session ID; the stores define which IDs are valid (store.ValidateSessionID), and a lease on an ID no store accepts is harmless. The zero value is ready to use, with DefaultTTL and the system clock. A Local is safe for concurrent use.
Example
ctx := context.Background()l, _ := lease.NewLocal(lease.WithTTL(time.Minute))
g, _ := l.Acquire(ctx, "session-1")_, err := l.Acquire(ctx, "session-1")fmt.Println(errors.Is(err, lease.ErrHeld))
_ = l.Release(ctx, g.Token)next, _ := l.Acquire(ctx, "session-1")fmt.Println(next.Token.Fence > g.Token.Fence)// Output:// true// trueOutput:
truetrueNewLocal
Section titled “NewLocal”func NewLocal(opts ...Option) (*Local, error)NewLocal returns a Local configured by opts.
Local.Acquire
Section titled “Local.Acquire”func (l *Local) Acquire(ctx context.Context, session string) (Grant, error)Acquire takes the session’s lease, or returns a *HeldError.
Local.Durable
Section titled “Local.Durable”func (*Local) Durable() boolDurable reports false: a Local’s fences start again at 1 in each process, so it can’t fence a log that outlives the process, which already holds higher fences. zelk.New refuses it with a durable store.
Local.Release
Section titled “Local.Release”func (l *Local) Release(ctx context.Context, t Token) errorRelease gives up the lease held by t.
Local.Renew
Section titled “Local.Renew”func (l *Local) Renew(ctx context.Context, t Token) (Grant, error)Renew extends the grant t holds.
Option
Section titled “Option”type Option func(*Local)Option configures a Local.
WithClock
Section titled “WithClock”func WithClock(now func() time.Time) OptionWithClock sets the clock that times grants. The default is time.Now.
WithTTL
Section titled “WithTTL”func WithTTL(d time.Duration) OptionWithTTL sets how long a grant lasts. The default is DefaultTTL.
type Token struct { Session string `json:"session"` Fence uint64 `json:"fence"`}Token proves that its holder held a session’s lease. Fence grows with every new holder of the same session, so comparing fences tells a current holder from a stale one.