Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

Fix for lost connection infinite loop
  • Loading branch information...
commit 5fdc3609e9deba4bad3abf1a17e6510514b5d0f6 2 parents 2227842 + fdc9f88
@chrisboulton authored
Showing with 18 additions and 3 deletions.
  1. +18 −3 lib/Redisent/Redisent.php
View
21 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,18 @@ function __call($name, $args) {
$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);
+ sleep(1);
+ $this->establishConnection();
+ $reconnects++;
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.