roadrunner / symfony-lock-driver
Symfony Lock store backed by the RoadRunner lock plugin: use RoadRunner distributed locks through the symfony/lock API
Package info
github.com/roadrunner-php/symfony-lock-driver
pkg:composer/roadrunner/symfony-lock-driver
Fund package maintenance!
Requires
- php: >=8.2
- roadrunner/lock: ^1.2
- symfony/lock: ^6.0 || ^7.0 || ^8.0
- symfony/polyfill-php83: ^1.32
Requires (Dev)
- mockery/mockery: ^1.6
- spiral/code-style: ^2.3.1
- testo/bridge-mockery: ^0.1.2
- testo/testo: ^0.10.55
- vimeo/psalm: ^6 || ^7
Suggests
None
Provides
None
Conflicts
None
Replaces
README
Symfony Lock store backed by the RoadRunner Lock plugin
This package is a bridge between the RoadRunner Lock plugin and the Symfony Lock component.
It provides a RoadRunnerStore, so symfony/lock can manage distributed locks through the RoadRunner server shared by all your workers.
Get Started
Installation
composer require roadrunner/symfony-lock-driver
Configuration
The Lock plugin is driven over RPC, so the RPC plugin must be enabled in .rr.yaml:
version: "3" rpc: listen: tcp://127.0.0.1:6001
Without a lock section the plugin uses the in-memory backend. To share locks between several RoadRunner instances, configure the Redis backend as described in the Lock plugin documentation.
Usage
Create a RoadRunnerStore on top of the RoadRunner Lock client and pass it to the Symfony LockFactory:
use RoadRunner\Lock\Lock; use Spiral\Goridge\RPC\RPC; use Spiral\RoadRunner\Symfony\Lock\RoadRunnerStore; use Symfony\Component\Lock\LockFactory; require __DIR__ . '/vendor/autoload.php'; $lock = new Lock(RPC::create('tcp://127.0.0.1:6001')); $factory = new LockFactory( new RoadRunnerStore($lock) ); $invoiceLock = $factory->createLock('invoice-42'); if ($invoiceLock->acquire()) { try { // ... critical section } finally { $invoiceLock->release(); } }
Read more about using the Symfony Lock component here.
Store options
RoadRunnerStore accepts two timing options:
| Option | Default | Description |
|---|---|---|
$initialTtl |
300.0 |
Default lock time-to-live, in seconds. When it elapses the lock is released automatically; 0 means it never expires on its own. |
$initialWaitTtl |
0 |
Default time to wait for the lock to become free, in seconds. 0 is effectively non-blocking: the in-memory backend caps a 0 wait at 1ms (the Redis backend makes a single attempt), so acquiring an already-held lock fails almost immediately. A positive value blocks for up to that duration. |
// Wait up to 5 seconds for the lock, and hold it for at most 30 seconds. $store = (new RoadRunnerStore($lock))->withTtl(ttl: 30.0, waitTtl: 5.0);
Contributing
Contributions are welcome! If you find an issue or have a feature request, please open an issue or submit a pull request.