lockfile

package
v0.0.0-...-c480f9e Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package lockfile is a remote-host mutex implemented on top of SFTP.

SFTP doesn't expose POSIX flock, so we use SFTP's open-with-CREATE-and-EXCL to atomically materialize a small JSON file. The file's mtime gates TTL staleness — if a previous deploy crashed and left the lock orphaned past the configured TTL, we steal it (logging loudly).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyHeld = errors.New("lock is already held by another deploy")
)

Errors returned by Acquire.

Functions

func Acquire

func Acquire(ctx context.Context, sc *sftp.Client, path string, ttl time.Duration, force bool) (*Handle, *Lock, bool, error)

Acquire creates the lock file atomically. If the file already exists and its mtime is within ttl, returns ErrAlreadyHeld. If older than ttl, the caller is given the chance to steal via force.

On steal, the existing lock content is returned so the caller can log who originally held it.

Types

type Handle

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

Handle is what Acquire returns. Call Release when done — the deferred Release in a deploy is critical.

func (*Handle) Release

func (h *Handle) Release(ctx context.Context) error

Release deletes the lock file. Idempotent — safe to call twice.

type Lock

type Lock struct {
	HeldBy     string    `json:"held_by"`
	Hostname   string    `json:"hostname"`
	PID        int       `json:"pid"`
	AcquiredAt time.Time `json:"acquired_at"`
}

Lock describes the file written to the remote lock path. It's not security state — it's diagnostic so a human can tell who else is in the middle of deploying.

Jump to

Keyboard shortcuts

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