Skip to content

Commit ab5e2bd

Browse files
committed
Switch Locker to Mutex (Close #47)
1 parent 6d5ac12 commit ab5e2bd

File tree

3 files changed

+26
-93
lines changed

3 files changed

+26
-93
lines changed

src/components/Storage.php

+21-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace hiqdev\assetpackagist\components;
1212

1313
use hiqdev\assetpackagist\exceptions\AssetFileStorageException;
14-
use hiqdev\assetpackagist\helpers\Locker;
1514
use hiqdev\assetpackagist\models\AssetPackage;
1615
use Yii;
1716
use yii\base\Component;
@@ -30,26 +29,35 @@ public function init()
3029
$this->_path = Yii::getAlias('@storage', false);
3130
}
3231

33-
protected function getLocker()
32+
protected function acquireLock()
3433
{
35-
if ($this->_locker === null) {
36-
$this->_locker = Locker::getInstance($this->buildPath('lock')); // TODO: get rid of singleton
34+
/* @var $mutex \yii\mutex\Mutex */
35+
$mutex = Yii::$app->mutex;
36+
37+
if (!$mutex->acquire('lock', 5)) {
38+
throw new \Exception('failed get lock');
3739
}
40+
}
41+
42+
protected function releaseLock()
43+
{
44+
/* @var $mutex \yii\mutex\Mutex */
45+
$mutex = Yii::$app->mutex;
3846

39-
return $this->_locker;
47+
$mutex->release('lock');
4048
}
4149

4250
/**
4351
* {@inheritdoc}
4452
*/
4553
public function getNextId()
4654
{
47-
$this->getLocker()->lock();
55+
$this->acquireLock();
4856
{
4957
$nextID = $this->readLastId() + 1;
5058
$this->writeLastId($nextID);
5159
}
52-
$this->getLocker()->release();
60+
$this->releaseLock();
5361

5462
return $nextID;
5563
}
@@ -89,7 +97,7 @@ public function writePackage(AssetPackage $package)
8997
$path = $this->buildHashedPath($name, $hash);
9098
$latestPath = $this->buildHashedPath($name);
9199
if (!file_exists($path)) {
92-
$this->getLocker()->lock();
100+
$this->acquireLock();
93101
try {
94102
if ($this->mkdir(dirname($path)) === false) {
95103
throw new AssetFileStorageException('Failed to create a directory for asset-package', $package);
@@ -102,7 +110,7 @@ public function writePackage(AssetPackage $package)
102110
}
103111
$this->writeProviderLatest($name, $hash);
104112
} finally {
105-
$this->getLocker()->release();
113+
$this->releaseLock();
106114
}
107115
} else {
108116
touch($latestPath);
@@ -162,7 +170,7 @@ protected function writeProviderLatest($name, $hash)
162170
$path = $this->buildHashedPath('provider-latest', $hash);
163171

164172
if (!file_exists($path)) {
165-
$this->getLocker()->lock();
173+
$this->acquireLock();
166174

167175
try {
168176
if ($this->mkdir(dirname($path)) === false) {
@@ -176,7 +184,7 @@ protected function writeProviderLatest($name, $hash)
176184
}
177185
$this->writePackagesJson($hash);
178186
} finally {
179-
$this->getLocker()->release();
187+
$this->releaseLock();
180188
}
181189
} else {
182190
touch($latestPath);
@@ -195,15 +203,15 @@ protected function writePackagesJson($hash)
195203
],
196204
],
197205
];
198-
$this->getLocker()->lock();
206+
$this->acquireLock();
199207
$filename = $this->buildPath('packages.json');
200208
try {
201209
if (file_put_contents($filename, Json::encode($data)) === false) {
202210
throw new AssetFileStorageException('Failed to write main packages.json');
203211
}
204212
touch($filename);
205213
} finally {
206-
$this->getLocker()->release();
214+
$this->releaseLock();
207215
}
208216
}
209217

src/config/common.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@
2424
'password' => $params['db.password'],
2525
'charset' => 'utf8',
2626
],
27+
'mutex' => [
28+
'class' => \yii\mutex\MysqlMutex::class,
29+
'db' => 'db',
30+
],
2731
'queue' => [
2832
'class' => \zhuravljov\yii\queue\db\Queue::class,
2933
'db' => 'db',
3034
'tableName' => '{{%queue}}',
3135
'channel' => 'package',
32-
'mutex' => \yii\mutex\MysqlMutex::class,
36+
'mutex' => 'mutex',
3337
'deleteReleased' => true,
3438
],
3539
'packageStorage' => [

src/helpers/Locker.php

-79
This file was deleted.

0 commit comments

Comments
 (0)