Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add Max reconnect attempts limit

  • Loading branch information...
commit 5fd5891bd40515290f6d2c156ef1b5861111da50 1 parent 2ee5d56
Nikita nike-17 authored

Showing 1 changed file with 7 additions and 4 deletions. Show diff stats Hide diff stats

  1. +7 4 library/Rediska/Connection.php
11 library/Rediska/Connection.php
@@ -37,6 +37,7 @@ class Rediska_Connection extends Rediska_Options
37 37 * timeout - Connection timeout for Redis server. Optional
38 38 * readTimeout - Read timeout for Redis server
39 39 * blockingMode - Blocking/non-blocking mode for reads
  40 + * maxReconnectAttempts - Reconnect attempts count
40 41 *
41 42 * @var array
42 43 */
@@ -52,7 +53,7 @@ class Rediska_Connection extends Rediska_Options
52 53 'readTimeout' => null,
53 54 'blockingMode' => true,
54 55 'streamContext' => null,
55   - 'reconnect' => true,
  56 + 'maxReconnectAttempts' => 10,
56 57 );
57 58
58 59 /**
@@ -175,16 +176,18 @@ public function write($string)
175 176 $needToWrite = (string)$string . Rediska::EOL;
176 177
177 178 $this->connect();
178   -
  179 + $maxReconnectAttempts = $this->getOption('maxReconnectAttempts');
  180 + $attemptsCount = 0;
179 181 while ($needToWrite) {
180 182 $bytes = @fwrite($this->_socket, $needToWrite);
181 183
182 184 if ($bytes === false) {
  185 + $attemptsCount++;
183 186 $this->disconnect();
184   - if ($this->getOption('reconnect')) {
  187 + if ($maxReconnectAttempts && $attemptsCount < $maxReconnectAttempts) {
185 188 return $this->write($string);
186 189 } else {
187   - throw new Rediska_Connection_Exception("Can't write to socket.");
  190 + throw new Rediska_Connection_Exception("Can't write to socket. Max reconnect attempts {$maxReconnectAttempts} was reached.");
188 191 }
189 192 }
190 193

0 comments on commit 5fd5891

Please sign in to comment.
Something went wrong with that request. Please try again.