]> git.mxchange.org Git - friendica.git/commitdiff
redesign of locking & caching
authorPhilipp Holzer <admin@philipp.info>
Thu, 28 Jun 2018 20:57:17 +0000 (22:57 +0200)
committerPhilipp Holzer <admin@philipp.info>
Thu, 28 Jun 2018 20:57:17 +0000 (22:57 +0200)
- New Factory "CacheDriverFactory" for Cache and Locks
- Adding Redis/Memcached Locking
- Moved Lock to Core
- other improvements

15 files changed:
src/Core/Cache.php
src/Core/Cache/CacheDriverFactory.php [new file with mode: 0644]
src/Core/Lock.php [new file with mode: 0644]
src/Core/Lock/AbstractLockDriver.php [new file with mode: 0644]
src/Core/Lock/CacheLockDriver.php [new file with mode: 0644]
src/Core/Lock/DatabaseLockDriver.php [new file with mode: 0644]
src/Core/Lock/ILockDriver.php [new file with mode: 0644]
src/Core/Lock/SemaphoreLockDriver.php [new file with mode: 0644]
src/Core/Worker.php
src/Protocol/OStatus.php
src/Util/Lock.php [deleted file]
src/Util/Lock/DatabaseLockDriver.php [deleted file]
src/Util/Lock/ILockDriver.php [deleted file]
src/Util/Lock/MemcacheLockDriver.php [deleted file]
src/Util/Lock/SemaphoreLockDriver.php [deleted file]

index 4202db325c411c38b7852321af769bfb0f09d33c..cc77d9bfd0dc7e35598130af672287454ffdd25c 100644 (file)
@@ -4,8 +4,7 @@
  */
 namespace Friendica\Core;
 
-use Friendica\Core\Cache;
-use Friendica\Core\Config;
+use Friendica\Core\Cache\CacheDriverFactory;
 
 /**
  * @brief Class for storing data for a short time
@@ -24,31 +23,13 @@ class Cache extends \Friendica\BaseObject
        /**
         * @var Cache\ICacheDriver
         */
-       static $driver = null;
+       private static $driver = null;
 
        public static function init()
        {
-               switch(Config::get('system', 'cache_driver', 'database')) {
-                       case 'memcache':
-                               $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
-                               $memcache_port = Config::get('system', 'memcache_port', 11211);
-
-                               self::$driver = new Cache\MemcacheCacheDriver($memcache_host, $memcache_port);
-                               break;
-                       case 'memcached':
-                               $memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
-
-                               self::$driver = new Cache\MemcachedCacheDriver($memcached_hosts);
-                               break;
-                       case 'redis':
-                               $redis_host = Config::get('system', 'redis_host', '127.0.0.1');
-                               $redis_port = Config::get('system', 'redis_port', 6379);
-
-                               self::$driver = new Cache\RedisCacheDriver($redis_host, $redis_port);
-                               break;
-                       default:
-                               self::$driver = new Cache\DatabaseCacheDriver();
-               }
+               $driver_name = Config::get('system', 'cache_driver', 'database');
+
+               self::$driver = CacheDriverFactory::create($driver_name);
        }
 
        /**
diff --git a/src/Core/Cache/CacheDriverFactory.php b/src/Core/Cache/CacheDriverFactory.php
new file mode 100644 (file)
index 0000000..d15b8e9
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+namespace Friendica\Core\Cache;
+
+use Friendica\Core\Config;
+
+/**
+ * Class CacheDriverFactory
+ *
+ * @package Friendica\Core\Cache
+ *
+ * @brief A basic class to generate a CacheDriver
+ */
+class CacheDriverFactory
+{
+       /**
+        * @brief This method creates a CacheDriver for the given cache driver name
+        *
+        * @param string $driver The name of the cache driver
+        * @return ICacheDriver  The instance of the CacheDriver
+        * @throws \Exception    The exception if something went wrong during the CacheDriver creation
+        */
+       public static function create(string $driver) : ICacheDriver {
+
+               switch ($driver) {
+                       case 'memcache':
+                               $memcache_host = Config::get('system', 'memcache_host', '127.0.0.1');
+                               $memcache_port = Config::get('system', 'memcache_port', 11211);
+
+                               return new MemcacheCacheDriver($memcache_host, $memcache_port);
+                               break;
+
+                       case 'memcached':
+                               $memcached_hosts = Config::get('system', 'memcached_hosts', [['127.0.0.1', 11211]]);
+
+                               return new MemcachedCacheDriver($memcached_hosts);
+                               break;
+                       case 'redis':
+                               $redis_host = Config::get('system', 'redis_host', '127.0.0.1');
+                               $redis_port = Config::get('system', 'redis_port', 6379);
+
+                               return new RedisCacheDriver($redis_host, $redis_port);
+                               break;
+                       default:
+                               return new DatabaseCacheDriver();
+               }
+       }
+}
diff --git a/src/Core/Lock.php b/src/Core/Lock.php
new file mode 100644 (file)
index 0000000..7db0ea0
--- /dev/null
@@ -0,0 +1,145 @@
+<?php
+/**
+ * @file src/Util/Lock.php
+ */
+namespace Friendica\Core;
+
+/**
+ * @file src/Core/Lock.php
+ * @brief Functions for preventing parallel execution of functions
+ */
+
+use Friendica\Core\Cache\CacheDriverFactory;
+
+/**
+ * @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':
+                                       $cache_driver = CacheDriverFactory::create('memcache');
+                                       self::$driver = new Lock\CacheLockDriver($cache_driver);
+                                       break;
+
+                               case 'memcached':
+                                       $cache_driver = CacheDriverFactory::create('memcached');
+                                       self::$driver = new Lock\CacheLockDriver($cache_driver);
+                                       break;
+
+                               case 'redis':
+                                       $cache_driver = CacheDriverFactory::create('redis');
+                                       self::$driver = new Lock\CacheLockDriver($cache_driver);
+                                       break;
+
+                               case 'database':
+                                       self::$driver = new Lock\DatabaseLockDriver();
+                                       break;
+
+                               case 'semaphore':
+                                       self::$driver = new Lock\SemaphoreLockDriver();
+                                       break;
+
+                               default:
+                                       self::useAutoDriver();
+                       }
+               } catch (\Exception $exception) {
+                       logger ('Driver \'' . $lock_driver . '\' failed - Fallback to \'useAutoDriver()\'');
+                       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) {
+                               logger ('Using Semaphore driver for locking failed: ' . $exception->getMessage());
+                       }
+               }
+
+               // 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);
+                               self::$driver = new Lock\CacheLockDriver($lock_driver);
+                               return;
+                       } catch (\Exception $exception) {
+                               logger('Using Cache driver for locking failed: ' . $exception->getMessage());
+                       }
+               }
+
+               // 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
+        *
+        * @return boolean Was the lock successful?
+        */
+       public static function acquireLock($key, $timeout = 120)
+       {
+               return self::getDriver()->acquireLock($key, $timeout);
+       }
+
+       /**
+        * @brief Releases a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        * @return mixed
+        */
+       public static function releaseLock($key)
+       {
+               return self::getDriver()->releaseLock($key);
+       }
+
+       /**
+        * @brief Releases all lock that were set by us
+        * @return void
+        */
+       public static function releaseAll()
+       {
+               self::getDriver()->releaseAll();
+       }
+}
diff --git a/src/Core/Lock/AbstractLockDriver.php b/src/Core/Lock/AbstractLockDriver.php
new file mode 100644 (file)
index 0000000..15820c3
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+namespace Friendica\Core\Lock;
+
+/**
+ * Class AbstractLockDriver
+ *
+ * @package Friendica\Core\Lock
+ *
+ * @brief Basic class for Locking with common functions (local acquired locks, releaseAll, ..)
+ */
+abstract class AbstractLockDriver implements ILockDriver
+{
+       /**
+        * @var array The local acquired locks
+        */
+       protected $acquiredLocks = [];
+
+       /**
+        * @brief Check if we've locally acquired a lock
+        *
+        * @param string key The Name of the lock
+        * @return bool      Returns true if the lock is set
+        */
+       protected function hasAcquiredLock(string $key): bool {
+               return isset($this->acquireLock[$key]);
+       }
+
+       /**
+        * @brief Mark a locally acquired lock
+        *
+        * @param string $key The Name of the lock
+        */
+       protected function markAcquire(string $key) {
+               $this->acquiredLocks[$key] = true;
+       }
+
+       /**
+        * @brief Mark a release of a locally acquired lock
+        *
+        * @param string $key The Name of the lock
+        */
+       protected function markRelease(string $key) {
+               unset($this->acquiredLocks[$key]);
+       }
+
+       /**
+        * @brief Releases all lock that were set by us
+        *
+        * @return void
+        */
+       public function releaseAll() {
+               foreach ($this->acquiredLocks as $acquiredLock) {
+                       $this->releaseLock($acquiredLock);
+               }
+       }
+}
diff --git a/src/Core/Lock/CacheLockDriver.php b/src/Core/Lock/CacheLockDriver.php
new file mode 100644 (file)
index 0000000..0adca14
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+namespace Friendica\Core\Lock;
+
+use Friendica\Core\Cache\ICacheDriver;
+
+class CacheLockDriver extends AbstractLockDriver
+{
+       /**
+        * @var \Friendica\Core\Cache\ICacheDriver;
+        */
+       private $cache;
+
+       /**
+        * CacheLockDriver constructor.
+        *
+        * @param ICacheDriver $cache The CacheDriver for this type of lock
+        */
+       public function __construct(ICacheDriver $cache)
+       {
+               $this->cache = $cache;
+       }
+
+       /**
+        *
+        * @brief Sets a lock for a given name
+        *
+        * @param string $key The Name of the lock
+        * @param integer $timeout Seconds until we give up
+        *
+        * @return boolean Was the lock successful?
+        */
+       public function acquireLock(string $key, int $timeout = 120)
+       {
+               $got_lock = false;
+               $start = time();
+
+               $cachekey = get_app()->get_hostname() . ";lock:" . $key;
+
+               do {
+                       $lock = $this->cache->get($cachekey);
+
+                       if (!is_bool($lock)) {
+                               $pid = (int)$lock;
+
+                               // When the process id isn't used anymore, we can safely claim the lock for us.
+                               // Or we do want to lock something that was already locked by us.
+                               if (!posix_kill($pid, 0) || ($pid == getmypid())) {
+                                       $lock = false;
+                               }
+                       }
+                       if (is_bool($lock)) {
+                               $this->cache->set($cachekey, getmypid(), 300);
+                               $got_lock = true;
+                       }
+
+                       if (!$got_lock && ($timeout > 0)) {
+                               usleep(rand(10000, 200000));
+                       }
+               } while (!$got_lock && ((time() - $start) < $timeout));
+
+               $this->markAcquire($key);
+
+               return $got_lock;
+       }
+
+       /**
+        * @brief Removes a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        *
+        * @return mixed
+        */
+       public function releaseLock(string $key)
+       {
+               $cachekey = get_app()->get_hostname() . ";lock:" . $key;
+               $lock = $this->cache->get($cachekey);
+
+               if (!is_bool($lock)) {
+                       if ((int)$lock == getmypid()) {
+                               $this->cache->delete($cachekey);
+                       }
+               }
+
+               $this->markRelease($key);
+
+               return;
+       }
+}
diff --git a/src/Core/Lock/DatabaseLockDriver.php b/src/Core/Lock/DatabaseLockDriver.php
new file mode 100644 (file)
index 0000000..f987834
--- /dev/null
@@ -0,0 +1,89 @@
+<?php
+
+namespace Friendica\Core\Lock;
+
+use dba;
+use Friendica\Database\DBM;
+
+/**
+ * Locking driver that stores the locks in the database
+ */
+class DatabaseLockDriver extends AbstractLockDriver
+{
+       /**
+        * @brief Sets a lock for a given name
+        *
+        * @param string  $key      The Name of the lock
+        * @param integer $timeout  Seconds until we give up
+        *
+        * @return boolean Was the lock successful?
+        */
+       public function acquireLock(string $key, int $timeout = 120)
+       {
+               $got_lock = false;
+               $start = time();
+
+               do {
+                       dba::lock('locks');
+                       $lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $key]);
+
+                       if (DBM::is_result($lock)) {
+                               if ($lock['locked']) {
+                                       // When the process id isn't used anymore, we can safely claim the lock for us.
+                                       if (!posix_kill($lock['pid'], 0)) {
+                                               $lock['locked'] = false;
+                                       }
+                                       // 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']) {
+                                       dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $key]);
+                                       $got_lock = true;
+                               }
+                       } else {
+                               dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid()]);
+                               $got_lock = true;
+                       }
+
+                       dba::unlock();
+
+                       if (!$got_lock && ($timeout > 0)) {
+                               usleep(rand(100000, 2000000));
+                       }
+               } while (!$got_lock && ((time() - $start) < $timeout));
+
+               $this->markAcquire($key);
+
+               return $got_lock;
+       }
+
+       /**
+        * @brief Removes a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        *
+        * @return mixed
+        */
+       public function releaseLock(string $key)
+       {
+               dba::delete('locks', ['locked' => false, 'pid' => 0], ['name' => $key, 'pid' => getmypid()]);
+
+               $this->releaseLock($key);
+
+               return;
+       }
+
+       /**
+        * @brief Removes all lock that were set by us
+        *
+        * @return void
+        */
+       public function releaseAll()
+       {
+               dba::delete('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);
+
+               $this->acquiredLocks = [];
+       }
+}
diff --git a/src/Core/Lock/ILockDriver.php b/src/Core/Lock/ILockDriver.php
new file mode 100644 (file)
index 0000000..39e4ba8
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+namespace Friendica\Core\Lock;
+
+/**
+ * Lock Driver Interface
+ *
+ * @author Philipp Holzer <admin@philipp.info>
+ */
+interface ILockDriver
+{
+       /**
+        *
+        * @brief Acquires a lock for a given name
+        *
+        * @param string  $key      The Name of the lock
+        * @param integer $timeout  Seconds until we give up
+        *
+        * @return boolean Was the lock successful?
+        */
+       public function acquireLock(string $key, int $timeout = 120);
+
+       /**
+        * @brief Releases a lock if it was set by us
+        *
+        * @param string $key The Name of the lock
+        *
+        * @return void
+        */
+       public function releaseLock(string $key);
+
+       /**
+        * @brief Releases all lock that were set by us
+        *
+        * @return void
+        */
+       public function releaseAll();
+}
diff --git a/src/Core/Lock/SemaphoreLockDriver.php b/src/Core/Lock/SemaphoreLockDriver.php
new file mode 100644 (file)
index 0000000..84965a1
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+
+namespace Friendica\Core\Lock;
+
+class SemaphoreLockDriver extends AbstractLockDriver
+{
+       public function __construct()
+       {
+               if (!function_exists('sem_get')) {
+                       throw new \Exception('Semaphore lock not supported');
+               }
+       }
+
+       /**
+        * @brief Creates a semaphore key
+        *
+        * @param string $key Name of the lock
+        *
+        * @return integer the semaphore key
+        */
+       private static function semaphoreKey(string $key): int
+       {
+               $temp = get_temppath();
+
+               $file = $temp.'/'.$key.'.sem';
+
+               if (!file_exists($file)) {
+                       file_put_contents($file, $key);
+               }
+
+               return ftok($file, 'f');
+       }
+
+       /**
+        *
+        * @brief Sets a lock for a given name
+        *
+        * @param string $key The Name of the lock
+        * @param integer $timeout Seconds until we give up
+        *
+        * @return boolean Was the lock successful?
+        */
+       public function acquireLock(string $key, int $timeout = 120)
+       {
+               $this->acquiredLocks[$key] = sem_get(self::semaphoreKey($key));
+               if ($this->acquiredLocks[$key]) {
+                       return sem_acquire($this->acquiredLocks[$key], ($timeout == 0));
+               }
+       }
+
+       /**
+        * @brief Removes a lock if it was set by us
+        *
+        * @param string $key Name of the lock
+        *
+        * @return mixed
+        */
+       public function releaseLock(string $key)
+       {
+               if (empty($this->acquiredLocks[$key])) {
+                       return false;
+               } else {
+                       $success = @sem_release($this->acquiredLocks[$key]);
+                       unset($this->acquiredLocks[$key]);
+                       return $success;
+               }
+       }
+}
index ef7873fcd44c312f5a88d90ee697f41bd7799a58..cbf2ae8bd91d83f00778d96f9d3a7f5d34221b7c 100644 (file)
@@ -7,10 +7,10 @@ namespace Friendica\Core;
 use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Core\System;
+use Friendica\Core\Lock;
 use Friendica\Database\DBM;
 use Friendica\Model\Process;
 use Friendica\Util\DateTimeFormat;
-use Friendica\Util\Lock;
 use Friendica\Util\Network;
 use dba;
 
index a1223b7b681f3b5162503893a7fdab259b7c68a3..288cbfed1b34d01ee14923d4084fb9c2fc4a4a96 100644 (file)
@@ -9,6 +9,7 @@ use Friendica\Content\Text\HTML;
 use Friendica\Core\Cache;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
+use Friendica\Core\Lock;
 use Friendica\Core\System;
 use Friendica\Database\DBM;
 use Friendica\Model\Contact;
@@ -19,7 +20,6 @@ use Friendica\Model\User;
 use Friendica\Network\Probe;
 use Friendica\Object\Image;
 use Friendica\Util\DateTimeFormat;
-use Friendica\Util\Lock;
 use Friendica\Util\Network;
 use Friendica\Util\XML;
 use dba;
diff --git a/src/Util/Lock.php b/src/Util/Lock.php
deleted file mode 100644 (file)
index b6a4a97..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-<?php
-/**
- * @file src/Util/Lock.php
- */
-namespace Friendica\Util;
-
-/**
- * @file src/Util/Lock.php
- * @brief Functions for preventing parallel execution of functions
- */
-
-use Friendica\Core\Config;
-use Friendica\Util\Lock;
-
-require_once 'include/dba.php';
-
-/**
- * @brief This class contain Functions for preventing parallel execution of functions
- */
-class Lock
-{
-       /**
-        * @var Lock\ILockDriver;
-        */
-       static $driver = null;
-
-       public static function init()
-       {
-               switch(Config::get('system', 'lock_driver', 'default')) {
-                       case 'memcache':
-                               self::$driver = new Lock\MemcacheLockDriver();
-                               break;
-                       case 'database':
-                               self::$driver = new Lock\DatabaseLockDriver();
-                               break;
-                       case 'semaphore':
-                               self::$driver = new Lock\SemaphoreLockDriver();
-                               break;
-                       default:
-                               // Determine what's the best/fastest locking driver (default behavior in Friendica)
-                               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                                       self::$driver = new Lock\SemaphoreLockDriver();
-                               } elseif (Config::get('system', 'cache_driver', 'database') == 'memcache') {
-                                       self::$driver = new Lock\MemcacheLockDriver();
-                               } else {
-                                       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
-        *
-        * @return boolean Was the lock successful?
-        */
-       public static function acquireLock($key, $timeout = 120)
-       {
-               return self::getDriver()->acquireLock($key, $timeout);
-       }
-
-       /**
-        * @brief Releases a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        * @return mixed
-        */
-       public static function releaseLock($key)
-       {
-               return self::getDriver()->releaseLock($key);
-       }
-
-       /**
-        * @brief Releases all lock that were set by us
-        * @return void
-        */
-       public static function releaseAll()
-       {
-               self::getDriver()->releaseAll();
-       }
-}
diff --git a/src/Util/Lock/DatabaseLockDriver.php b/src/Util/Lock/DatabaseLockDriver.php
deleted file mode 100644 (file)
index b2e8f50..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-
-namespace Friendica\Util\Lock;
-
-use dba;
-use Friendica\Database\DBM;
-
-/**
- * Locking driver that stores the locks in the database
- */
-class DatabaseLockDriver implements ILockDriver
-{
-       /**
-        * @brief Sets a lock for a given name
-        *
-        * @param string  $key      The Name of the lock
-        * @param integer $timeout  Seconds until we give up
-        *
-        * @return boolean Was the lock successful?
-        */
-       public function acquireLock($key, $timeout = 120)
-       {
-               $got_lock = false;
-               $start = time();
-
-               do {
-                       dba::lock('locks');
-                       $lock = dba::selectFirst('locks', ['locked', 'pid'], ['name' => $key]);
-
-                       if (DBM::is_result($lock)) {
-                               if ($lock['locked']) {
-                                       // When the process id isn't used anymore, we can safely claim the lock for us.
-                                       if (!posix_kill($lock['pid'], 0)) {
-                                               $lock['locked'] = false;
-                                       }
-                                       // 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']) {
-                                       dba::update('locks', ['locked' => true, 'pid' => getmypid()], ['name' => $key]);
-                                       $got_lock = true;
-                               }
-                       } elseif (!DBM::is_result($lock)) {
-                               dba::insert('locks', ['name' => $key, 'locked' => true, 'pid' => getmypid()]);
-                               $got_lock = true;
-                       }
-
-                       dba::unlock();
-
-                       if (!$got_lock && ($timeout > 0)) {
-                               usleep(rand(100000, 2000000));
-                       }
-               } while (!$got_lock && ((time() - $start) < $timeout));
-
-               return $got_lock;
-       }
-
-       /**
-        * @brief Removes a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
-        */
-       public function releaseLock($key)
-       {
-               dba::update('locks', ['locked' => false, 'pid' => 0], ['name' => $key, 'pid' => getmypid()]);
-
-               return;
-       }
-
-       /**
-        * @brief Removes all lock that were set by us
-        *
-        * @return void
-        */
-       public function releaseAll()
-       {
-               dba::update('locks', ['locked' => false, 'pid' => 0], ['pid' => getmypid()]);
-       }
-}
diff --git a/src/Util/Lock/ILockDriver.php b/src/Util/Lock/ILockDriver.php
deleted file mode 100644 (file)
index 7f5a359..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-<?php
-
-namespace Friendica\Util\Lock;
-
-/**
- * Lock Driver Interface
- *
- * @author Philipp Holzer <admin@philipp.info>
- */
-interface ILockDriver
-{
-       /**
-        *
-        * @brief Acquires a lock for a given name
-        *
-        * @param string  $key      The Name of the lock
-        * @param integer $timeout  Seconds until we give up
-        *
-        * @return boolean Was the lock successful?
-        */
-       public function acquireLock($key, $timeout = 120);
-
-       /**
-        * @brief Releases a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
-        */
-       public function releaseLock($key);
-
-       /**
-        * @brief Releases all lock that were set by us
-        *
-        * @return void
-        */
-       public function releaseAll();
-}
diff --git a/src/Util/Lock/MemcacheLockDriver.php b/src/Util/Lock/MemcacheLockDriver.php
deleted file mode 100644 (file)
index d0789a9..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-<?php
-
-namespace Friendica\Util\Lock;
-
-use Friendica\Core\Cache;
-use dba;
-
-class MemcacheLockDriver implements ILockDriver
-{
-       /**
-        *
-        * @brief Sets a lock for a given name
-        *
-        * @param string $key The Name of the lock
-        * @param integer $timeout Seconds until we give up
-        *
-        * @return boolean Was the lock successful?
-        */
-       public function acquireLock($key, $timeout = 120)
-       {
-               $got_lock = false;
-               $start = time();
-
-               $cachekey = get_app()->get_hostname() . ";lock:" . $key;
-
-               do {
-                       // We only lock to be sure that nothing happens at exactly the same time
-                       dba::lock('locks');
-                       $lock = Cache::get($cachekey);
-
-                       if (!is_bool($lock)) {
-                               $pid = (int)$lock;
-
-                               // When the process id isn't used anymore, we can safely claim the lock for us.
-                               // Or we do want to lock something that was already locked by us.
-                               if (!posix_kill($pid, 0) || ($pid == getmypid())) {
-                                       $lock = false;
-                               }
-                       }
-                       if (is_bool($lock)) {
-                               Cache::set($cachekey, getmypid(), 300);
-                               $got_lock = true;
-                       }
-
-                       dba::unlock();
-
-                       if (!$got_lock && ($timeout > 0)) {
-                               usleep(rand(10000, 200000));
-                       }
-               } while (!$got_lock && ((time() - $start) < $timeout));
-
-               return $got_lock;
-       }
-
-       /**
-        * @brief Removes a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
-        */
-       public function releaseLock($key)
-       {
-               $cachekey = get_app()->get_hostname() . ";lock:" . $key;
-               $lock = Cache::get($cachekey);
-
-               if (!is_bool($lock)) {
-                       if ((int)$lock == getmypid()) {
-                               Cache::delete($cachekey);
-                       }
-               }
-
-               return;
-       }
-
-       /**
-        * @brief Removes all lock that were set by us
-        *
-        * @return void
-        */
-       public function releaseAll()
-       {
-               // We cannot delete all cache entries, but this doesn't matter with memcache
-               return;
-       }
-}
diff --git a/src/Util/Lock/SemaphoreLockDriver.php b/src/Util/Lock/SemaphoreLockDriver.php
deleted file mode 100644 (file)
index 1cd3af1..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-<?php
-
-namespace Friendica\Util\Lock;
-
-class SemaphoreLockDriver implements ILockDriver
-{
-       /**
-        * @var array stored semaphores
-        */
-       private static $semaphore = [];
-
-       /**
-        * @brief Creates a semaphore key
-        *
-        * @param string $key Name of the lock
-        *
-        * @return integer the semaphore key
-        */
-       private static function semaphoreKey($key)
-       {
-               $temp = get_temppath();
-
-               $file = $temp.'/'.$key.'.sem';
-
-               if (!file_exists($file)) {
-                       file_put_contents($file, $key);
-               }
-
-               return ftok($file, 'f');
-       }
-
-       /**
-        *
-        * @brief Sets a lock for a given name
-        *
-        * @param string $key The Name of the lock
-        * @param integer $timeout Seconds until we give up
-        *
-        * @return boolean Was the lock successful?
-        */
-       public function acquireLock($key, $timeout = 120)
-       {
-               // The second parameter for "sem_acquire" doesn't exist before 5.6.1
-               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                       self::$semaphore[$key] = sem_get(self::semaphoreKey($key));
-                       if (self::$semaphore[$key]) {
-                               return sem_acquire(self::$semaphore[$key], ($timeout == 0));
-                       }
-               }
-       }
-
-       /**
-        * @brief Removes a lock if it was set by us
-        *
-        * @param string $key Name of the lock
-        *
-        * @return mixed
-        */
-       public function releaseLock($key)
-       {
-               if (function_exists('sem_get') && version_compare(PHP_VERSION, '5.6.1', '>=')) {
-                       if (empty(self::$semaphore[$key])) {
-                               return false;
-                       } else {
-                               $success = @sem_release(self::$semaphore[$key]);
-                               unset(self::$semaphore[$key]);
-                               return $success;
-                       }
-               }
-       }
-
-       /**
-        * @brief Removes all lock that were set by us
-        *
-        * @return void
-        */
-       public function releaseAll()
-       {
-               // not needed/supported
-               return;
-       }
-}