file

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: Apache-2.0 Imports: 15 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func NewBackend(dir string, opts ...Option) (*Backend, error)

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

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

func WithFileMode(mode fs.FileMode) Option

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

func WithPollInterval(interval time.Duration) Option

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.

Jump to

Keyboard shortcuts

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