@@ -291,11 +291,11 @@ class Connection extends AbstractConnection
291291 /**
292292 * @var resource redis socket connection
293293 */
294- private $ _socket = false ;
294+ private $ _socket = null ;
295295 /**
296296 * @var resource redis socket connection
297297 */
298- private $ _socketSlave = false ;
298+ private $ _socketSlave = null ;
299299 /** @var bool */
300300 private $ separate = false ;
301301
@@ -315,7 +315,7 @@ public function __sleep()
315315 */
316316 public function close (bool $ quit = true )
317317 {
318- if ($ this ->_socket !== false ) {
318+ if ($ this ->_socket !== null ) {
319319 $ connection = ($ this ->unixSocket ?: $ this ->hostname . ': ' . $ this ->port ) . ', database= ' . $ this ->database ;
320320 App::warning ('Closing DB connection: ' . $ connection , 'redis ' );
321321 if ($ quit ) {
@@ -326,11 +326,11 @@ public function close(bool $quit = true)
326326 }
327327 }
328328 fclose ($ this ->_socket );
329- $ this ->_socket = false ;
329+ $ this ->_socket = null ;
330330 }
331- if ($ this ->_socketSlave !== false ) {
331+ if ($ this ->_socketSlave !== null ) {
332332 fclose ($ this ->_socketSlave );
333- $ this ->_socketSlave = false ;
333+ $ this ->_socketSlave = null ;
334334 }
335335 }
336336
@@ -363,7 +363,6 @@ public function close(bool $quit = true)
363363 */
364364 public function executeCommand (string $ name , array $ params = [])
365365 {
366- $ this ->open ();
367366 $ name = strtoupper ($ name );
368367 $ tmp = [];
369368 if ($ this ->_socketSlave && in_array ($ name , Redis::READ_COMMAND )) {
@@ -377,35 +376,28 @@ public function executeCommand(string $name, array $params = [])
377376 foreach ($ params as $ arg ) {
378377 $ command .= '$ ' . mb_strlen ($ arg , '8bit ' ) . "\r\n" . $ arg . "\r\n" ;
379378 }
380-
381379 App::debug ("Executing Redis Command: {$ name }" , 'redis ' );
382- if ($ this ->retries > 0 ) {
383- $ tries = $ this ->retries ;
384- while ($ tries -- > 0 ) {
385- try {
386- $ data = $ this ->sendCommandInternal ($ command , $ params , $ type );
387- if ($ name === 'HGETALL ' || ($ name === 'CONFIG ' && is_array ($ data ))) {
388- return Redis::parseData ($ data );
389- }
390- return $ data ;
391- } catch (SocketException $ e ) {
392- App::error ((string )$ e , 'redis ' );
393- // backup retries, fail on commands that fail inside here
394- $ retries = $ this ->retries ;
395- $ this ->retries = 0 ;
396- $ this ->close (false );
397- App::warning (sprintf ('Redis connection retry host=%s port=%d,after %.3f ' , $ this ->hostname , $ this ->port , $ this ->retryDelay ));
398- System::sleep ($ this ->retryDelay );
399- $ this ->open ();
400- $ this ->retries = $ retries ;
380+ $ this ->open ();
381+ $ retrys = $ this ->retries > 0 ? $ this ->retries : 1 ;
382+ while ($ retrys -- >= 0 ) {
383+ try {
384+ $ data = $ this ->sendCommandInternal ($ command , $ params , $ type );
385+ if ($ name === 'HGETALL ' || ($ name === 'CONFIG ' && is_array ($ data ))) {
386+ return Redis::parseData ($ data );
387+ }
388+ return $ data ;
389+ } catch (SocketException $ e ) {
390+ if ($ retrys === 0 ) {
391+ throw $ e ;
401392 }
393+ App::error ((string )$ e , 'redis ' );
394+ $ this ->close (false );
395+ App::warning (sprintf ('Redis connection retry host=%s port=%d,after %.3f ' , $ this ->hostname , $ this ->port , $ this ->retryDelay ));
396+ System::sleep ($ this ->retryDelay );
397+ $ this ->$ type = null ;
398+ $ this ->open ();
402399 }
403400 }
404- $ data = $ this ->sendCommandInternal ($ command , $ params , $ type );
405- if ($ name === 'HGETALL ' || ($ name === 'CONFIG ' && is_array ($ data ))) {
406- return Redis::parseData ($ data );
407- }
408- return $ data ;
409401 }
410402
411403 /**
@@ -432,7 +424,7 @@ private function parseParams(array $params, array &$tmp): void
432424 */
433425 public function open ()
434426 {
435- if ($ this ->_socket !== false ) {
427+ if ($ this ->_socket !== null ) {
436428 return ;
437429 }
438430 $ pool = PoolManager::getPool ($ this ->poolKey );
@@ -441,7 +433,7 @@ public function open()
441433 $ config = $ this ->parseUri ($ address );
442434 $ this ->separate = ArrayHelper::remove ($ config , 'separate ' , false );
443435 $ this ->makeConn ($ config , '_socket ' );
444- if ($ this ->separate && $ this ->_socketSlave === false ) {
436+ if ($ this ->separate && $ this ->_socketSlave === null ) {
445437 $ this ->makeConn ($ config , '_socketSlave ' );
446438 }
447439 }
@@ -481,6 +473,7 @@ private function makeConn(array $config, string $type): void
481473 $ this ->executeCommand ('SELECT ' , [$ this ->database ]);
482474 }
483475 } else {
476+ $ this ->$ type = null ;
484477 App::error ("Failed to open redis DB connection ( $ connection): $ errorNumber - $ errorDescription " , 'redis ' );
485478 $ message = getDI ('debug ' ) ? "Failed to open redis DB connection ( $ connection): $ errorNumber - $ errorDescription " : 'Failed to open DB connection. ' ;
486479 throw new Exception ($ message , $ errorDescription , $ errorNumber );
0 commit comments