]> git.mxchange.org Git - friendica.git/blob - src/Core/Lock/DatabaseLock.php
Merge pull request #7514 from MrPetovan/bug/7507-allow-protocolless
[friendica.git] / src / Core / Lock / DatabaseLock.php
1 <?php
2
3 namespace Friendica\Core\Lock;
4
5 use Friendica\Core\Cache;
6 use Friendica\Database\Database;
7 use Friendica\Util\DateTimeFormat;
8
9 /**
10  * Locking driver that stores the locks in the database
11  */
12 class DatabaseLock extends Lock
13 {
14         /**
15          * The current ID of the process
16          *
17          * @var int
18          */
19         private $pid;
20
21         /**
22          * @var Database The database connection of Friendica
23          */
24         private $dba;
25
26         /**
27          * @param null|int $pid The Id of the current process (null means determine automatically)
28          */
29         public function __construct(Database $dba, $pid = null)
30         {
31                 $this->dba = $dba;
32                 $this->pid = isset($pid) ? $pid : getmypid();
33         }
34
35         /**
36          * (@inheritdoc)
37          */
38         public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
39         {
40                 $got_lock = false;
41                 $start    = time();
42
43                 do {
44                         $this->dba->lock('locks');
45                         $lock = $this->dba->selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
46
47                         if ($this->dba->isResult($lock)) {
48                                 if ($lock['locked']) {
49                                         // We want to lock something that was already locked by us? So we got the lock.
50                                         if ($lock['pid'] == $this->pid) {
51                                                 $got_lock = true;
52                                         }
53                                 }
54                                 if (!$lock['locked']) {
55                                         $this->dba->update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
56                                         $got_lock = true;
57                                 }
58                         } else {
59                                 $this->dba->insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
60                                 $got_lock = true;
61                                 $this->markAcquire($key);
62                         }
63
64                         $this->dba->unlock();
65
66                         if (!$got_lock && ($timeout > 0)) {
67                                 usleep(rand(100000, 2000000));
68                         }
69                 } while (!$got_lock && ((time() - $start) < $timeout));
70
71                 return $got_lock;
72         }
73
74         /**
75          * (@inheritdoc)
76          */
77         public function releaseLock($key, $override = false)
78         {
79                 if ($override) {
80                         $where = ['name' => $key];
81                 } else {
82                         $where = ['name' => $key, 'pid' => $this->pid];
83                 }
84
85                 $return = $this->dba->delete('locks', $where);
86
87                 $this->markRelease($key);
88
89                 return $return;
90         }
91
92         /**
93          * (@inheritdoc)
94          */
95         public function releaseAll($override = false)
96         {
97                 $success = parent::releaseAll($override);
98
99                 if ($override) {
100                         $where = ['1 = 1'];
101                 } else {
102                         $where = ['pid' => $this->pid];
103                 }
104                 $return = $this->dba->delete('locks', $where);
105
106                 $this->acquiredLocks = [];
107
108                 return $return;
109         }
110
111         /**
112          * (@inheritdoc)
113          */
114         public function isLocked($key)
115         {
116                 $lock = $this->dba->selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
117
118                 if ($this->dba->isResult($lock)) {
119                         return $lock['locked'] !== false;
120                 } else {
121                         return false;
122                 }
123         }
124
125         /**
126          * {@inheritDoc}
127          */
128         public function getName()
129         {
130                 return self::TYPE_DATABASE;
131         }
132
133         /**
134          * {@inheritDoc}
135          */
136         public function getLocks(string $prefix = '')
137         {
138                 if (empty($prefix)) {
139                         $where = ['`expires` >= ?', DateTimeFormat::utcNow()];
140                 } else {
141                         $where = ['`expires` >= ? AND `name` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
142                 }
143
144                 $stmt = $this->dba->select('locks', ['name'], $where);
145
146                 $keys = [];
147                 while ($key = $this->dba->fetch($stmt)) {
148                         array_push($keys, $key['name']);
149                 }
150                 $this->dba->close($stmt);
151
152                 return $keys;
153         }
154 }