1 | <?php |
||
13 | class Manager |
||
14 | { |
||
15 | const MIN_DRIFT = 2; |
||
16 | |||
17 | const UNLOCK_SCRIPT = ' |
||
18 | if redis.call("GET", KEYS[1]) == ARGV[1] then |
||
19 | return redis.call("DEL", KEYS[1]) |
||
20 | else |
||
21 | return 0 |
||
22 | end |
||
23 | '; |
||
24 | |||
25 | /** |
||
26 | * @var int Delay millisecond after fail to get lock |
||
27 | */ |
||
28 | private $retryDelay; |
||
29 | /** |
||
30 | * @var int Number of times after fail to get lock |
||
31 | */ |
||
32 | private $retryCount; |
||
33 | /** |
||
34 | * @var float |
||
35 | * Clock drift coefficient, |
||
36 | * to prevent redis and web time to pass the speed of different, |
||
37 | * resulting in lock early failure of the insurance value |
||
38 | */ |
||
39 | private $clockDriftFactor = 0.01; |
||
40 | /** |
||
41 | * @var Pool |
||
42 | */ |
||
43 | private $pool; |
||
44 | |||
45 | |||
46 | 21 | public function __construct(Pool $pool, $retryDelay = 200, $retryCount = 3) |
|
52 | |||
53 | /** |
||
54 | * $ttl's unit is milliseconds, min $ttl is 2 milliseconds which means you will aways |
||
55 | * get a lock which is expired |
||
56 | * @param string $resource |
||
57 | * @param int $ttl |
||
58 | * @param bool $autoRelease |
||
59 | * @return Lock|bool |
||
60 | */ |
||
61 | 5 | public function lock($resource, $ttl, $autoRelease = false) |
|
101 | |||
102 | /** |
||
103 | * @param Lock $lock |
||
104 | * @throws ManagerCompareException |
||
105 | */ |
||
106 | 6 | public function unlock(Lock $lock) |
|
117 | |||
118 | 7 | private static function unlockServer(Server $server, $resource, $token) |
|
123 | |||
124 | 10 | public function available() |
|
141 | } |