2018-06-26 22:31:04 +02:00
|
|
|
<?php
|
|
|
|
|
2018-06-28 22:57:17 +02:00
|
|
|
namespace Friendica\Core\Lock;
|
2018-06-26 22:31:04 +02:00
|
|
|
|
2018-07-07 19:46:16 +02:00
|
|
|
use Friendica\Core\Cache;
|
2018-07-20 08:19:26 -04:00
|
|
|
use Friendica\Database\DBA;
|
2018-07-04 23:37:22 +02:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2018-06-26 22:31:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Locking driver that stores the locks in the database
|
|
|
|
*/
|
2018-06-28 22:57:17 +02:00
|
|
|
class DatabaseLockDriver extends AbstractLockDriver
|
2018-06-26 22:31:04 +02:00
|
|
|
{
|
|
|
|
/**
|
2018-07-04 23:37:22 +02:00
|
|
|
* (@inheritdoc)
|
2018-06-26 22:31:04 +02:00
|
|
|
*/
|
2018-07-07 19:46:16 +02:00
|
|
|
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
2018-06-26 22:31:04 +02:00
|
|
|
{
|
|
|
|
$got_lock = false;
|
|
|
|
$start = time();
|
|
|
|
|
|
|
|
do {
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::lock('locks');
|
|
|
|
$lock = DBA::selectFirst('locks', ['locked', 'pid'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
2018-06-26 22:31:04 +02:00
|
|
|
|
2018-07-21 08:46:04 -04:00
|
|
|
if (DBA::isResult($lock)) {
|
2018-06-26 22:31:04 +02:00
|
|
|
if ($lock['locked']) {
|
|
|
|
// We want to lock something that was already locked by us? So we got the lock.
|
|
|
|
if ($lock['pid'] == getmypid()) {
|
|
|
|
$got_lock = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$lock['locked']) {
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::update('locks', ['locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')], ['name' => $key]);
|
2018-06-26 22:31:04 +02:00
|
|
|
$got_lock = true;
|
|
|
|
}
|
2018-06-28 22:57:17 +02:00
|
|
|
} else {
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid(), 'expires' => DateTimeFormat::utc('now + ' . $ttl . 'seconds')]);
|
2018-06-26 22:31:04 +02:00
|
|
|
$got_lock = true;
|
2018-07-04 23:37:22 +02:00
|
|
|
$this->markAcquire($key);
|
2018-06-26 22:31:04 +02:00
|
|
|
}
|
|
|
|
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::unlock();
|
2018-06-26 22:31:04 +02:00
|
|
|
|
|
|
|
if (!$got_lock && ($timeout > 0)) {
|
|
|
|
usleep(rand(100000, 2000000));
|
|
|
|
}
|
|
|
|
} while (!$got_lock && ((time() - $start) < $timeout));
|
|
|
|
|
|
|
|
return $got_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-04 23:37:22 +02:00
|
|
|
* (@inheritdoc)
|
2018-06-26 22:31:04 +02:00
|
|
|
*/
|
2018-07-05 20:57:31 +02:00
|
|
|
public function releaseLock($key)
|
2018-06-26 22:31:04 +02:00
|
|
|
{
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::delete('locks', ['name' => $key, 'pid' => getmypid()]);
|
2018-06-28 22:57:17 +02:00
|
|
|
|
2018-07-04 23:37:22 +02:00
|
|
|
$this->markRelease($key);
|
2018-06-26 22:31:04 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-04 23:37:22 +02:00
|
|
|
* (@inheritdoc)
|
2018-06-26 22:31:04 +02:00
|
|
|
*/
|
|
|
|
public function releaseAll()
|
|
|
|
{
|
2018-07-20 08:19:26 -04:00
|
|
|
DBA::delete('locks', ['pid' => getmypid()]);
|
2018-06-28 22:57:17 +02:00
|
|
|
|
|
|
|
$this->acquiredLocks = [];
|
2018-06-26 22:31:04 +02:00
|
|
|
}
|
2018-07-04 23:37:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* (@inheritdoc)
|
|
|
|
*/
|
|
|
|
public function isLocked($key)
|
|
|
|
{
|
2018-07-20 08:19:26 -04:00
|
|
|
$lock = DBA::selectFirst('locks', ['locked'], ['`name` = ? AND `expires` >= ?', $key, DateTimeFormat::utcNow()]);
|
2018-07-04 23:37:22 +02:00
|
|
|
|
2018-07-21 08:46:04 -04:00
|
|
|
if (DBA::isResult($lock)) {
|
2018-07-04 23:37:22 +02:00
|
|
|
return $lock['locked'] !== false;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 22:31:04 +02:00
|
|
|
}
|