1
0
Fork 0

Replace Lock::acquire() with DI::lock()->acquire()

This commit is contained in:
Philipp Holzer 2020-01-07 00:20:31 +01:00
parent e56c8dcc3c
commit 634e657d23
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
13 changed files with 38 additions and 52 deletions

View file

@ -156,11 +156,11 @@ HELP;
}
if (!empty($ttl) && !empty($timeout)) {
$result = $this->lock->acquireLock($lock, $timeout, $ttl);
$result = $this->lock->acquire($lock, $timeout, $ttl);
} elseif (!empty($timeout)) {
$result = $this->lock->acquireLock($lock, $timeout);
$result = $this->lock->acquire($lock, $timeout);
} else {
$result = $this->lock->acquireLock($lock);
$result = $this->lock->acquire($lock);
}
if ($result) {

View file

@ -15,21 +15,6 @@ use Friendica\DI;
*/
class Lock
{
/**
* @brief Acquires a lock for a given name
*
* @param string $key Name of the lock
* @param integer $timeout Seconds until we give up
* @param integer $ttl The Lock lifespan, must be one of the Cache constants
*
* @return boolean Was the lock successful?
* @throws \Exception
*/
public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
{
return DI::lock()->acquireLock($key, $timeout, $ttl);
}
/**
* @brief Releases a lock if it was set by us
*

View file

@ -30,7 +30,7 @@ class CacheLock extends Lock
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
{
$got_lock = false;
$start = time();

View file

@ -35,7 +35,7 @@ class DatabaseLock extends Lock
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
public function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
{
$got_lock = false;
$start = time();

View file

@ -30,7 +30,7 @@ interface ILock
*
* @return boolean Was the lock successful?
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES);
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES);
/**
* Releases a lock if it was set by us

View file

@ -36,7 +36,7 @@ class SemaphoreLock extends Lock
/**
* (@inheritdoc)
*/
public function acquireLock($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
public function acquire($key, $timeout = 120, $ttl = Cache\Cache::FIVE_MINUTES)
{
self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
if (!empty(self::$semaphore[$key])) {

View file

@ -5,6 +5,7 @@ namespace Friendica\Core;
use Friendica\App;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Util\Strings;
class Update
@ -95,7 +96,7 @@ class Update
// Compare the current structure with the defined structure
// If the Lock is acquired, never release it automatically to avoid double updates
if (Lock::acquire('dbupdate', 120, Cache::INFINITE)) {
if (DI::lock()->acquire('dbupdate', 120, Cache::INFINITE)) {
// Checks if the build changed during Lock acquiring (so no double update occurs)
$retryBuild = Config::get('system', 'build', null, true);
@ -181,7 +182,7 @@ class Update
// If the update fails or times-out completely you may need to
// delete the config entry to try again.
if (Lock::acquire('dbupdate_function', 120,Cache::INFINITE)) {
if (DI::lock()->acquire('dbupdate_function', 120,Cache::INFINITE)) {
// call the specific update
$retval = $funcname();

View file

@ -115,7 +115,7 @@ class Worker
}
// Trying to fetch new processes - but only once when successful
if (!$refetched && Lock::acquire('worker_process', 0)) {
if (!$refetched && DI::lock()->acquire('worker_process', 0)) {
self::findWorkerProcesses();
Lock::release('worker_process');
self::$state = self::STATE_REFETCH;
@ -129,7 +129,7 @@ class Worker
if (!self::getWaitingJobForPID()) {
self::$state = self::STATE_LONG_LOOP;
if (Lock::acquire('worker', 0)) {
if (DI::lock()->acquire('worker', 0)) {
// Count active workers and compare them with a maximum value that depends on the load
if (self::tooMuchWorkers()) {
Logger::log('Active worker limit reached, quitting.', Logger::DEBUG);
@ -933,7 +933,7 @@ class Worker
}
$stamp = (float)microtime(true);
if (!Lock::acquire('worker_process')) {
if (!DI::lock()->acquire('worker_process')) {
return false;
}
self::$lock_duration += (microtime(true) - $stamp);
@ -1172,7 +1172,7 @@ class Worker
}
// If there is a lock then we don't have to check for too much worker
if (!Lock::acquire('worker', 0)) {
if (!DI::lock()->acquire('worker', 0)) {
return $added;
}

View file

@ -2060,7 +2060,7 @@ class Item
}
// To avoid timing problems, we are using locks.
$locked = Lock::acquire('item_insert_activity');
$locked = DI::lock()->acquire('item_insert_activity');
if (!$locked) {
Logger::log("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
}
@ -2103,7 +2103,7 @@ class Item
}
// To avoid timing problems, we are using locks.
$locked = Lock::acquire('item_insert_content');
$locked = DI::lock()->acquire('item_insert_content');
if (!$locked) {
Logger::log("Couldn't acquire lock for URI " . $item['uri'] . " - proceeding anyway.");
}

View file

@ -539,7 +539,7 @@ class OStatus
Logger::log("Item with uri ".$item["uri"]." is from a blocked contact.", Logger::DEBUG);
} else {
// We are having duplicated entries. Hopefully this solves it.
if (Lock::acquire('ostatus_process_item_insert')) {
if (DI::lock()->acquire('ostatus_process_item_insert')) {
$ret = Item::insert($item);
Lock::release('ostatus_process_item_insert');
Logger::log("Item with uri ".$item["uri"]." for user ".$importer["uid"].' stored. Return value: '.$ret);

View file

@ -22,7 +22,7 @@ trait DbaLockMockTrait
* @param null $time The current timestamp
* @param null|int $times How often the method will get used
*
*@see DatabaseLock::acquireLock()
*@see DatabaseLock::acquire()
*
*/
public function mockAcquireLock($key, $ttl = Cache::FIVE_MINUTES, $locked = false, $pid = null, $rowExists = true, $time = null, $times = null)

View file

@ -38,7 +38,7 @@ abstract class LockTest extends MockedTest
public function testLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
}
@ -49,10 +49,10 @@ abstract class LockTest extends MockedTest
public function testDoubleLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
// We already locked it
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
}
/**
@ -61,7 +61,7 @@ abstract class LockTest extends MockedTest
public function testReleaseLock()
{
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->instance->releaseLock('foo');
$this->assertFalse($this->instance->isLocked('foo'));
@ -72,9 +72,9 @@ abstract class LockTest extends MockedTest
*/
public function testReleaseAll()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('bar', 1));
$this->assertTrue($this->instance->acquireLock('nice', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->acquire('bar', 1));
$this->assertTrue($this->instance->acquire('nice', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));
@ -95,9 +95,9 @@ abstract class LockTest extends MockedTest
$this->assertFalse($this->instance->isLocked('foo'));
$this->assertFalse($this->instance->isLocked('bar'));
$this->assertFalse($this->instance->isLocked('nice'));
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('bar', 1));
$this->assertTrue($this->instance->acquireLock('nice', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->acquire('bar', 1));
$this->assertTrue($this->instance->acquire('nice', 1));
$this->assertTrue($this->instance->releaseLock('foo'));
@ -117,7 +117,7 @@ abstract class LockTest extends MockedTest
public function testReleaseWitTTL()
{
$this->assertFalse($this->instance->isLocked('test'));
$this->assertTrue($this->instance->acquireLock('test', 1, 10));
$this->assertTrue($this->instance->acquire('test', 1, 10));
$this->assertTrue($this->instance->isLocked('test'));
$this->assertTrue($this->instance->releaseLock('test'));
$this->assertFalse($this->instance->isLocked('test'));
@ -128,9 +128,9 @@ abstract class LockTest extends MockedTest
*/
public function testGetLocks()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('bar', 1));
$this->assertTrue($this->instance->acquireLock('nice', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->acquire('bar', 1));
$this->assertTrue($this->instance->acquire('nice', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));
@ -148,9 +148,9 @@ abstract class LockTest extends MockedTest
*/
public function testGetLocksWithPrefix()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('test1', 1));
$this->assertTrue($this->instance->acquireLock('test2', 1));
$this->assertTrue($this->instance->acquire('foo', 1));
$this->assertTrue($this->instance->acquire('test1', 1));
$this->assertTrue($this->instance->acquire('test2', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('test1'));
@ -174,8 +174,8 @@ abstract class LockTest extends MockedTest
$this->assertFalse($this->instance->isLocked('bar'));
// TODO [nupplaphil] - Because of the Datetime-Utils for the database, we have to wait a FULL second between the checks to invalidate the db-locks/cache
$this->assertTrue($this->instance->acquireLock('foo', 2, 1));
$this->assertTrue($this->instance->acquireLock('bar', 2, 3));
$this->assertTrue($this->instance->acquire('foo', 2, 1));
$this->assertTrue($this->instance->acquire('bar', 2, 3));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));

View file

@ -85,7 +85,7 @@ class SemaphoreLockTest extends LockTest
touch($file);
$this->assertTrue(file_exists($file));
$this->assertTrue($this->instance->acquireLock('test'));
$this->assertTrue($this->instance->acquire('test'));
$this->assertTrue($this->instance->isLocked('test'));
$this->assertTrue($this->instance->releaseLock('test'));
}