Commit a1ce8b6a authored by Ian Ward's avatar Ian Ward

Schedule failed server to reconnect only once

Fixes 3rd-Eden/node-memcached#122
Fixes 3rd-Eden/node-memcached#123
parent 6cc14167
......@@ -26,6 +26,7 @@ function IssueLog (args) {
this.messages = [];
this.failed = false;
this.locked = false;
this.isScheduledToReconnect = false;
this.totalFailures = 0;
this.retry = 0;
......@@ -51,7 +52,10 @@ issues.log = function log (message) {
if (this.remove) return this.emit('remove', this.details);
setTimeout(issue.attemptReconnect.bind(issue), this.reconnect);
if (!this.isScheduledToReconnect) {
this.isScheduledToReconnect = true;
setTimeout(issue.attemptReconnect.bind(issue), this.reconnect);
}
};
Object.defineProperty(issues, 'details', {
......@@ -101,6 +105,7 @@ issues.attemptReconnect = function attemptReconnect () {
issue.totalReconnectsSuccess++;
issue.messages.length = 0;
issue.failed = false;
issue.isScheduledToReconnect = false;
// we connected again, so we are going through the whole cycle again
Utils.merge(issue, JSON.parse(JSON.stringify(issue.config)));
......
......@@ -110,4 +110,33 @@ describe('Memcached connections', function () {
});
});
});
it('should properly schedule server reconnection attempts', function(done) {
var server = '127.0.0.1:1234'
, memcached = new Memcached(server, {
retries: 3,
minTimeout: 0,
maxTimeout: 100,
failures: 0,
reconnect: 100 })
, reconnectAttempts = 0;
memcached.on('reconnecting', function() {
reconnectAttempts++;
});
// First request will mark server dead and schedule reconnect
memcached.get('idontcare', function (err) {
assert.throws(function() { throw err }, /connect ECONNREFUSED/);
// Second request should not schedule another reconnect
memcached.get('idontcare', function (err) {
assert.throws(function() { throw err }, /Server not available/);
// Allow enough time to pass for a connection retries to occur
setTimeout(function() {
assert.deepEqual(reconnectAttempts, 1);
memcached.end();
done();
}, 400);
});
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment