Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Yii Framework 2 redis extension Change Log
------------------------

- Enh #195: Use `Instance::ensure()` to initialize `Session::$redis` (rob006)
- Enh #199: Increase frequency of lock tries when `$timeout` is used in `Mutex::acquire()` (rob006)


2.0.11 November 05, 2019
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
}
],
"require": {
"yiisoft/yii2": "~2.0.14"
"yiisoft/yii2": "~2.0.16"
},
"require-dev": {
"phpunit/phpunit": "<7",
"yiisoft/yii2-dev": "~2.0.14"
"yiisoft/yii2-dev": "~2.0.16"
},
"autoload": {
"psr-4": { "yii\\redis\\": "src" }
Expand Down
20 changes: 11 additions & 9 deletions src/Mutex.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yii;
use yii\base\InvalidConfigException;
use yii\di\Instance;
use yii\mutex\RetryAcquireTrait;

/**
* Redis Mutex implements a mutex component using [redis](http://redis.io/) as the storage medium.
Expand Down Expand Up @@ -58,6 +59,8 @@
*/
class Mutex extends \yii\mutex\Mutex
{
use RetryAcquireTrait;

/**
* @var int the number of seconds in which the lock will be auto released.
*/
Expand Down Expand Up @@ -109,16 +112,15 @@ protected function acquireLock($name, $timeout = 0)
{
$key = $this->calculateKey($name);
$value = Yii::$app->security->generateRandomString(20);
$waitTime = 0;
while (!$this->redis->executeCommand('SET', [$key, $value, 'NX', 'PX', (int) ($this->expire * 1000)])) {
$waitTime++;
if ($waitTime > $timeout) {
return false;
}
sleep(1);

$result = $this->retryAcquire($timeout, function () use ($key, $value) {
return $this->redis->executeCommand('SET', [$key, $value, 'NX', 'PX', (int) ($this->expire * 1000)]);
});

if ($result) {
$this->_lockValues[$name] = $value;
}
$this->_lockValues[$name] = $value;
return true;
return $result;
}

/**
Expand Down