Documentation
¶
Overview ¶
Package lease implements distributed-coordination fenced leases on top of any object store that honors conditional writes.
Each coordination key is one small JSON object holding the owner, fencing token, and expiry. Acquisition creates the object with IfAbsent or replaces an expired or released lease with IfMatchVersion; renewal and release are IfMatchVersion compare-and-swaps against the version written by the holder. Release keeps the object in a released state instead of deleting it, so the next acquisition continues the per-key token sequence and fencing tokens increase strictly across release and expiry.
Expiry is evaluated against the clocks of the processes involved. Clocks must agree to well within one TTL; protected resources must still check fencing tokens, which stay correct under skew and pauses.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backend ¶
type Backend struct {
// contains filtered or unexported fields
}
Backend grants leases stored as objects in one bucket under one prefix. It and the leases it grants are safe for concurrent use.
func New ¶
func New(store objectstore.Store, bucket objectstore.Bucket, prefix string, options Options) (*Backend, error)
New constructs a lease backend. Lease objects are stored at prefix + "/" + AcquireRequest.Key, or at the request key itself when prefix is empty.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"time"
coordination "github.com/justusschock/distributed-coordination"
"github.com/justusschock/object-storage/backend/memory"
"github.com/justusschock/object-storage/lease"
)
func main() {
backend, err := lease.New(memory.New(), "coordination", "leases", lease.Options{})
if err != nil {
panic(err)
}
ctx := context.Background()
held, err := backend.Acquire(ctx, coordination.AcquireRequest{Key: "jobs/nightly", Owner: "worker-1", TTL: time.Minute})
if err != nil {
panic(err)
}
_, err = backend.Acquire(ctx, coordination.AcquireRequest{Key: "jobs/nightly", Owner: "worker-2", TTL: time.Minute})
fmt.Println(held.Info().Token, errors.Is(err, coordination.ErrNotAcquired))
if err := held.Release(ctx); err != nil {
panic(err)
}
}
Output: 1 true
func (*Backend) Acquire ¶
func (backend *Backend) Acquire(ctx context.Context, request coordination.AcquireRequest) (coordination.Lease, error)
Acquire grants the lease when it is absent, released, or expired. With Wait it polls until then or until ctx ends.
type Options ¶
type Options struct {
// Clock supplies time for expiry and polling; nil uses the system clock.
Clock coordination.Clock
// PollInterval bounds how long a waiting Acquire sleeps before
// re-reading a held lease; zero means one second. Waiters also wake at
// the holder's expiry when that comes sooner.
PollInterval time.Duration
}
Options configures a Backend.