From e4f39a60931e68e83d963c44d8c42615ec70f188 Mon Sep 17 00:00:00 2001 From: Allen Torres Date: Fri, 28 Dec 2012 09:33:09 -0500 Subject: [PATCH] Updated Redisent.php to attempt three reconnects to the redis server before giving up. Also supressed fwrite errors. --- lib/Redisent/Redisent.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/Redisent/Redisent.php b/lib/Redisent/Redisent.php index 4b480f6..727f16b 100644 --- a/lib/Redisent/Redisent.php +++ b/lib/Redisent/Redisent.php @@ -43,6 +43,13 @@ class Redisent { * @access public */ public $port; + + /** + * Number of times to attempt a reconnect + * + * @var int + */ + public $max_reconnects = 3; /** * Creates a Redisent connection to the Redis server on host {@link $host} and port {@link $port}. @@ -73,10 +80,17 @@ class Redisent { $command = sprintf('*%d%s%s%s', count($args), CRLF, implode(array_map(array($this, 'formatArgument'), $args), CRLF), CRLF); /* Open a Redis connection and execute the command */ + $reconnects = 0; for ($written = 0; $written < strlen($command); $written += $fwrite) { - $fwrite = fwrite($this->__sock, substr($command, $written)); - if ($fwrite === FALSE) { - throw new Exception('Failed to write entire command to stream'); + $fwrite = @fwrite($this->__sock, substr($command, $written)); + if ($fwrite === FALSE || $fwrite === 0) { + if ($reconnects >= (int)$this->max_reconnects) { + throw new Exception('Failed to write entire command to stream'); + }else{ + fclose($this->__sock); + $this->establishConnection(); + $reconnects++; + } } }