]> git.mxchange.org Git - friendica.git/blob - src/Core/Lock/DatabaseLockDriver.php
Avoid memory issue in exception
[friendica.git] / src / Core / Lock / DatabaseLockDriver.php
1 <?php
2
3 namespace Friendica\Core\Lock;
4
5 use Friendica\Core\Cache;
6 use Friendica\Database\DBA;
7 use Friendica\Util\DateTimeFormat;
8
9 /**
10  * Locking driver that stores the locks in the database
11  */
12 class DatabaseLockDriver extends AbstractLockDriver
13 {
14         /**
15          * The current ID of the process
16          *
17          * @var int
18          */
19         private $pid;
20
21         /**
22          * @param null|int $pid The Id of the current process (null means determine automatically)
23          */
24         public function __construct($pid = null)
25         {
26                 $this->pid = isset($pid) ? $pid : getmypid();
27         }
28
29         /**
30          * (@inheritdoc)
31          */
32         public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
33         {
34                 $got_lock = false;
35                 $start = time();
36
37                 do {
38                         DBA::lock('locks');
39                         $lock = DBA::selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
40
41                         if (DBA::isResult($lock)) {
42                                 if ($lock['locked']) {
43                                         // We want to lock something that was already locked by us? So we got the lock.
44                                         if ($lock['pid'] == $this->pid) {
45                                                 $got_lock = true;
46                                         }
47                                 }
48                                 if (!$lock['locked']) {
49                                         DBA::update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
50                                         $got_lock = true;
51                                 }
52                         } else {
53                                 DBA::insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
54                                 $got_lock = true;
55                                 $this->markAcquire($key);
56                         }
57
58                         DBA::unlock();
59
60                         if (!$got_lock && ($timeout > 0)) {
61                                 usleep(rand(100000, 2000000));
62                         }
63                 } while (!$got_lock && ((time() - $start) < $timeout));
64
65                 return $got_lock;
66         }
67
68         /**
69          * (@inheritdoc)
70          */
71         public function releaseLock($key)
72         {
73                 DBA::delete('locks', ['name' => $key, 'pid' => $this->pid]);
74
75                 $this->markRelease($key);
76
77                 return;
78         }
79
80         /**
81          * (@inheritdoc)
82          */
83         public function releaseAll()
84         {
85                 DBA::delete('locks', ['pid' => $this->pid]);
86
87                 $this->acquiredLocks = [];
88         }
89
90         /**
91          * (@inheritdoc)
92          */
93         public function isLocked($key)
94         {
95                 $lock = DBA::selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
96
97                 if (DBA::isResult($lock)) {
98                         return $lock['locked'] !== false;
99                 } else {
100                         return false;
101                 }
102         }
103 }