2018-06-28 22:57:17 +02:00
|
|
|
<?php
|
2018-07-05 20:57:31 +02:00
|
|
|
|
2018-06-28 22:57:17 +02:00
|
|
|
/**
|
|
|
|
* @file src/Core/Lock.php
|
|
|
|
* @brief Functions for preventing parallel execution of functions
|
|
|
|
*/
|
|
|
|
|
2019-01-06 22:28:14 +01:00
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2019-03-03 15:05:35 +01:00
|
|
|
use Friendica\Factory\CacheDriverFactory;
|
2018-07-04 23:37:22 +02:00
|
|
|
use Friendica\Core\Cache\IMemoryCacheDriver;
|
2018-06-28 22:57:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This class contain Functions for preventing parallel execution of functions
|
|
|
|
*/
|
|
|
|
class Lock
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Lock\ILockDriver;
|
|
|
|
*/
|
|
|
|
static $driver = null;
|
|
|
|
|
|
|
|
public static function init()
|
|
|
|
{
|
|
|
|
$lock_driver = Config::get('system', 'lock_driver', 'default');
|
|
|
|
|
|
|
|
try {
|
|
|
|
switch ($lock_driver) {
|
|
|
|
case 'memcache':
|
|
|
|
case 'memcached':
|
|
|
|
case 'redis':
|
2018-07-05 07:59:56 +02:00
|
|
|
$cache_driver = CacheDriverFactory::create($lock_driver);
|
2018-07-04 23:37:22 +02:00
|
|
|
if ($cache_driver instanceof IMemoryCacheDriver) {
|
|
|
|
self::$driver = new Lock\CacheLockDriver($cache_driver);
|
|
|
|
}
|
2018-06-28 22:57:17 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'database':
|
|
|
|
self::$driver = new Lock\DatabaseLockDriver();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'semaphore':
|
|
|
|
self::$driver = new Lock\SemaphoreLockDriver();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
self::useAutoDriver();
|
|
|
|
}
|
|
|
|
} catch (\Exception $exception) {
|
2018-10-31 17:52:41 +01:00
|
|
|
Logger::log('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'');
|
2018-06-28 22:57:17 +02:00
|
|
|
self::useAutoDriver();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief This method tries to find the best - local - locking method for Friendica
|
|
|
|
*
|
|
|
|
* The following sequence will be tried:
|
|
|
|
* 1. Semaphore Locking
|
|
|
|
* 2. Cache Locking
|
|
|
|
* 3. Database Locking
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private static function useAutoDriver() {
|
|
|
|
|
|
|
|
// 1. Try to use Semaphores for - local - locking
|
|
|
|
if (function_exists('sem_get')) {
|
|
|
|
try {
|
|
|
|
self::$driver = new Lock\SemaphoreLockDriver();
|
|
|
|
return;
|
|
|
|
} catch (\Exception $exception) {
|
2018-10-31 17:52:41 +01:00
|
|
|
Logger::log('Using Semaphore driver for locking failed: ' . $exception->getMessage());
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!)
|
|
|
|
$cache_driver = Config::get('system', 'cache_driver', 'database');
|
|
|
|
if ($cache_driver != 'database') {
|
|
|
|
try {
|
|
|
|
$lock_driver = CacheDriverFactory::create($cache_driver);
|
2018-07-04 23:37:22 +02:00
|
|
|
if ($lock_driver instanceof IMemoryCacheDriver) {
|
|
|
|
self::$driver = new Lock\CacheLockDriver($lock_driver);
|
|
|
|
}
|
2018-06-28 22:57:17 +02:00
|
|
|
return;
|
|
|
|
} catch (\Exception $exception) {
|
2018-10-29 22:20:46 +01:00
|
|
|
Logger::log('Using Cache driver for locking failed: ' . $exception->getMessage());
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Use Database Locking as a Fallback
|
|
|
|
self::$driver = new Lock\DatabaseLockDriver();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current cache driver
|
|
|
|
*
|
|
|
|
* @return Lock\ILockDriver;
|
|
|
|
*/
|
|
|
|
private static function getDriver()
|
|
|
|
{
|
|
|
|
if (self::$driver === null) {
|
|
|
|
self::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Acquires a lock for a given name
|
|
|
|
*
|
|
|
|
* @param string $key Name of the lock
|
|
|
|
* @param integer $timeout Seconds until we give up
|
2018-10-29 10:16:07 +01:00
|
|
|
* @param integer $ttl The Lock lifespan, must be one of the Cache constants
|
2018-06-28 22:57:17 +02:00
|
|
|
*
|
|
|
|
* @return boolean Was the lock successful?
|
|
|
|
*/
|
2018-10-29 10:16:07 +01:00
|
|
|
public static function acquire($key, $timeout = 120, $ttl = Cache::FIVE_MINUTES)
|
2018-06-28 22:57:17 +02:00
|
|
|
{
|
2018-10-29 10:16:07 +01:00
|
|
|
return self::getDriver()->acquireLock($key, $timeout, $ttl);
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Releases a lock if it was set by us
|
|
|
|
*
|
2019-02-24 12:24:09 +01:00
|
|
|
* @param string $key Name of the lock
|
|
|
|
* @param bool $override Overrides the lock to get releases
|
2018-07-05 20:57:31 +02:00
|
|
|
* @return void
|
2018-06-28 22:57:17 +02:00
|
|
|
*/
|
2019-02-24 12:24:09 +01:00
|
|
|
public static function release($key, $override = false)
|
2018-06-28 22:57:17 +02:00
|
|
|
{
|
2019-02-24 12:24:09 +01:00
|
|
|
self::getDriver()->releaseLock($key, $override);
|
2018-06-28 22:57:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Releases all lock that were set by us
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function releaseAll()
|
|
|
|
{
|
|
|
|
self::getDriver()->releaseAll();
|
|
|
|
}
|
|
|
|
}
|