comparison core/lib/Drupal/Core/Lock/LockBackendAbstract.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Lock;
4
5 /**
6 * Non backend related common methods implementation for lock backends.
7 *
8 * @ingroup lock
9 */
10 abstract class LockBackendAbstract implements LockBackendInterface {
11
12 /**
13 * Current page lock token identifier.
14 *
15 * @var string
16 */
17 protected $lockId;
18
19 /**
20 * Existing locks for this page.
21 *
22 * @var array
23 */
24 protected $locks = [];
25
26 /**
27 * {@inheritdoc}
28 */
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.
39
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;
44
45 // Begin sleeping at 25ms.
46 $sleep = 25000;
47 while ($delay > 0) {
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.
51 usleep($sleep);
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.
58 return FALSE;
59 }
60 }
61 // The caller must still wait longer to get the lock.
62 return TRUE;
63 }
64
65 /**
66 * {@inheritdoc}
67 */
68 public function getLockId() {
69 if (!isset($this->lockId)) {
70 $this->lockId = uniqid(mt_rand(), TRUE);
71 }
72 return $this->lockId;
73 }
74
75 }