lock

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package lock provides file-based locking for the gitbak application.

This package implements a simple file-based locking mechanism to ensure that only one instance of gitbak runs for a given repository at a time. It creates lock files containing the process ID and handles cleanup to prevent stale locks.

Core Components

  • Locker: Main type that manages lock files

Features

  • Repository-specific lock files
  • Process ID tracking to identify lock ownership
  • Stale lock detection and cleanup
  • Clean error messages for lock conflicts

Usage

Basic usage pattern:

// Create a new file lock for a repository
lock, err := lock.New("/path/to/repo")
if err != nil {
    // Handle error
}

// Acquire the lock
if err := lock.Acquire(); err != nil {
    // Handle lock acquisition failure
    // Often means another instance is running
}

// Use the locked resource
// ...

// Release the lock when done
defer lock.Release()

Lock Files

Lock files are created in the system's temporary directory with a name derived from the repository path. Each lock file contains the process ID of the locking process to facilitate ownership verification and cleanup.

The lock file path follows the pattern:

/tmp/gitbak-<repo-hash>.lock

Where <repo-hash> is a hash of the repository's absolute path.

Cleanup

Lock files are automatically removed when the lock is released or when the owning process terminates normally. In case of abnormal termination, subsequent instances will detect and clean up stale locks.

Thread Safety

The FileLock type is not designed to be used concurrently by multiple goroutines. A single instance should only be accessed from one goroutine at a time.

System Requirements

This package relies on the ability to create and write to files in the system's temporary directory. It requires:

  • Write permissions to the temporary directory
  • A filesystem that supports exclusive file creation
  • OS-level process ID information

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Locker

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

Locker prevents concurrent gitbak instances using file locks

func New

func New(repoPath string) (*Locker, error)

New creates a Locker for the specified repository path

func (*Locker) Acquire

func (l *Locker) Acquire() error

Acquire tries to acquire the lock

func (*Locker) Release

func (l *Locker) Release() error

Release releases the lock if it was acquired

Jump to

Keyboard shortcuts

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