Skip to content
This repository
Browse code

Support new option 'max_attempts' to specify a total number of connec…

…tion retries
  • Loading branch information...
commit 54cf583dc31c3a064528f1e78de7b5a4208b461a 1 parent 025c2e9
Owen Smith authored March 13, 2011 mranney committed November 10, 2011

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

  1. 22  index.js
22  index.js
@@ -35,6 +35,10 @@ function RedisClient(stream, options) {
35 35
     this.should_buffer = false;
36 36
     this.command_queue_high_water = this.options.command_queue_high_water || 1000;
37 37
     this.command_queue_low_water = this.options.command_queue_low_water || 0;
  38
+    this.max_attempts = null;
  39
+    if (options.max_attempts && !isNaN(options.max_attempts) && options.max_attempts > 0) {
  40
+        this.max_attempts = +options.max_attempts;
  41
+    }
38 42
     this.command_queue = new Queue(); // holds sent commands to de-pipeline them
39 43
     this.offline_queue = new Queue(); // holds commands issued but not able to be sent
40 44
     this.commands_sent = 0;
@@ -168,6 +172,7 @@ RedisClient.prototype.on_connect = function () {
168 172
     this.connections += 1;
169 173
     this.command_queue = new Queue();
170 174
     this.emitted_end = false;
  175
+    this.max_attempts = 0;
171 176
     this.retry_totaltime = 0;
172 177
     this.retry_timer = null;
173 178
     this.current_retry_delay = this.retry_time;
@@ -347,10 +352,19 @@ RedisClient.prototype.connection_gone = function (why) {
347 352
     if (exports.debug_mode) {
348 353
         console.log("Retry connection in " + this.current_retry_delay + " ms");
349 354
     }
350  
-    this.attempts += 1;
351  
-    this.emit("reconnecting", {
352  
-        delay: this.current_retry_delay,
353  
-        attempt: this.attempts
  355
+    
  356
+    if (self.max_attempts && self.attempts >= self.max_attempts) {
  357
+        self.retry_timer = null;
  358
+        if (exports.debug_mode) {
  359
+            console.log("Aborting connection attempt: Max attempts " + self.max_attempts + " failed.");
  360
+        }
  361
+        return;
  362
+    }
  363
+    
  364
+    self.attempts += 1;
  365
+    self.emit("reconnecting", {
  366
+        delay: self.retry_delay,
  367
+        attempt: self.attempts
354 368
     });
355 369
     this.retry_timer = setTimeout(function () {
356 370
         if (exports.debug_mode) {

0 notes on commit 54cf583

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