adachsoft / lease-lock
Owner-bound expiring lease locks with file-backed and in-memory implementations and PSR-20 clock support.
Requires
- php: ^8.3
- adachsoft/lease-lock-contracts: ^0.1.0
- psr/clock: ^1.0
Requires (Dev)
- adachsoft/lease-lock-contract-tests: ^0.1.0
- adachsoft/php-code-style: ^0.7.3
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^12.5
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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.