]> git.mxchange.org Git - friendica.git/blob - src/Core/Lock/DatabaseLockDriver.php
coding standards
[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          * (@inheritdoc)
16          */
17         public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
18         {
19                 $got_lock = false;
20                 $start = time();
21
22                 do {
23                         DBA::lock('locks');
24                         $lock = DBA::selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
25
26                         if (DBA::isResult($lock)) {
27                                 if ($lock['locked']) {
28                                         // We want to lock something that was already locked by us? So we got the lock.
29                                         if ($lock['pid'] == getmypid()) {
30                                                 $got_lock = true;
31                                         }
32                                 }
33                                 if (!$lock['locked']) {
34                                         DBA::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
35                                         $got_lock = true;
36                                 }
37                         } else {
38                                 DBA::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
39                                 $got_lock = true;
40                                 $this->markAcquire($key);
41                         }
42
43                         DBA::unlock();
44
45                         if (!$got_lock && ($timeout > 0)) {
46                                 usleep(rand(100000, 2000000));
47                         }
48                 } while (!$got_lock && ((time() - $start) < $timeout));
49
50                 return $got_lock;
51         }
52
53         /**
54          * (@inheritdoc)
55          */
56         public function releaseLock($key)
57         {
58                 DBA::delete('locks', ['name' => $key, 'pid' => getmypid()]);
59
60                 $this->markRelease($key);
61
62                 return;
63         }
64
65         /**
66          * (@inheritdoc)
67          */
68         public function releaseAll()
69         {
70                 DBA::delete('locks', ['pid' => getmypid()]);
71
72                 $this->acquiredLocks = [];
73         }
74
75         /**
76          * (@inheritdoc)
77          */
78         public function isLocked($key)
79         {
80                 $lock = DBA::selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
81
82                 if (DBA::isResult($lock)) {
83                         return $lock['locked'] !== false;
84                 } else {
85                         return false;
86                 }
87         }
88 }