1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Monospice\LaravelRedisSentinel; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Predis\Client; |
8
|
|
|
use Predis\CommunicationException; |
9
|
|
|
use Predis\PubSub\Consumer as PubSub; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Executes Redis commands using the Predis client. |
14
|
|
|
* |
15
|
|
|
* This package extends the Predis client to work around issues experienced |
16
|
|
|
* when using the it to send commands over "aggregate" connections (in this |
17
|
|
|
* case, Sentinel connections). |
18
|
|
|
* |
19
|
|
|
* Unlike in the 2.x branch, this PredisConnection implementation extends the |
20
|
|
|
* Predis client instead of consuming it as a dependency so we don't break |
21
|
|
|
* backward compatibility in Laravel 5.3 and below. |
22
|
|
|
* |
23
|
|
|
* @category Package |
24
|
|
|
* @package Monospice\LaravelRedisSentinel |
25
|
|
|
* @author Cy Rossignol <[email protected]> |
26
|
|
|
* @license See LICENSE file |
27
|
|
|
* @link https://github.com/monospice/laravel-redis-sentinel-drivers |
28
|
|
|
*/ |
29
|
|
|
class PredisConnection extends Client |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The number of times the client attempts to retry a command when it fails |
33
|
|
|
* to connect to a Redis instance behind Sentinel. |
34
|
|
|
* |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
protected $retryLimit = 20; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The time in milliseconds to wait before the client retries a failed |
41
|
|
|
* command. |
42
|
|
|
* |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
protected $retryWait = 1000; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Set the default amount of time to wait before determining that a |
49
|
|
|
* connection attempt to a Sentinel server failed. |
50
|
|
|
* |
51
|
|
|
* @param float $seconds The timeout value in seconds. |
52
|
|
|
* |
53
|
|
|
* @return $this The current instance for method chaining. |
54
|
|
|
*/ |
55
|
|
|
public function setSentinelTimeout($seconds) |
56
|
|
|
{ |
57
|
|
|
$this->getConnection()->setSentinelTimeout($seconds); |
|
|
|
|
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the default number of attempts to retry a command when the client |
64
|
|
|
* fails to connect to a Redis instance behind Sentinel. |
65
|
|
|
* |
66
|
|
|
* @param int $attempts With a value of 0, throw an exception after the |
67
|
|
|
* first failed attempt. Pass a value of -1 to retry connections forever. |
68
|
|
|
* |
69
|
|
|
* @return $this The current instance for method chaining. |
70
|
|
|
*/ |
71
|
|
|
public function setRetryLimit($attempts) |
72
|
|
|
{ |
73
|
|
|
$this->retryLimit = (int) $attempts; |
74
|
|
|
$this->getConnection()->setRetryLimit($attempts); |
|
|
|
|
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the time to wait before retrying a command after a connection |
81
|
|
|
* attempt failed. |
82
|
|
|
* |
83
|
|
|
* @param int $milliseconds The wait time in milliseconds. When 0, retry |
84
|
|
|
* a failed command immediately. |
85
|
|
|
* |
86
|
|
|
* @return $this The current instance for method chaining. |
87
|
|
|
*/ |
88
|
|
|
public function setRetryWait($milliseconds) |
89
|
|
|
{ |
90
|
|
|
$this->retryWait = (int) $milliseconds; |
91
|
|
|
$this->getConnection()->setRetryWait($milliseconds); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set whether the client should update the list of known Sentinels each |
98
|
|
|
* time it needs to connect to a Redis server behind Sentinel. |
99
|
|
|
* |
100
|
|
|
* @param bool $enable If TRUE, fetch the updated Sentinel list. |
101
|
|
|
* |
102
|
|
|
* @return $this The current instance for method chaining. |
103
|
|
|
*/ |
104
|
|
|
public function setUpdateSentinels($enable) |
105
|
|
|
{ |
106
|
|
|
$this->getConnection()->setUpdateSentinels($enable); |
|
|
|
|
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Subscribe to a set of given channels for messages. |
113
|
|
|
* |
114
|
|
|
* @param array|string $channels The names of the channels to subscribe to. |
115
|
|
|
* @param Closure $callback Executed for each message. Receives the |
116
|
|
|
* message string in the first argument and the message channel as the |
117
|
|
|
* second argument. Return FALSE to unsubscribe. |
118
|
|
|
* @param string $method The subscription command ("subscribe" or |
119
|
|
|
* "psubscribe"). |
120
|
|
|
* |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function createSubscription( |
124
|
|
|
$channels, |
125
|
|
|
Closure $callback, |
126
|
|
|
$method = 'subscribe' |
127
|
|
|
) { |
128
|
|
|
$this->retryOnFailure(function () use ($method, $channels, $callback) { |
129
|
|
|
$loop = $this->pubSubLoop([ $method => (array) $channels ]); |
130
|
|
|
|
131
|
|
|
if ($method === 'psubscribe') { |
132
|
|
|
$messageKind = 'pmessage'; |
133
|
|
|
} else { |
134
|
|
|
$messageKind = 'message'; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$this->consumeMessages($loop, $messageKind, $callback); |
|
|
|
|
138
|
|
|
|
139
|
|
|
unset($loop); |
140
|
|
|
}); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Create a new PUB/SUB subscriber and pass messages to the callback if |
145
|
|
|
* provided. |
146
|
|
|
* |
147
|
|
|
* WARNING: Consumers created using this method are not monitored for |
148
|
|
|
* connection failures. For Sentinel support, use one of the methods |
149
|
|
|
* provided by the Laravel API instead (subscribe() and psubscribe()). |
150
|
|
|
* |
151
|
|
|
* @param array|null $options Configures the channel(s) to subscribe to. |
152
|
|
|
* @param callable $callback Optional callback executed for each message |
|
|
|
|
153
|
|
|
* published to the configured channel(s). |
154
|
|
|
* |
155
|
|
|
* @return \Predis\PubSub\Consumer|null A PUB/SUB context used to create |
156
|
|
|
* a subscription loop if no callback provided. |
157
|
|
|
*/ |
158
|
|
|
public function pubSubLoop($options = null, $callback = null) |
159
|
|
|
{ |
160
|
|
|
// Messages published to the master propagate to each of the slaves. We |
161
|
|
|
// pick a random slave to distribute load away from the master: |
162
|
|
|
return $this->getRandomSlave()->pubSubLoop($options, $callback); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Execute commands in a transaction. |
167
|
|
|
* |
168
|
|
|
* This package overrides the transaction() method to work around a |
169
|
|
|
* limitation in the Predis API that disallows transactions on "aggregate" |
170
|
|
|
* connections like Sentinel. Note that transactions execute on the Redis |
171
|
|
|
* master instance. |
172
|
|
|
* |
173
|
|
|
* @param callable|null $callback Contains the Redis commands to execute in |
174
|
|
|
* the transaction. The callback receives a Predis\Transaction\MultiExec |
175
|
|
|
* transaction abstraction as the only argument. We use this object to |
176
|
|
|
* execute Redis commands by calling its methods just like we would with |
177
|
|
|
* the Laravel Redis service. |
178
|
|
|
* |
179
|
|
|
* @return array|Predis\Transaction\MultiExec An array containing the |
180
|
|
|
* result for each command executed during the transaction. If no callback |
181
|
|
|
* provided, returns an instance of the Predis transaction abstraction. |
182
|
|
|
*/ |
183
|
|
|
public function transaction(callable $callback = null) |
184
|
|
|
{ |
185
|
|
|
return $this->retryOnFailure(function () use ($callback) { |
186
|
|
|
return $this->getMaster()->transaction($callback); |
187
|
|
|
}); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Creates a new client instance for the specified connection ID or alias, |
192
|
|
|
* only when working with an aggregate connection (cluster, replication). |
193
|
|
|
* The new client instances uses the same options of the original one. |
194
|
|
|
* |
195
|
|
|
* @param string $connectionID Identifier of a connection. |
196
|
|
|
* |
197
|
|
|
* @return Client A Predis client instance for the specified connection. |
198
|
|
|
* |
199
|
|
|
* @throws InvalidArgumentException When the aggregate connection does not |
200
|
|
|
* contain a node that matches the specified ID. |
201
|
|
|
*/ |
202
|
|
|
public function getClientFor($connectionID) |
203
|
|
|
{ |
204
|
|
|
if (! $connection = $this->getConnectionById($connectionID)) { |
205
|
|
|
throw new InvalidArgumentException( |
206
|
|
|
"Invalid connection ID: $connectionID." |
|
|
|
|
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// Because this class extends the Predis client, we need to return an |
211
|
|
|
// instance of the base client class so that clients created for the |
212
|
|
|
// Redis servers behind Sentinel don't implement the overrides here. |
213
|
|
|
return new parent($connection, $this->options); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Attempt to retry the provided operation when the client fails to connect |
218
|
|
|
* to a Redis server. |
219
|
|
|
* |
220
|
|
|
* We adapt Predis' Sentinel connection failure handling logic here to |
221
|
|
|
* reproduce the high-availability mode provided by the actual client. To |
222
|
|
|
* work around "aggregate" connection limitations in Predis, this class |
223
|
|
|
* provides methods that don't use the high-level Sentinel connection API |
224
|
|
|
* of Predis directly, so it needs to handle connection failures itself. |
225
|
|
|
* |
226
|
|
|
* @param callable $callback The operation to execute. |
227
|
|
|
* |
228
|
|
|
* @return mixed The result of the first successful attempt. |
229
|
|
|
* |
230
|
|
|
* @throws CommunicationException After exhausting the allowed number of |
231
|
|
|
* attempts to reconnect. |
232
|
|
|
*/ |
233
|
|
|
protected function retryOnFailure(callable $callback) |
234
|
|
|
{ |
235
|
|
|
$attempts = 0; |
236
|
|
|
|
237
|
|
|
do { |
238
|
|
|
try { |
239
|
|
|
return $callback(); |
240
|
|
|
} catch (CommunicationException $exception) { |
241
|
|
|
$exception->getConnection()->disconnect(); |
242
|
|
|
$this->getConnection()->querySentinel(); |
|
|
|
|
243
|
|
|
|
244
|
|
|
usleep($this->retryWait * 1000); |
245
|
|
|
|
246
|
|
|
$attempts++; |
247
|
|
|
} |
248
|
|
|
} while ($attempts <= $this->retryLimit); |
249
|
|
|
|
250
|
|
|
throw $exception; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Execute the provided callback for each message read by the PUB/SUB |
255
|
|
|
* consumer. |
256
|
|
|
* |
257
|
|
|
* @param PubSub $loop Reads the messages published to a channel. |
258
|
|
|
* @param string $kind The subscribed message type ([p]message). |
259
|
|
|
* @param Closure $callback Executed for each message. |
260
|
|
|
* |
261
|
|
|
* @return void |
262
|
|
|
*/ |
263
|
|
|
protected function consumeMessages(PubSub $loop, $kind, Closure $callback) |
264
|
|
|
{ |
265
|
|
|
foreach ($loop as $message) { |
266
|
|
|
if ($message->kind === $kind) { |
267
|
|
|
if ($callback($message->payload, $message->channel) === false) { |
268
|
|
|
return; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Get a Predis client instance for the master. |
276
|
|
|
* |
277
|
|
|
* @return Client The client instance for the current master. |
278
|
|
|
*/ |
279
|
|
|
protected function getMaster() |
280
|
|
|
{ |
281
|
|
|
return $this->getClientFor('master'); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Get a Predis client instance for a random slave. |
286
|
|
|
* |
287
|
|
|
* @param bool $fallbackToMaster If TRUE, return a client for the master |
288
|
|
|
* if the connection does not include any slaves. |
289
|
|
|
* |
290
|
|
|
* @return Client The client instance for the selected slave. |
291
|
|
|
* |
292
|
|
|
* @throws RuntimeException When the client cannot reach any replicas |
293
|
|
|
* (and the master if $fallbackToMaster is TRUE). |
294
|
|
|
*/ |
295
|
|
|
protected function getRandomSlave($fallbackToMaster = true) |
296
|
|
|
{ |
297
|
|
|
$slaves = $this->getConnection()->getSlaves(); |
|
|
|
|
298
|
|
|
|
299
|
|
|
if (count($slaves) > 0) { |
300
|
|
|
$slave = $slaves[rand(1, count($slaves)) - 1]; |
301
|
|
|
|
302
|
|
|
return $this->getClientFor($slave->getParameters()->alias); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
if ($fallbackToMaster) { |
306
|
|
|
return $this->getMaster(); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
throw new RuntimeException('No slave present on connection.'); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: