1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Monospice\LaravelRedisSentinel\Connections; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Redis\Connections\PredisConnection as LaravelPredisConnection; |
7
|
|
|
use Monospice\SpicyIdentifiers\DynamicMethod; |
8
|
|
|
use Predis\ClientInterface as Client; |
9
|
|
|
use Predis\CommunicationException; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Executes Redis commands using the Predis client. |
14
|
|
|
* |
15
|
|
|
* This package extends Laravel's PredisConnection class to work around issues |
16
|
|
|
* experienced when using the Predis client to send commands over "aggregate" |
17
|
|
|
* connections (in this case, Sentinel connections). |
18
|
|
|
* |
19
|
|
|
* @category Package |
20
|
|
|
* @package Monospice\LaravelRedisSentinel |
21
|
|
|
* @author @pdbreen, Cy Rossignol <[email protected]> |
22
|
|
|
* @license See LICENSE file |
23
|
|
|
* @link https://github.com/monospice/laravel-redis-sentinel-drivers |
24
|
|
|
*/ |
25
|
|
|
class PredisConnection extends LaravelPredisConnection |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The number of times the client attempts to retry a command when it fails |
29
|
|
|
* to connect to a Redis instance behind Sentinel. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $retryLimit = 20; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The time in milliseconds to wait before the client retries a failed |
37
|
|
|
* command. |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $retryWait = 1000; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create a Redis Sentinel connection using a Predis client. |
45
|
|
|
* |
46
|
|
|
* @param Client $client The Redis client to wrap. |
47
|
|
|
* @param array $sentinelOptions Sentinel-specific connection options. |
48
|
|
|
*/ |
49
|
|
|
public function __construct(Client $client, array $sentinelOptions = [ ]) |
50
|
|
|
{ |
51
|
|
|
parent::__construct($client); |
|
|
|
|
52
|
|
|
|
53
|
|
|
// Set the Sentinel-specific connection options on the Predis Client |
54
|
|
|
// connection and the current instance of this class. |
55
|
|
|
foreach ($sentinelOptions as $option => $value) { |
56
|
|
|
DynamicMethod::parseFromUnderscore($option) |
57
|
|
|
->prepend('set') |
58
|
|
|
->callOn($this, [ $value ]); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the default amount of time to wait before determining that a |
64
|
|
|
* connection attempt to a Sentinel server failed. |
65
|
|
|
* |
66
|
|
|
* @param float $seconds The timeout value in seconds. |
67
|
|
|
* |
68
|
|
|
* @return $this The current instance for method chaining. |
69
|
|
|
*/ |
70
|
|
|
public function setSentinelTimeout($seconds) |
71
|
|
|
{ |
72
|
|
|
$this->client->getConnection()->setSentinelTimeout($seconds); |
|
|
|
|
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set the default number of attempts to retry a command when the client |
79
|
|
|
* fails to connect to a Redis instance behind Sentinel. |
80
|
|
|
* |
81
|
|
|
* @param int $attempts With a value of 0, throw an exception after the |
82
|
|
|
* first failed attempt. Pass a value of -1 to retry connections forever. |
83
|
|
|
* |
84
|
|
|
* @return $this The current instance for method chaining. |
85
|
|
|
*/ |
86
|
|
|
public function setRetryLimit($attempts) |
87
|
|
|
{ |
88
|
|
|
$this->retryLimit = (int) $attempts; |
89
|
|
|
$this->client->getConnection()->setRetryLimit($attempts); |
|
|
|
|
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the time to wait before retrying a command after a connection |
96
|
|
|
* attempt failed. |
97
|
|
|
* |
98
|
|
|
* @param int $milliseconds The wait time in milliseconds. When 0, retry |
99
|
|
|
* a failed command immediately. |
100
|
|
|
* |
101
|
|
|
* @return $this The current instance for method chaining. |
102
|
|
|
*/ |
103
|
|
|
public function setRetryWait($milliseconds) |
104
|
|
|
{ |
105
|
|
|
$this->retryWait = (int) $milliseconds; |
106
|
|
|
$this->client->getConnection()->setRetryWait($milliseconds); |
|
|
|
|
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Set whether the client should update the list of known Sentinels each |
113
|
|
|
* time it needs to connect to a Redis server behind Sentinel. |
114
|
|
|
* |
115
|
|
|
* @param bool $enable If TRUE, fetch the updated Sentinel list. |
116
|
|
|
* |
117
|
|
|
* @return $this The current instance for method chaining. |
118
|
|
|
*/ |
119
|
|
|
public function setUpdateSentinels($enable) |
120
|
|
|
{ |
121
|
|
|
$this->client->getConnection()->setUpdateSentinels($enable); |
|
|
|
|
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Subscribe to a set of given channels for messages. |
128
|
|
|
* |
129
|
|
|
* @param array|string $channels The names of the channels to subscribe to. |
130
|
|
|
* @param Closure $callback Executed for each message. Receives the |
131
|
|
|
* message string in the first argument and the message channel as the |
132
|
|
|
* second argument. |
133
|
|
|
* @param string $method The subscription command ("subscribe" or |
134
|
|
|
* "psubscribe"). |
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
|
|
public function createSubscription( |
139
|
|
|
$channels, |
140
|
|
|
Closure $callback, |
141
|
|
|
$method = 'subscribe' |
142
|
|
|
) { |
143
|
|
|
// Messages published to the master propagate to each of the slaves. We |
144
|
|
|
// pick a random slave to distribute load away from the master: |
145
|
|
|
$loop = $this->retryOnFailure(function () { |
146
|
|
|
return $this->getRandomSlave()->pubSubLoop(); |
147
|
|
|
}); |
148
|
|
|
|
149
|
|
|
call_user_func_array([$loop, $method], (array) $channels); |
150
|
|
|
|
151
|
|
|
foreach ($loop as $message) { |
152
|
|
|
if ($message->kind === 'message' || $message->kind === 'pmessage') { |
153
|
|
|
call_user_func($callback, $message->payload, $message->channel); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
unset($loop); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Execute commands in a transaction. |
162
|
|
|
* |
163
|
|
|
* This package overrides the transaction() method to work around a |
164
|
|
|
* limitation in the Predis API that disallows transactions on "aggregate" |
165
|
|
|
* connections like Sentinel. Note that transactions execute on the Redis |
166
|
|
|
* master instance. |
167
|
|
|
* |
168
|
|
|
* @param callable $callback Contains the Redis commands to execute in the |
|
|
|
|
169
|
|
|
* transaction. The callback receives a Predis\Transaction\MultiExec |
170
|
|
|
* transaction abstraction as the only argument. We use this object to |
171
|
|
|
* execute Redis commands by calling its methods just like we would with |
172
|
|
|
* the Laravel Redis service. |
173
|
|
|
* |
174
|
|
|
* @return array|Predis\Transaction\MultiExec An array containing the |
175
|
|
|
* result for each command executed during the transaction. If no callback |
176
|
|
|
* provided, returns an instance of the Predis transaction abstraction. |
177
|
|
|
*/ |
178
|
|
|
public function transaction(callable $callback = null) |
179
|
|
|
{ |
180
|
|
|
return $this->retryOnFailure(function () use ($callback) { |
181
|
|
|
return $this->getMaster()->transaction($callback); |
182
|
|
|
}); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Attempt to retry the provided operation when the client fails to connect |
187
|
|
|
* to a Redis server. |
188
|
|
|
* |
189
|
|
|
* We adapt Predis' Sentinel connection failure handling logic here to |
190
|
|
|
* reproduce the high-availability mode provided by the actual client. To |
191
|
|
|
* work around "aggregate" connection limitations in Predis, this class |
192
|
|
|
* provides methods that don't use the high-level Sentinel connection API |
193
|
|
|
* of Predis directly, so it needs to handle connection failures itself. |
194
|
|
|
* |
195
|
|
|
* @param callable $callback The operation to execute. |
196
|
|
|
* |
197
|
|
|
* @return mixed The result of the first successful attempt. |
198
|
|
|
*/ |
199
|
|
|
protected function retryOnFailure(callable $callback) |
200
|
|
|
{ |
201
|
|
|
$attempts = 0; |
202
|
|
|
|
203
|
|
|
do { |
204
|
|
|
try { |
205
|
|
|
return $callback(); |
206
|
|
|
} catch (CommunicationException $exception) { |
207
|
|
|
$exception->getConnection()->disconnect(); |
208
|
|
|
$this->client->getConnection()->querySentinel(); |
|
|
|
|
209
|
|
|
|
210
|
|
|
usleep($this->retryWait * 1000); |
211
|
|
|
|
212
|
|
|
$attempts++; |
213
|
|
|
} |
214
|
|
|
} while ($attempts <= $this->retryLimit); |
215
|
|
|
|
216
|
|
|
throw $exception; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get a Predis client instance for the master. |
221
|
|
|
* |
222
|
|
|
* @return Client The client instance for the current master. |
|
|
|
|
223
|
|
|
*/ |
224
|
|
|
protected function getMaster() |
225
|
|
|
{ |
226
|
|
|
return $this->client->getClientFor('master'); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Get a Predis client instance for a random slave. |
231
|
|
|
* |
232
|
|
|
* @param bool $fallbackToMaster If TRUE, return a client for the master |
233
|
|
|
* if the connection does not include any slaves. |
234
|
|
|
* |
235
|
|
|
* @return Client The client instance for the selected slave. |
236
|
|
|
*/ |
237
|
|
|
protected function getRandomSlave($fallbackToMaster = true) |
238
|
|
|
{ |
239
|
|
|
$slaves = $this->client->getConnection()->getSlaves(); |
|
|
|
|
240
|
|
|
|
241
|
|
|
if (count($slaves) > 0) { |
242
|
|
|
$slave = $slaves[rand(1, count($slaves)) - 1]; |
243
|
|
|
|
244
|
|
|
return $this->client->getClientFor($slave->getParameters()->alias); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if ($fallbackToMaster) { |
248
|
|
|
return $client->getClientFor('master'); |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
throw new RuntimeException('No slave present on connection.'); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.