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.
composer require roadrunner/symfony-lock-driverThe 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:6001Without 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.
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.
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);Contributions are welcome! If you find an issue or have a feature request, please open an issue or submit a pull request.