3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Lock;
24 use Friendica\Core\BaseLock;
25 use Friendica\Core\Cache\Duration;
26 use Friendica\Database\Database;
27 use Friendica\Util\DateTimeFormat;
30 * Locking driver that stores the locks in the database
32 class DatabaseLock extends BaseLock
35 * The current ID of the process
42 * @var Database The database connection of Friendica
47 * @param null|int $pid The Id of the current process (null means determine automatically)
49 public function __construct(Database $dba, $pid = null)
52 $this->pid = isset($pid) ? $pid : getmypid();
58 public function acquire($key, $timeout = 120, $ttl = Duration::FIVE_MINUTES)
64 $this->dba->lock('locks');
65 $lock = $this->dba->selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
67 if ($this->dba->isResult($lock)) {
68 if ($lock['locked']) {
69 // We want to lock something that was already locked by us? So we got the lock.
70 if ($lock['pid'] == $this->pid) {
74 if (!$lock['locked']) {
75 $this->dba->update('locks', ['locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
79 $this->dba->insert('locks', ['name' => $key, 'locked' => true, 'pid' => $this->pid, 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
81 $this->markAcquire($key);
86 if (!$got_lock && ($timeout > 0)) {
87 usleep(rand(100000, 2000000));
89 } while (!$got_lock && ((time() - $start) < $timeout));
97 public function release($key, $override = false)
100 $where = ['name' => $key];
102 $where = ['name' => $key, 'pid' => $this->pid];
105 if ($this->dba->exists('locks', $where)) {
106 $return = $this->dba->delete('locks', $where);
111 $this->markRelease($key);
119 public function releaseAll($override = false)
121 $success = parent::releaseAll($override);
126 $where = ['pid' => $this->pid];
128 $return = $this->dba->delete('locks', $where);
130 $this->acquiredLocks = [];
132 return $return && $success;
138 public function isLocked($key)
140 $lock = $this->dba->selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
142 if ($this->dba->isResult($lock)) {
143 return $lock['locked'] !== false;
152 public function getName()
154 return Type::DATABASE;
160 public function getLocks(string $prefix = '')
162 if (empty($prefix)) {
163 $where = ['`expires` >= ?', DateTimeFormat::utcNow()];
165 $where = ['`expires` >= ? AND `name` LIKE CONCAT(?, \'%\')', DateTimeFormat::utcNow(), $prefix];
168 $stmt = $this->dba->select('locks', ['name'], $where);
171 while ($key = $this->dba->fetch($stmt)) {
172 array_push($keys, $key['name']);
174 $this->dba->close($stmt);