@@ -39,6 +39,7 @@ class Redis implements Adapter
3939 'persistent ' => null , // phpredis sentinel persistence parameter
4040 'retry_interval ' => 0 , // phpredis sentinel retry interval
4141 '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
4243 'username ' => '' , // phpredis sentinel auth username
4344 'password ' => '' , // phpredis sentinel auth password
4445 'ssl ' => null ,
@@ -95,15 +96,13 @@ public function __construct(array $options = [])
9596 * Sentinels descoverMaster
9697 * @param array $options
9798 */
98- public function isSentinel (array $ options = [])
99+ public function getSentinelPrimary (array $ options = [])
99100 {
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 ' ];
107106 return $ options ;
108107 }
109108
@@ -262,22 +261,30 @@ private function ensureOpenConnection(): void
262261 return ;
263262 }
264263
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+ }
278281 }
282+ $ retries ++;
279283 }
284+ } else {
285+ $ this ->connectToServer ();
280286 }
287+
281288
282289
283290 $ authParams = [];
0 commit comments