Skip to content

Commit 593d44e

Browse files
committed
Merge branch 'MC-32426' into 2.4-develop
2 parents 13aa42c + 7a04c9f commit 593d44e

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

lib/internal/Magento/Framework/Cache/LockGuardedCacheLoader.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,31 @@ class LockGuardedCacheLoader
4848
private $loadTimeout;
4949

5050
/**
51-
* LockGuardedCacheLoader constructor.
51+
* Minimal delay timeout in ms.
52+
*
53+
* @var int
54+
*/
55+
private $minimalDelayTimeout;
56+
57+
/**
5258
* @param LockManagerInterface $locker
5359
* @param int $lockTimeout
5460
* @param int $delayTimeout
5561
* @param int $loadTimeout
62+
* @param int $minimalDelayTimeout
5663
*/
5764
public function __construct(
5865
LockManagerInterface $locker,
5966
int $lockTimeout = 10000,
6067
int $delayTimeout = 20,
61-
int $loadTimeout = 10000
68+
int $loadTimeout = 10000,
69+
int $minimalDelayTimeout = 5
6270
) {
6371
$this->locker = $locker;
6472
$this->lockTimeout = $lockTimeout;
6573
$this->delayTimeout = $delayTimeout;
6674
$this->loadTimeout = $loadTimeout;
75+
$this->minimalDelayTimeout = $minimalDelayTimeout;
6776
}
6877

6978
/**
@@ -100,7 +109,7 @@ public function lockedLoadData(
100109
}
101110

102111
if ($cachedData === false) {
103-
usleep($this->delayTimeout * 1000);
112+
usleep($this->getLookupTimeout() * 1000);
104113
$cachedData = $dataLoader();
105114
}
106115
}
@@ -118,7 +127,7 @@ public function lockedLoadData(
118127
public function lockedCleanData(string $lockName, callable $dataCleaner)
119128
{
120129
while ($this->locker->isLocked($lockName)) {
121-
usleep($this->delayTimeout * 1000);
130+
usleep($this->getLookupTimeout() * 1000);
122131
}
123132
try {
124133
if ($this->locker->lock($lockName, $this->lockTimeout / 1000)) {
@@ -128,4 +137,18 @@ public function lockedCleanData(string $lockName, callable $dataCleaner)
128137
$this->locker->unlock($lockName);
129138
}
130139
}
140+
141+
/**
142+
* Delay will be applied as rand($minimalDelayTimeout, $delayTimeout).
143+
* This helps to desynchronize multiple clients trying
144+
* to acquire the lock for the same resource at the same time
145+
*
146+
* @return int
147+
*/
148+
private function getLookupTimeout()
149+
{
150+
$lookupTimeout = rand($this->minimalDelayTimeout, $this->delayTimeout);
151+
152+
return $lookupTimeout;
153+
}
131154
}

0 commit comments

Comments
 (0)