Merge pull request #77 from atorres757/fix-lost-connection

Fix for lost connection infinite loop
This commit is contained in:
Chris Boulton 2012-12-28 19:12:02 -08:00
commit 5fdc3609e9

View File

@ -44,6 +44,13 @@ class Redisent {
*/ */
public $port; 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}. * Creates a Redisent connection to the Redis server on host {@link $host} and port {@link $port}.
* @param string $host The hostname of the Redis server * @param string $host The hostname of the Redis server
@ -73,10 +80,18 @@ class Redisent {
$command = sprintf('*%d%s%s%s', count($args), CRLF, implode(array_map(array($this, 'formatArgument'), $args), CRLF), CRLF); $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 */ /* Open a Redis connection and execute the command */
$reconnects = 0;
for ($written = 0; $written < strlen($command); $written += $fwrite) { for ($written = 0; $written < strlen($command); $written += $fwrite) {
$fwrite = fwrite($this->__sock, substr($command, $written)); $fwrite = @fwrite($this->__sock, substr($command, $written));
if ($fwrite === FALSE) { if ($fwrite === FALSE || $fwrite === 0) {
if ($reconnects >= (int)$this->max_reconnects) {
throw new Exception('Failed to write entire command to stream'); throw new Exception('Failed to write entire command to stream');
}else{
fclose($this->__sock);
sleep(1);
$this->establishConnection();
$reconnects++;
}
} }
} }