Search by

adachsoft / lease-lock

Arkadiusz Adach

Owner-bound expiring lease locks with file-backed and in-memory implementations and PSR-20 clock support.

Package info

gitlab.com/a.adach/lease-lock

Issues

pkg:composer/adachsoft/lease-lock

Statistics

Installs: 35

Dependents: 2

Suggesters: 0

Stars: 0

v0.1.0 2026-11-20 11:09 UTC

This package is auto-updated.

Last update: 2026-11-20 10:09:43 UTC


README

adachsoft/lease-lock provides expiring, owner-bound leases for coordinating work. It includes file-backed and in-memory implementations of LeaseLockInterface, with acquisition, renewal, release, and lookup operations.

Features

  • Lease ownership prevents one worker from releasing or renewing another worker's lease.
  • Configurable time-to-live and renewal for long-running work.
  • PSR-20 clock injection for deterministic time handling.
  • File storage coordinated with flock() and a process-local in-memory implementation.

Installation

composer require adachsoft/lease-lock

The package installs adachsoft/lease-lock-contracts and uses psr/clock for its clock interface.

Requirements

  • PHP 8.3 or later
  • Composer

Usage

use AdachSoft\LeaseLock\FileLeaseLock;
use Psr\Clock\ClockInterface;
use DateTimeImmutable;

$clock = new class implements ClockInterface {
    public function now(): DateTimeImmutable
    {
        return new DateTimeImmutable();
    }
};

$lock = new FileLeaseLock(__DIR__ . '/var/leases', $clock);
$lease = $lock->acquire('reports:daily', 'worker-1', 60);

if ($lease !== null) {
    try {
        // Perform the protected operation.
    } finally {
        $lock->release('reports:daily', 'worker-1');
    }
}

See the usage examples for renewal and in-memory usage.

Storage notes

FileLeaseLock uses flock() and is intended for cooperating processes on a local filesystem. Do not rely on network filesystem locking semantics. The in-memory implementation stores leases within the current PHP process; instances using the same clock object share a store. Leases do not provide fencing tokens, so applications should account for a former owner continuing work after expiration.