1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\mutex; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidArgumentException; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* PgsqlMutex implements mutex "lock" mechanism via PgSQL locks. |
15
|
|
|
* |
16
|
|
|
* Application configuration example: |
17
|
|
|
* |
18
|
|
|
* ``` |
19
|
|
|
* [ |
20
|
|
|
* 'components' => [ |
21
|
|
|
* 'db' => [ |
22
|
|
|
* 'class' => 'yii\db\Connection', |
23
|
|
|
* 'dsn' => 'pgsql:host=127.0.0.1;dbname=demo', |
24
|
|
|
* ] |
25
|
|
|
* 'mutex' => [ |
26
|
|
|
* 'class' => 'yii\mutex\PgsqlMutex', |
27
|
|
|
* ], |
28
|
|
|
* ], |
29
|
|
|
* ] |
30
|
|
|
* ``` |
31
|
|
|
* |
32
|
|
|
* @see Mutex |
33
|
|
|
* |
34
|
|
|
* @author nineinchnick <[email protected]> |
35
|
|
|
* @since 2.0.8 |
36
|
|
|
*/ |
37
|
|
|
class PgsqlMutex extends DbMutex |
38
|
|
|
{ |
39
|
|
|
use RetryAcquireTrait; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Initializes PgSQL specific mutex component implementation. |
44
|
|
|
* @throws InvalidConfigException if [[db]] is not PgSQL connection. |
45
|
|
|
*/ |
46
|
10 |
|
public function init() |
47
|
|
|
{ |
48
|
10 |
|
parent::init(); |
49
|
10 |
|
if ($this->db->driverName !== 'pgsql') { |
50
|
|
|
throw new InvalidConfigException('In order to use PgsqlMutex connection must be configured to use PgSQL database.'); |
51
|
|
|
} |
52
|
10 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Converts a string into two 16 bit integer keys using the SHA1 hash function. |
56
|
|
|
* @param string $name |
57
|
|
|
* @return array contains two 16 bit integer keys |
58
|
|
|
*/ |
59
|
10 |
|
private function getKeysFromName($name) |
60
|
|
|
{ |
61
|
10 |
|
return array_values(unpack('n2', sha1($name, true))); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Acquires lock by given name. |
66
|
|
|
* @param string $name of the lock to be acquired. |
67
|
|
|
* @param int $timeout time (in seconds) to wait for lock to become released. |
68
|
|
|
* @return bool acquiring result. |
69
|
|
|
* @see http://www.postgresql.org/docs/9.0/static/functions-admin.html |
70
|
|
|
*/ |
71
|
10 |
|
protected function acquireLock($name, $timeout = 0) |
72
|
|
|
{ |
73
|
10 |
|
list($key1, $key2) = $this->getKeysFromName($name); |
74
|
|
|
|
75
|
|
|
return $this->retryAcquire($timeout, function () use ($key1, $key2) { |
76
|
|
|
return $this->db->useMaster(function ($db) use ($key1, $key2) { |
77
|
|
|
/** @var \yii\db\Connection $db */ |
78
|
10 |
|
return (bool) $db->createCommand( |
79
|
10 |
|
'SELECT pg_try_advisory_lock(:key1, :key2)', |
80
|
10 |
|
[':key1' => $key1, ':key2' => $key2] |
81
|
10 |
|
)->queryScalar(); |
82
|
10 |
|
}); |
83
|
10 |
|
}); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Releases lock by given name. |
88
|
|
|
* @param string $name of the lock to be released. |
89
|
|
|
* @return bool release result. |
90
|
|
|
* @see http://www.postgresql.org/docs/9.0/static/functions-admin.html |
91
|
|
|
*/ |
92
|
10 |
|
protected function releaseLock($name) |
93
|
|
|
{ |
94
|
10 |
|
list($key1, $key2) = $this->getKeysFromName($name); |
95
|
10 |
|
return $this->db->useMaster(function ($db) use ($key1, $key2) { |
96
|
|
|
/** @var \yii\db\Connection $db */ |
97
|
10 |
|
return (bool) $db->createCommand( |
98
|
10 |
|
'SELECT pg_advisory_unlock(:key1, :key2)', |
99
|
10 |
|
[':key1' => $key1, ':key2' => $key2] |
100
|
10 |
|
)->queryScalar(); |
101
|
10 |
|
}); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|