1 | <?php |
||
26 | class PredisConnection extends LaravelPredisConnection |
||
27 | { |
||
28 | /** |
||
29 | * The number of times the client attempts to retry a command when it fails |
||
30 | * to connect to a Redis instance behind Sentinel. |
||
31 | * |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $retryLimit = 20; |
||
35 | |||
36 | /** |
||
37 | * The time in milliseconds to wait before the client retries a failed |
||
38 | * command. |
||
39 | * |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $retryWait = 1000; |
||
43 | |||
44 | /** |
||
45 | * Create a Redis Sentinel connection using a Predis client. |
||
46 | * |
||
47 | * @param Client $client The Redis client to wrap. |
||
48 | * @param array $sentinelOptions Sentinel-specific connection options. |
||
49 | */ |
||
50 | public function __construct(Client $client, array $sentinelOptions = [ ]) |
||
62 | |||
63 | /** |
||
64 | * Set the default amount of time to wait before determining that a |
||
65 | * connection attempt to a Sentinel server failed. |
||
66 | * |
||
67 | * @param float $seconds The timeout value in seconds. |
||
68 | * |
||
69 | * @return $this The current instance for method chaining. |
||
70 | */ |
||
71 | public function setSentinelTimeout($seconds) |
||
77 | |||
78 | /** |
||
79 | * Set the default number of attempts to retry a command when the client |
||
80 | * fails to connect to a Redis instance behind Sentinel. |
||
81 | * |
||
82 | * @param int $attempts With a value of 0, throw an exception after the |
||
83 | * first failed attempt. Pass a value of -1 to retry connections forever. |
||
84 | * |
||
85 | * @return $this The current instance for method chaining. |
||
86 | */ |
||
87 | public function setRetryLimit($attempts) |
||
94 | |||
95 | /** |
||
96 | * Set the time to wait before retrying a command after a connection |
||
97 | * attempt failed. |
||
98 | * |
||
99 | * @param int $milliseconds The wait time in milliseconds. When 0, retry |
||
100 | * a failed command immediately. |
||
101 | * |
||
102 | * @return $this The current instance for method chaining. |
||
103 | */ |
||
104 | public function setRetryWait($milliseconds) |
||
111 | |||
112 | /** |
||
113 | * Set whether the client should update the list of known Sentinels each |
||
114 | * time it needs to connect to a Redis server behind Sentinel. |
||
115 | * |
||
116 | * @param bool $enable If TRUE, fetch the updated Sentinel list. |
||
117 | * |
||
118 | * @return $this The current instance for method chaining. |
||
119 | */ |
||
120 | public function setUpdateSentinels($enable) |
||
126 | |||
127 | /** |
||
128 | * Subscribe to a set of given channels for messages. |
||
129 | * |
||
130 | * @param array|string $channels The names of the channels to subscribe to. |
||
131 | * @param Closure $callback Executed for each message. Receives the |
||
132 | * message string in the first argument and the message channel as the |
||
133 | * second argument. Return FALSE to unsubscribe. |
||
134 | * @param string $method The subscription command ("subscribe" or |
||
135 | * "psubscribe"). |
||
136 | * |
||
137 | * @return void |
||
138 | */ |
||
139 | public function createSubscription( |
||
158 | |||
159 | /** |
||
160 | * Create a new PUB/SUB subscriber and pass messages to the callback if |
||
161 | * provided. |
||
162 | * |
||
163 | * WARNING: Consumers created using this method are not monitored for |
||
164 | * connection failures. For Sentinel support, use one of the methods |
||
165 | * provided by the Laravel API instead (subscribe() and psubscribe()). |
||
166 | * |
||
167 | * @param array|null $options Configures the channel(s) to subscribe to. |
||
168 | * @param callable $callback Optional callback executed for each message |
||
169 | * published to the configured channel(s). |
||
170 | * |
||
171 | * @return \Predis\PubSub\Consumer|null A PUB/SUB context used to create |
||
172 | * a subscription loop if no callback provided. |
||
173 | */ |
||
174 | public function pubSubLoop($options = null, $callback = null) |
||
180 | |||
181 | /** |
||
182 | * Execute commands in a transaction. |
||
183 | * |
||
184 | * This package overrides the transaction() method to work around a |
||
185 | * limitation in the Predis API that disallows transactions on "aggregate" |
||
186 | * connections like Sentinel. Note that transactions execute on the Redis |
||
187 | * master instance. |
||
188 | * |
||
189 | * @param callable|null $callback Contains the Redis commands to execute in |
||
190 | * the transaction. The callback receives a Predis\Transaction\MultiExec |
||
191 | * transaction abstraction as the only argument. We use this object to |
||
192 | * execute Redis commands by calling its methods just like we would with |
||
193 | * the Laravel Redis service. |
||
194 | * |
||
195 | * @return array|Predis\Transaction\MultiExec An array containing the |
||
196 | * result for each command executed during the transaction. If no callback |
||
197 | * provided, returns an instance of the Predis transaction abstraction. |
||
198 | */ |
||
199 | public function transaction(callable $callback = null) |
||
205 | |||
206 | /** |
||
207 | * Attempt to retry the provided operation when the client fails to connect |
||
208 | * to a Redis server. |
||
209 | * |
||
210 | * We adapt Predis' Sentinel connection failure handling logic here to |
||
211 | * reproduce the high-availability mode provided by the actual client. To |
||
212 | * work around "aggregate" connection limitations in Predis, this class |
||
213 | * provides methods that don't use the high-level Sentinel connection API |
||
214 | * of Predis directly, so it needs to handle connection failures itself. |
||
215 | * |
||
216 | * @param callable $callback The operation to execute. |
||
217 | * |
||
218 | * @return mixed The result of the first successful attempt. |
||
219 | */ |
||
220 | protected function retryOnFailure(callable $callback) |
||
239 | |||
240 | /** |
||
241 | * Execute the provided callback for each message read by the PUB/SUB |
||
242 | * consumer. |
||
243 | * |
||
244 | * @param PubSub $loop Reads the messages published to a channel. |
||
245 | * @param string $kind The subscribed message type ([p]message). |
||
246 | * @param Closure $callback Executed for each message. |
||
247 | * |
||
248 | * @return void |
||
249 | */ |
||
250 | protected function consumeMessages(PubSub $loop, $kind, Closure $callback) |
||
260 | |||
261 | /** |
||
262 | * Get a Predis client instance for the master. |
||
263 | * |
||
264 | * @return Client The client instance for the current master. |
||
265 | */ |
||
266 | protected function getMaster() |
||
270 | |||
271 | /** |
||
272 | * Get a Predis client instance for a random slave. |
||
273 | * |
||
274 | * @param bool $fallbackToMaster If TRUE, return a client for the master |
||
275 | * if the connection does not include any slaves. |
||
276 | * |
||
277 | * @return Client The client instance for the selected slave. |
||
278 | */ |
||
279 | protected function getRandomSlave($fallbackToMaster = true) |
||
295 | } |
||
296 |
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.