Skip to content

Commit 92f0984

Browse files
committed
acquire mutexes in random order
1 parent 211715a commit 92f0984

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/Mutex/DistributedMutex.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ protected function acquireWithToken(string $key, float $expireTimeout)
4747
{
4848
$acquireTimeout = \Closure::bind(fn () => $this->acquireTimeout, $this, AbstractSpinlockMutex::class)();
4949

50+
$mutexesInRandomOrder = $this->mutexes;
51+
shuffle($mutexesInRandomOrder);
52+
5053
// 1. This differs from the specification to avoid an overflow on 32-Bit systems.
5154
$startTs = microtime(true);
5255

@@ -55,7 +58,7 @@ protected function acquireWithToken(string $key, float $expireTimeout)
5558
$notAcquired = 0;
5659
$errored = 0;
5760
$exception = null;
58-
foreach ($this->mutexes as $index => $mutex) {
61+
foreach ($mutexesInRandomOrder as $index => $mutex) {
5962
try {
6063
if ($this->acquireMutex($mutex, $key, $acquireTimeout - (microtime(true) - $startTs), $expireTimeout)) {
6164
$acquiredIndexes[] = $index;

0 commit comments

Comments
 (0)