@@ -39,6 +39,7 @@ class Redis implements Adapter
39
39
'persistent ' => null , // phpredis sentinel persistence parameter
40
40
'retry_interval ' => 0 , // phpredis sentinel retry interval
41
41
'read_timeout ' => 0 , // phpredis sentinel read timeout
42
+ 'reconnect ' => 0 , // retries after losing connection to redis asking for a new primary, if -1 will retry indefinetely
42
43
'username ' => '' , // phpredis sentinel auth username
43
44
'password ' => '' , // phpredis sentinel auth password
44
45
'ssl ' => null ,
@@ -95,15 +96,13 @@ public function __construct(array $options = [])
95
96
* Sentinels descoverMaster
96
97
* @param array $options
97
98
*/
98
- public function isSentinel (array $ options = [])
99
+ public function getSentinelPrimary (array $ options = [])
99
100
{
100
- if ($ options ['sentinel ' ] && $ options ['sentinel ' ]['enable ' ]) {
101
- $ sentinel = new RedisSentinelConnector ();
102
- $ options ['sentinel ' ]['host ' ] = $ options ['sentinel ' ]['host ' ] ?? $ options ['host ' ];
103
- $ master = $ sentinel ->getMaster ($ options ['sentinel ' ]);
104
- $ options ['host ' ] = $ master ['ip ' ];
105
- $ options ['port ' ] = $ master ['port ' ];
106
- }
101
+ $ sentinel = new RedisSentinelConnector ();
102
+ $ options ['sentinel ' ]['host ' ] = $ options ['sentinel ' ]['host ' ] ?? $ options ['host ' ];
103
+ $ master = $ sentinel ->getMaster ($ options ['sentinel ' ]);
104
+ $ options ['host ' ] = $ master ['ip ' ];
105
+ $ options ['port ' ] = $ master ['port ' ];
107
106
return $ options ;
108
107
}
109
108
@@ -262,22 +261,30 @@ private function ensureOpenConnection(): void
262
261
return ;
263
262
}
264
263
265
- while (true ) {
266
- try {
267
- $ this ->options = $ this ->isSentinel ($ this ->options );
268
- $ this ->connectToServer ();
269
- break ;
270
- } catch (\RedisException $ e ) {
271
- $ retry = $ this ->reconnectIfRedisIsUnavailableOrReadonly ($ e );
272
- if (!$ retry ) {
273
- throw new StorageException (
274
- sprintf ("Can't connect to Redis server. %s " , $ e ->getMessage ()),
275
- $ e ->getCode (),
276
- $ e
277
- );
264
+ if ($ this ->options ['sentinel ' ] && $ this ->options ['sentinel ' ]['enable ' ]){
265
+ $ reconnect = $ this ->options ['sentinel ' ]['reconnect ' ];
266
+ $ retries = 0 ;
267
+ while ($ retries <=$ reconnect ) {
268
+ try {
269
+ $ this ->options = $ this ->getSentinelPrimary ($ this ->options );
270
+ $ this ->connectToServer ();
271
+ break ;
272
+ } catch (\RedisException $ e ) {
273
+ $ retry = $ this ->reconnectIfRedisIsUnavailableOrReadonly ($ e );
274
+ if (!$ retry ) {
275
+ throw new StorageException (
276
+ sprintf ("Can't connect to Redis server. %s " , $ e ->getMessage ()),
277
+ $ e ->getCode (),
278
+ $ e
279
+ );
280
+ }
278
281
}
282
+ $ retries ++;
279
283
}
284
+ } else {
285
+ $ this ->connectToServer ();
280
286
}
287
+
281
288
282
289
283
290
$ authParams = [];
0 commit comments