Skip to content

Commit

Permalink
Added setDefaultLockTimeout($seconds) and getDefaultLockTimeout().
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Nov 21, 2017
1 parent e02d831 commit 1ef333a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Lock
static private $data = [];
static private $dir = null;
static private $keyPrefix = null;
static private $defaultLockTimeout = 1.5;

/**
*
Expand Down Expand Up @@ -52,6 +53,24 @@ static function setKeyPrefix($prefix)
self::$keyPrefix = $prefix;
}

/**
*
* @return string
*/
static function getDefaultLockTimeout()
{
return self::$defaultLockTimeout;
}

/**
*
* @param string $prefix
*/
static function setDefaultLockTimeout($seconds)
{
self::$defaultLockTimeout = $seconds;
}

/**
*
* @param mixed $key
Expand All @@ -61,7 +80,7 @@ static function setKeyPrefix($prefix)
static public function acquire($key, $options = [])
{
$keyMD5 = md5(self::$keyPrefix . serialize($key));
$timeout = isset($options['timeout']) ? (float) $options['timeout'] : 1.5;
$timeout = isset($options['timeout']) ? (float) $options['timeout'] : self::$defaultLockTimeout;
$retryInterval = 0.5;
$maxRetriesCount = floor($timeout / $retryInterval);
$lock = function() use ($keyMD5) {
Expand Down Expand Up @@ -118,7 +137,11 @@ static public function acquire($key, $options = [])
static public function exists($key)
{
$keyMD5 = md5(self::$keyPrefix . serialize($key));
$filename = self::getLocksDir() . $keyMD5 . '.lock';
$dir = self::getLocksDir();
if (!is_dir($dir)) {
return false;
}
$filename = $dir . $keyMD5 . '.lock';
set_error_handler(function($errno, $errstr) {
throw new \Exception($errstr);
});
Expand Down Expand Up @@ -151,6 +174,9 @@ static public function release($key)
$keyMD5 = md5(self::$keyPrefix . serialize($key));
if (!isset(self::$data[$keyMD5])) {
throw new \Exception('A lock name "' . $key . '" does not exists in current process!');
}
$dir = self::getLocksDir();
if (!is_dir($dir)) {
return;
}
set_error_handler(function($errno, $errstr) {
Expand All @@ -159,7 +185,7 @@ static public function release($key)
try {
if (flock(self::$data[$keyMD5], LOCK_UN) && fclose(self::$data[$keyMD5])) {
try {
$filename = self::getLocksDir() . $keyMD5 . '.lock';
$filename = $dir . $keyMD5 . '.lock';
$tempFilename = $filename . '.' . md5(uniqid() . rand(0, 999999));
$renameResult = rename($filename, $tempFilename);
if ($renameResult) {
Expand Down
21 changes: 21 additions & 0 deletions tests/LocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testRelease()
public function testKeyPrefix()
{
Lock::setKeyPrefix('prefix1');
$this->assertTrue(Lock::getKeyPrefix() === 'prefix1');
$this->assertTrue(Lock::exists('test1') === false);
Lock::acquire('test1');
$this->assertTrue(Lock::exists('test1') === true);
Expand All @@ -67,6 +68,26 @@ public function testExists()
$this->assertTrue(Lock::exists('test3') === false);
}

/**
*
*/
public function testTimeout()
{
Lock::setDefaultLockTimeout(2.5);
$this->assertTrue(Lock::getDefaultLockTimeout() === 2.5);
Lock::acquire('test2');
try {
$time = microtime(true);
Lock::acquire('test2');
throw new Exception('Should not get here');
} catch (Exception $e) {
$time = microtime(true) - $time;
$this->assertTrue($e->getMessage() === 'Cannot acquire lock for "test2"');
$this->assertTrue($time > 2.5);
$this->assertTrue($time < 3);
}
}

/**
*
*/
Expand Down

0 comments on commit 1ef333a

Please sign in to comment.