PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.0.5
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.0.5
5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Concurrency / Lock.php
matomo / app / core / Concurrency Last commit date
LockBackend 6 years ago DistributedList.php 6 years ago Lock.php 6 years ago LockBackend.php 6 years ago
Lock.php
139 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link http://piwik.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik\Concurrency;
10
11 use Piwik\Common;
12
13 class Lock
14 {
15 const MAX_KEY_LEN = 70;
16
17 /**
18 * @var LockBackend
19 */
20 private $backend;
21
22 private $lockKeyStart;
23
24 private $lockKey = null;
25 private $lockValue = null;
26 private $defaultTtl = null;
27
28 public function __construct(LockBackend $backend, $lockKeyStart, $defaultTtl = null)
29 {
30 $this->backend = $backend;
31 $this->lockKeyStart = $lockKeyStart;
32 $this->lockKey = $this->lockKeyStart;
33 $this->defaultTtl = $defaultTtl;
34 }
35
36 public function reexpireLock()
37 {
38 $this->expireLock($this->defaultTtl);
39 }
40
41 public function getNumberOfAcquiredLocks()
42 {
43 return count($this->getAllAcquiredLockKeys());
44 }
45
46 public function getAllAcquiredLockKeys()
47 {
48 return $this->backend->getKeysMatchingPattern($this->lockKeyStart . '*');
49 }
50
51 public function execute($id, $callback)
52 {
53 $i = 0;
54 while (!$this->acquireLock($id)) {
55 $i++;
56 usleep( 100 * 1000 ); // 100ms
57 if ($i > 50) { // give up after 5seconds (50 * 100ms)
58 throw new \Exception('Could not get the lock for ID: ' . $id);
59 }
60 };
61 try {
62 return $callback();
63 } finally {
64 $this->unlock();
65 }
66 }
67
68 public function acquireLock($id, $ttlInSeconds = 60)
69 {
70 $this->lockKey = $this->lockKeyStart . $id;
71
72 if (Common::mb_strlen($this->lockKey) > self::MAX_KEY_LEN) {
73 // Lock key might be too long for DB column, so we hash it but leave the start of the original as well
74 // to make it more readable
75 $md5Len = 32;
76 $this->lockKey = Common::mb_substr($id, 0, self::MAX_KEY_LEN - $md5Len - 1) . md5($id);
77 }
78
79 $lockValue = substr(Common::generateUniqId(), 0, 12);
80 $locked = $this->backend->setIfNotExists($this->lockKey, $lockValue, $ttlInSeconds);
81
82 if ($locked) {
83 $this->lockValue = $lockValue;
84 }
85
86 return $locked;
87 }
88
89 public function isLocked()
90 {
91 if (!$this->lockValue) {
92 return false;
93 }
94
95 return $this->lockValue === $this->backend->get($this->lockKey);
96 }
97
98 public function unlock()
99 {
100 if ($this->lockValue) {
101 $this->backend->deleteIfKeyHasValue($this->lockKey, $this->lockValue);
102 $this->lockValue = null;
103 }
104 }
105
106 public function expireLock($ttlInSeconds)
107 {
108 if ($ttlInSeconds > 0) {
109 if ($this->lockValue) {
110 $success = $this->backend->expireIfKeyHasValue($this->lockKey, $this->lockValue, $ttlInSeconds);
111 if (!$success) {
112 $value = $this->backend->get($this->lockKey);
113 $message = sprintf('Failed to expire key %s (%s / %s).', $this->lockKey, $this->lockValue, (string)$value);
114
115 if ($value === false) {
116 Common::printDebug($message . ' It seems like the key already expired as it no longer exists.');
117 } elseif (!empty($value) && $value == $this->lockValue) {
118 Common::printDebug($message . ' We still have the lock but for some reason it did not expire.');
119 } elseif (!empty($value)) {
120 Common::printDebug($message . ' This lock has been acquired by another process/server.');
121 } else {
122 Common::printDebug($message . ' Failed to expire key.');
123 }
124
125 return false;
126 }
127
128 return true;
129 } else {
130 Common::printDebug('Lock is not acquired, cannot update expiration.');
131 }
132 } else {
133 Common::printDebug('Provided TTL ' . $ttlInSeconds . ' is in valid in Lock::expireLock().');
134 }
135
136 return false;
137 }
138 }
139