Documentation
¶
Overview ¶
Package file implements coordination.Backend with cross-process leases on a local filesystem directory.
Each key maps to a lock file guarded by an operating-system advisory lock (flock on Unix, LockFileEx on Windows) and a state file that persists the current owner, fencing token, and deadline. The holding process enforces lease expiry, and process death releases the operating-system lock.
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 backed by lock files in one directory.
func NewBackend ¶
NewBackend returns a backend that stores lock files in dir, creating it if necessary.
Example ¶
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
coordination "github.com/justusschock/distributed-coordination"
"github.com/justusschock/distributed-coordination/file"
)
func main() {
dir, err := os.MkdirTemp("", "locks")
if err != nil {
fmt.Println(err)
return
}
defer func() { _ = os.RemoveAll(dir) }()
backend, err := file.NewBackend(filepath.Join(dir, "leases"), file.WithPollInterval(50*time.Millisecond))
if err != nil {
fmt.Println(err)
return
}
// Every process that opens a backend on the same directory competes for
// the same keys; fencing tokens persist in the directory.
for range 2 {
lease, err := backend.Acquire(context.Background(), coordination.AcquireRequest{
Key: "repo",
Owner: fmt.Sprintf("pid-%d", os.Getpid()),
TTL: time.Minute,
Wait: true,
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println("token", lease.Info().Token)
if err := lease.Release(context.Background()); err != nil {
fmt.Println(err)
}
}
}
Output: token 1 token 2
func (*Backend) Acquire ¶
func (b *Backend) Acquire(ctx context.Context, request coordination.AcquireRequest) (coordination.Lease, error)
Acquire grants the lease for request.Key when no other lease handle, in this or another process, holds it.
type Option ¶
type Option func(*config)
Option configures a Backend.
func WithClock ¶
func WithClock(clock coordination.Clock) Option
WithClock sets the clock used for lease timestamps and expiry. A nil clock selects the real clock.
func WithFileMode ¶
WithFileMode sets the permission bits for created lock and state files. The directory, when created, receives the same bits plus matching execute bits.
func WithPollInterval ¶
WithPollInterval sets the maximum real-time delay between attempts of a waiting acquisition. Attempts start at a fraction of the interval and back off exponentially up to it.