| 1 | <?php |
||
| 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 = [ ]) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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( |
||
| 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) |
||
| 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) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get a Predis client instance for the master. |
||
| 221 | * |
||
| 222 | * @return Client The client instance for the current master. |
||
|
1 ignored issue
–
show
|
|||
| 223 | */ |
||
| 224 | protected function getMaster() |
||
| 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) |
||
| 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.