Skip to content

Commit 5fdc360

Browse files
committed
Merge pull request #77 from atorres757/fix-lost-connection
Fix for lost connection infinite loop
2 parents 2227842 + fdc9f88 commit 5fdc360

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

lib/Redisent/Redisent.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class Redisent {
4343
* @access public
4444
*/
4545
public $port;
46+
47+
/**
48+
* Number of times to attempt a reconnect
49+
*
50+
* @var int
51+
*/
52+
public $max_reconnects = 3;
4653

4754
/**
4855
* Creates a Redisent connection to the Redis server on host {@link $host} and port {@link $port}.
@@ -73,10 +80,18 @@ function __call($name, $args) {
7380
$command = sprintf('*%d%s%s%s', count($args), CRLF, implode(array_map(array($this, 'formatArgument'), $args), CRLF), CRLF);
7481

7582
/* Open a Redis connection and execute the command */
83+
$reconnects = 0;
7684
for ($written = 0; $written < strlen($command); $written += $fwrite) {
77-
$fwrite = fwrite($this->__sock, substr($command, $written));
78-
if ($fwrite === FALSE) {
79-
throw new Exception('Failed to write entire command to stream');
85+
$fwrite = @fwrite($this->__sock, substr($command, $written));
86+
if ($fwrite === FALSE || $fwrite === 0) {
87+
if ($reconnects >= (int)$this->max_reconnects) {
88+
throw new Exception('Failed to write entire command to stream');
89+
}else{
90+
fclose($this->__sock);
91+
sleep(1);
92+
$this->establishConnection();
93+
$reconnects++;
94+
}
8095
}
8196
}
8297

0 commit comments

Comments
 (0)