-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMutexManager.php
145 lines (125 loc) · 3.48 KB
/
MutexManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Haskel\Component\Mutex;
use Haskel\Component\Mutex\Adapter\Adapter;
use Haskel\Component\Mutex\Exception\AcquireException;
use Haskel\Component\Mutex\Mutex\ExpiringMutex;
use Haskel\Component\Mutex\Mutex\Mutex;
use Closure;
use Exception;
use Haskel\Component\Mutex\Mutex\SelfReleasableMutex;
use Psr\Log\LoggerInterface;
class MutexManager
{
/**
* How long time wait for the next attempt to acquire the lock
*
* @var int
*/
private $acquireAttemptPeriod = 1;
/**
* @var Adapter
*/
private $storageAdapter;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var array
*/
private $acquiredMutexList = [];
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
/**
* @param Adapter $storageAdapter
* @todo: move to constructor arguments
*/
public function setAdapter(Adapter $storageAdapter)
{
$this->storageAdapter = $storageAdapter;
}
/**
* @param Mutex $mutex
*
* @return string
*/
private function getLockKey(Mutex $mutex)
{
$classHash = md5(strtolower(get_class($mutex)));
return "{$classHash}_{$mutex->getKey()}";
}
/**
* @param Mutex $mutex
* @param int $waitTimeout
*/
public function acquire(Mutex $mutex, $waitTimeout = 0)
{
$action = function () use ($mutex) {
$lockKey = $this->getLockKey($mutex);
// @todo: make it atomic
if ($this->storageAdapter->exists($lockKey)) {
throw new AcquireException("key '{$lockKey}' already acquired");
}
if ($mutex instanceof SelfReleasableMutex) {
$mutex->setManager($this);
}
$this->storageAdapter->create($lockKey, $mutex->getContext());
$mutexId = spl_object_hash($mutex);
$this->acquiredMutexList[$mutexId] = $mutex;
};
$action->bindTo($this);
$attemptsCount = floor($waitTimeout / $this->acquireAttemptPeriod);
$attemptsCount = $attemptsCount ?: 1;
$this->attempt($action, $attemptsCount, $this->acquireAttemptPeriod);
}
/**
* @param Mutex $mutex
*/
public function release(Mutex $mutex)
{
$lockKey = $this->getLockKey($mutex);
$mutexId = spl_object_hash($mutex);
if ($this->storageAdapter->exists($lockKey) && isset($this->acquiredMutexList[$mutexId])) {
$this->storageAdapter->delete($lockKey);
}
}
/**
* @param Mutex $mutex
*
* @return mixed
*/
public function isAcquired(Mutex $mutex)
{
$lockKey = $this->getLockKey($mutex);
return $this->storageAdapter->exists($lockKey);
}
/**
* @param Closure $action
* @param int $attemptsCount
*
* @return bool
*/
private function attempt(Closure $action, $attemptsCount = 1, $waitSeconds = 1)
{
if ($waitSeconds < 1) {
$waitSeconds = 1;
}
foreach (range(1, $attemptsCount) as $attemptNumber) {
try {
$action();
return true;
} catch (Exception $e) {
$this->logger->error(sprintf("attempt #%d: %s", $attemptNumber, $e->getMessage()));
}
sleep((int) $waitSeconds);
}
}
public function isExpired(ExpiringMutex $mutex)
{
}
public function whoAcquired(Mutex $mutex)
{
}
}