-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implemented
Symfony\Lock\LockPoolingHelper
- Loading branch information
Showing
10 changed files
with
261 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Symfony\Lock; | ||
|
||
use PetrKnap\CriticalSection\CriticalSection; | ||
use PetrKnap\Optional\Optional; | ||
use Symfony\Component\Lock\LockFactory; | ||
use Symfony\Component\Lock\LockInterface; | ||
|
||
/** | ||
* @phpstan-type TLockPool = array<non-empty-string, LockInterface> | ||
*/ | ||
final class LockPoolingHelper | ||
{ | ||
/** | ||
* @return TLockPool | ||
*/ | ||
public static function createLockPool(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* @param TLockPool $lockPool | ||
* @param Optional<float>|null $ttl | ||
* @param Optional<bool>|null $autoRelease | ||
*/ | ||
public static function getOrCreateLock( | ||
array &$lockPool, | ||
LockFactory $lockFactory, | ||
string $resource, | ||
Optional|null $ttl = null, | ||
Optional|null $autoRelease = null, | ||
): LockInterface { | ||
if (array_key_exists($resource, $lockPool)) { | ||
return $lockPool[$resource]; | ||
} elseif ($ttl === null && $autoRelease === null) { | ||
$lockPool[$resource] = $lockFactory->createLock($resource); | ||
} elseif ($ttl === null) { | ||
$lockPool[$resource] = $lockFactory->createLock($resource, autoRelease: $autoRelease->orElseThrow()); | ||
} elseif ($autoRelease === null) { | ||
$lockPool[$resource] = $lockFactory->createLock($resource, $ttl->isPresent() ? $ttl->get() : null); | ||
} else { | ||
$lockPool[$resource] = $lockFactory->createLock($resource, $ttl->isPresent() ? $ttl->get() : null, $autoRelease->orElseThrow()); | ||
} | ||
return $lockPool[$resource]; | ||
} | ||
|
||
/** | ||
* @param TLockPool $lockPool | ||
*/ | ||
public static function createCriticalSection(array $lockPool): CriticalSection | ||
{ | ||
ksort($lockPool); | ||
return CriticalSection::withLocks($lockPool); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Some; | ||
|
||
use PetrKnap\CriticalSection\LockableResource; | ||
use PetrKnap\CriticalSection\Locked; | ||
use PetrKnap\CriticalSection\Symfony\Lock\LockPoolingHelper; | ||
use Symfony\Component\Lock\LockFactory; | ||
|
||
final class NamedCriticalSectionService | ||
{ | ||
public function __construct( | ||
private readonly LockFactory $lockFactory, | ||
) { | ||
} | ||
|
||
/** | ||
* @template T of mixed | ||
* | ||
* @param callable(Locked<Resource> ...$resources): T $do | ||
* | ||
* @return T | ||
*/ | ||
public function updateSomeResources(callable $do, Resource ...$resources): mixed | ||
{ | ||
$lockPool = LockPoolingHelper::createLockPool(); | ||
foreach ($resources as &$resource) { | ||
$resource = LockableResource::of( | ||
$resource, | ||
LockPoolingHelper::getOrCreateLock($lockPool, $this->lockFactory, 'some_resource-' . $resource->id), | ||
); | ||
} | ||
return LockPoolingHelper::createCriticalSection($lockPool)($do, ...$resources); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Symfony\Lock; | ||
|
||
use PetrKnap\Optional\OptionalBool; | ||
use PetrKnap\Optional\OptionalFloat; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Lock\LockFactory; | ||
use Symfony\Component\Lock\LockInterface; | ||
use Symfony\Component\Lock\SharedLockInterface; | ||
|
||
class LockPoolingHelperTest extends TestCase | ||
{ | ||
private const RESOURCE = 'resource'; | ||
|
||
private MockObject&SharedLockInterface $lock; | ||
private MockObject&LockFactory $lockFactory; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->lock = self::createMock(SharedLockInterface::class); | ||
$this->lockFactory = self::createMock(LockFactory::class); | ||
} | ||
|
||
public function testGetsLockWhenLockIsPresentInPool(): void | ||
{ | ||
$this->lockFactory | ||
->expects(self::never()) | ||
->method('createLock') | ||
; | ||
$lockPool = [self::RESOURCE => $this->lock]; | ||
|
||
self::assertSame( | ||
$this->lock, | ||
LockPoolingHelper::getOrCreateLock($lockPool, $this->lockFactory, self::RESOURCE), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider dataCreatesNewLockAndAddsItToPoolWhenLockIsNotPresentInPool | ||
*/ | ||
public function testCreatesNewLockAndAddsItToPoolWhenLockIsNotPresentInPool(OptionalFloat|null $ttl, OptionalBool|null $autoRelease): void | ||
{ | ||
$this->lockFactory | ||
->expects(self::once()) | ||
->method('createLock') | ||
->with(self::RESOURCE, $ttl === null ? 300.0 : null, $autoRelease === null ? true : false) | ||
->willReturn($this->lock) | ||
; | ||
$lockPool = []; | ||
|
||
self::assertSame( | ||
$this->lock, | ||
LockPoolingHelper::getOrCreateLock($lockPool, $this->lockFactory, self::RESOURCE, $ttl, $autoRelease), | ||
); | ||
|
||
self::assertSame( | ||
[self::RESOURCE => $this->lock], | ||
$lockPool, | ||
); | ||
} | ||
|
||
public static function dataCreatesNewLockAndAddsItToPoolWhenLockIsNotPresentInPool(): array | ||
{ | ||
return [ | ||
'ttl & autoRelease' => [OptionalFloat::empty(), OptionalBool::of(false)], | ||
'ttl' => [OptionalFloat::empty(), null], | ||
'autoRelease' => [null, OptionalBool::of(false)], | ||
'default values' => [null, null], | ||
]; | ||
} | ||
|
||
public function testCreatesCriticalSectionWithSortedLocksFromPool(): void | ||
{ | ||
$acquired = []; | ||
$lockA = self::createMock(LockInterface::class); | ||
$lockA->method('acquire')->willReturnCallback(static function () use (&$acquired) { | ||
$acquired[] = 'a'; | ||
return true; | ||
}); | ||
$lockB = self::createMock(LockInterface::class); | ||
$lockB->method('acquire')->willReturnCallback(static function () use (&$acquired) { | ||
$acquired[] = 'b'; | ||
return true; | ||
}); | ||
$lockC = self::createMock(LockInterface::class); | ||
$lockC->method('acquire')->willReturnCallback(static function () use (&$acquired) { | ||
$acquired[] = 'c'; | ||
return true; | ||
}); | ||
$lockPool = [ | ||
'b' => $lockB, | ||
'a' => $lockA, | ||
'c' => $lockC, | ||
]; | ||
|
||
LockPoolingHelper::createCriticalSection($lockPool)(static fn (): bool => true); | ||
|
||
self::assertSame( | ||
['a', 'b', 'c'], | ||
$acquired, | ||
); | ||
} | ||
} |