Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add Max reconnect attempts limit

  • Loading branch information...
commit 5fd5891bd40515290f6d2c156ef1b5861111da50 1 parent 2ee5d56
Nikita nike-17 authored
Showing with 7 additions and 4 deletions.
  1. +7 −4 library/Rediska/Connection.php
11 library/Rediska/Connection.php
View
@@ -37,6 +37,7 @@ class Rediska_Connection extends Rediska_Options
* timeout - Connection timeout for Redis server. Optional
* readTimeout - Read timeout for Redis server
* blockingMode - Blocking/non-blocking mode for reads
+ * maxReconnectAttempts - Reconnect attempts count
*
* @var array
*/
@@ -52,7 +53,7 @@ class Rediska_Connection extends Rediska_Options
'readTimeout' => null,
'blockingMode' => true,
'streamContext' => null,
- 'reconnect' => true,
+ 'maxReconnectAttempts' => 10,
);
/**
@@ -175,16 +176,18 @@ public function write($string)
$needToWrite = (string)$string . Rediska::EOL;
$this->connect();
-
+ $maxReconnectAttempts = $this->getOption('maxReconnectAttempts');
+ $attemptsCount = 0;
while ($needToWrite) {
$bytes = @fwrite($this->_socket, $needToWrite);
if ($bytes === false) {
+ $attemptsCount++;
$this->disconnect();
- if ($this->getOption('reconnect')) {
+ if ($maxReconnectAttempts && $attemptsCount < $maxReconnectAttempts) {
return $this->write($string);
} else {
- throw new Rediska_Connection_Exception("Can't write to socket.");
+ throw new Rediska_Connection_Exception("Can't write to socket. Max reconnect attempts {$maxReconnectAttempts} was reached.");
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.