Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for lost connection infinite loop #77

Merged
merged 6 commits into from Dec 29, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 18 additions & 3 deletions lib/Redisent/Redisent.php
Expand Up @@ -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}.
Expand Down Expand Up @@ -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++;
}
}
}

Expand Down