Update to Symfony/Lock 6.x, add RetryTillSaveStore.

This commit is contained in:
Buster "Silver Eagle" Neece 2021-12-25 21:56:22 -06:00
parent 7d2a58b11d
commit d98d33742a
No known key found for this signature in database
GPG Key ID: 9FC8B9E008872109
4 changed files with 115 additions and 13 deletions

View File

@ -75,7 +75,7 @@
"symfony/event-dispatcher": "^5.4|^6",
"symfony/finder": "^5.4|^6",
"symfony/intl": "^6",
"symfony/lock": "^5.4",
"symfony/lock": "^6",
"symfony/mailer": "^6",
"symfony/messenger": "^6",
"symfony/process": "^5.4|^6",

20
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c7da32c773b20acf0d07bc22a8bee50b",
"content-hash": "b3ebafa29ce07ba83b26aad09b6da7c8",
"packages": [
{
"name": "aws/aws-crt-php",
@ -7295,23 +7295,21 @@
},
{
"name": "symfony/lock",
"version": "v5.4.1",
"version": "v6.0.1",
"source": {
"type": "git",
"url": "https://github.com/symfony/lock.git",
"reference": "380c2fa596098a1ae8ba026a237b64ef9cb5a5f5"
"reference": "cf013d480064b2fd8cec0d86a4ed82616067787d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/lock/zipball/380c2fa596098a1ae8ba026a237b64ef9cb5a5f5",
"reference": "380c2fa596098a1ae8ba026a237b64ef9cb5a5f5",
"url": "https://api.github.com/repos/symfony/lock/zipball/cf013d480064b2fd8cec0d86a4ed82616067787d",
"reference": "cf013d480064b2fd8cec0d86a4ed82616067787d",
"shasum": ""
},
"require": {
"php": ">=7.2.5",
"psr/log": "^1|^2|^3",
"symfony/deprecation-contracts": "^2.1|^3",
"symfony/polyfill-php80": "^1.16"
"php": ">=8.0.2",
"psr/log": "^1|^2|^3"
},
"conflict": {
"doctrine/dbal": "<2.13"
@ -7354,7 +7352,7 @@
"semaphore"
],
"support": {
"source": "https://github.com/symfony/lock/tree/v5.4.1"
"source": "https://github.com/symfony/lock/tree/v6.0.1"
},
"funding": [
{
@ -7370,7 +7368,7 @@
"type": "tidelift"
}
],
"time": "2021-12-01T15:47:38+00:00"
"time": "2021-12-08T15:13:44+00:00"
},
{
"name": "symfony/mailer",

View File

@ -0,0 +1,104 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Lock;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
use Symfony\Component\Lock\BlockingStoreInterface;
use Symfony\Component\Lock\Exception\LockConflictedException;
use Symfony\Component\Lock\Key;
use Symfony\Component\Lock\PersistingStoreInterface;
/**
* Copied from Symfony 5.x as it was deprecated in 6.x with no suitable replacement.
*
* RetryTillSaveStore is a PersistingStoreInterface implementation which decorate a non blocking
* PersistingStoreInterface to provide a blocking storage.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
final class RetryTillSaveStore implements BlockingStoreInterface, LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* @param int $retrySleep Duration in ms between 2 retry
* @param int $retryCount Maximum amount of retry
*/
public function __construct(
private PersistingStoreInterface $decorated,
private int $retrySleep = 100,
private int $retryCount = \PHP_INT_MAX
) {
$this->logger = new NullLogger();
}
/**
* {@inheritdoc}
*/
public function save(Key $key): void
{
$this->decorated->save($key);
}
/**
* {@inheritdoc}
*/
public function waitAndSave(Key $key): void
{
$retry = 0;
$sleepRandomness = (int)($this->retrySleep / 10);
do {
try {
$this->decorated->save($key);
return;
} catch (LockConflictedException $e) {
usleep(($this->retrySleep + random_int(-$sleepRandomness, $sleepRandomness)) * 1000);
}
} while (++$retry < $this->retryCount);
if (null !== $this->logger) {
$this->logger->warning(
'Failed to store the "{resource}" lock. Abort after {retry} retry.',
['resource' => $key, 'retry' => $retry]
);
}
throw new LockConflictedException();
}
/**
* {@inheritdoc}
*/
public function putOffExpiration(Key $key, float $ttl): void
{
$this->decorated->putOffExpiration($key, $ttl);
}
/**
* {@inheritdoc}
*/
public function delete(Key $key): void
{
$this->decorated->delete($key);
}
/**
* {@inheritdoc}
*/
public function exists(Key $key): bool
{
return $this->decorated->exists($key);
}
}

View File

@ -4,12 +4,12 @@ declare(strict_types=1);
namespace App;
use App\Lock\RetryTillSaveStore;
use Psr\Log\LoggerInterface;
use Symfony\Component\Lock\BlockingStoreInterface;
use Symfony\Component\Lock\LockFactory as SymfonyLockFactory;
use Symfony\Component\Lock\LockInterface;
use Symfony\Component\Lock\PersistingStoreInterface;
use Symfony\Component\Lock\Store\RetryTillSaveStore;
class LockFactory extends SymfonyLockFactory
{