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\InvalidConfigException; |
11
|
|
|
use yii\base\InvalidParamException; |
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
|
|
|
/** |
40
|
|
|
* Initializes PgSQL specific mutex component implementation. |
41
|
|
|
* @throws InvalidConfigException if [[db]] is not PgSQL connection. |
42
|
|
|
*/ |
43
|
2 |
|
public function init() |
44
|
|
|
{ |
45
|
2 |
|
parent::init(); |
46
|
2 |
|
if ($this->db->driverName !== 'pgsql') { |
47
|
|
|
throw new InvalidConfigException('In order to use PgsqlMutex connection must be configured to use PgSQL database.'); |
48
|
|
|
} |
49
|
2 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Converts a string into two 16 bit integer keys using the SHA1 hash function. |
53
|
|
|
* @param string $name |
54
|
|
|
* @return array contains two 16 bit integer keys |
55
|
|
|
*/ |
56
|
2 |
|
private function getKeysFromName($name) |
57
|
|
|
{ |
58
|
2 |
|
return array_values(unpack('n2', sha1($name, true))); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Acquires lock by given name. |
63
|
|
|
* @param string $name of the lock to be acquired. |
64
|
|
|
* @param int $timeout to wait for lock to become released. |
65
|
|
|
* @return bool acquiring result. |
66
|
|
|
* @see http://www.postgresql.org/docs/9.0/static/functions-admin.html |
67
|
|
|
*/ |
68
|
2 |
|
protected function acquireLock($name, $timeout = 0) |
69
|
|
|
{ |
70
|
2 |
|
if ($timeout !== 0) { |
71
|
|
|
throw new InvalidParamException('PgsqlMutex does not support timeout.'); |
72
|
|
|
} |
73
|
2 |
|
list($key1, $key2) = $this->getKeysFromName($name); |
74
|
2 |
|
return (bool) $this->db |
75
|
2 |
|
->createCommand('SELECT pg_try_advisory_lock(:key1, :key2)', [':key1' => $key1, ':key2' => $key2]) |
76
|
2 |
|
->queryScalar(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Releases lock by given name. |
81
|
|
|
* @param string $name of the lock to be released. |
82
|
|
|
* @return bool release result. |
83
|
|
|
* @see http://www.postgresql.org/docs/9.0/static/functions-admin.html |
84
|
|
|
*/ |
85
|
1 |
|
protected function releaseLock($name) |
86
|
|
|
{ |
87
|
1 |
|
list($key1, $key2) = $this->getKeysFromName($name); |
88
|
1 |
|
return (bool) $this->db |
89
|
1 |
|
->createCommand('SELECT pg_advisory_unlock(:key1, :key2)', [':key1' => $key1, ':key2' => $key2]) |
90
|
1 |
|
->queryScalar(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|