3 namespace Drupal\Core\Lock;
6 * Non backend related common methods implementation for lock backends.
10 abstract class LockBackendAbstract implements LockBackendInterface {
13 * Current page lock token identifier.
20 * Existing locks for this page.
24 protected $locks = [];
29 public function wait($name, $delay = 30) {
30 // Pause the process for short periods between calling
31 // lock_may_be_available(). This prevents hitting the database with constant
32 // database queries while waiting, which could lead to performance issues.
33 // However, if the wait period is too long, there is the potential for a
34 // large number of processes to be blocked waiting for a lock, especially
35 // if the item being rebuilt is commonly requested. To address both of these
36 // concerns, begin waiting for 25ms, then add 25ms to the wait period each
37 // time until it reaches 500ms. After this point polling will continue every
38 // 500ms until $delay is reached.
40 // $delay is passed in seconds, but we will be using usleep(), which takes
41 // microseconds as a parameter. Multiply it by 1 million so that all
42 // further numbers are equivalent.
43 $delay = (int) $delay * 1000000;
45 // Begin sleeping at 25ms.
48 // This function should only be called by a request that failed to get a
49 // lock, so we sleep first to give the parallel request a chance to finish
50 // and release the lock.
52 // After each sleep, increase the value of $sleep until it reaches
53 // 500ms, to reduce the potential for a lock stampede.
54 $delay = $delay - $sleep;
55 $sleep = min(500000, $sleep + 25000, $delay);
56 if ($this->lockMayBeAvailable($name)) {
57 // No longer need to wait.
61 // The caller must still wait longer to get the lock.
68 public function getLockId() {
69 if (!isset($this->lockId)) {
70 $this->lockId = uniqid(mt_rand(), TRUE);