- grunt watch will now abort mid task
- connection's ping method now accepts requestTimeout, path, and method params like all the grown-up API calls - ConnectionPool now managed connection timeouts. When a connection dies a timeout object is created to track when the timeout is scheduled and the function to call when it does. It also tracks how many times it has run to allow the timeout to grow - Timeouts now grow with use of `config.calcDeadTimeout` which is set to 'exponential' by default, but can also be set to flat in order to always use the standard deadTimeout. Exponential growth of the deadTimeout is stopped at config.maxDeadTimeout which is set to 30 minutes by default. - Connections no longer have a resuscitate method (too hard to spell). Now the method is created dynamically as a part of the timeout object as it just calls the connection's ping method and needed to access variables like revive attempts. - Timeouts were moved to the transport layer, meaning that you need to capture the abort method and abort the request yourself if you are handling connections directly, ConnectionsAbstract's ping method does this.
This commit is contained in:
@ -36,7 +36,9 @@ module.exports = {
|
||||
}
|
||||
} else if (externalExists === void 0) {
|
||||
doCreateClient(function () {
|
||||
client.ping(function (err) {
|
||||
client.ping({
|
||||
requestTimeout: 1000
|
||||
}, function (err) {
|
||||
if (err instanceof es.errors.ConnectionFault) {
|
||||
externalExists = false;
|
||||
create(done);
|
||||
|
||||
@ -11,6 +11,12 @@ describe('Client instances creation', function () {
|
||||
client = new es.Client();
|
||||
});
|
||||
|
||||
it('throws an error linking to the es module when you try to instanciate the exports', function () {
|
||||
(function () {
|
||||
var client = new es();
|
||||
}).should.throw(/previous "elasticsearch" module/);
|
||||
});
|
||||
|
||||
it('inherits the api', function () {
|
||||
client.bulk.should.eql(api.bulk);
|
||||
client.cluster.nodeStats.should.eql(api.cluster.prototype.nodeStats);
|
||||
|
||||
@ -8,12 +8,9 @@ var stub = require('./auto_release_stub').make();
|
||||
describe('Connection Abstract', function () {
|
||||
var host = new Host('localhost:9200');
|
||||
|
||||
it('constructs with defaults for deadTimeout, requestCount, host, and bound', function () {
|
||||
it('constructs with defaults for host, and bound', function () {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
conn.deadTimeout.should.eql(30000);
|
||||
conn.requestCount.should.eql(0);
|
||||
conn.host.should.be.exactly(host);
|
||||
conn.bound.should.have.properties('resuscitate');
|
||||
});
|
||||
|
||||
it('requires a valid host', function () {
|
||||
@ -34,21 +31,50 @@ describe('Connection Abstract', function () {
|
||||
});
|
||||
|
||||
describe('#ping', function () {
|
||||
it('requires a callback', function () {
|
||||
(function () {
|
||||
(new ConnectionAbstract(host)).ping();
|
||||
}).should.throw(TypeError);
|
||||
it('accpets just a callback', function () {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
stub(conn, 'request');
|
||||
var cb = function () {};
|
||||
conn.ping(cb);
|
||||
conn.request.callCount.should.eql(1);
|
||||
conn.request.lastCall.args[0].should.have.type('object');
|
||||
conn.request.lastCall.args[1].should.have.type('function');
|
||||
});
|
||||
|
||||
it('accpets just params', function () {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
stub(conn, 'request');
|
||||
conn.ping({});
|
||||
conn.request.callCount.should.eql(1);
|
||||
conn.request.lastCall.args[0].should.have.type('object');
|
||||
conn.request.lastCall.args[1].should.have.type('function');
|
||||
});
|
||||
|
||||
it('allows overriding the requestTimeout, method, and path', function () {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
stub(conn, 'request');
|
||||
var params = {
|
||||
method: 'HEAD',
|
||||
path: '/',
|
||||
requestTimeout: 10000
|
||||
};
|
||||
conn.ping(params);
|
||||
conn.request.callCount.should.eql(1);
|
||||
conn.request.lastCall.args[0].should.include(params);
|
||||
conn.request.lastCall.args[1].should.have.type('function');
|
||||
});
|
||||
|
||||
it('calls it\'s own request method', function () {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
var football = {};
|
||||
conn.request = function () {
|
||||
return football;
|
||||
};
|
||||
|
||||
conn.ping(function () {}).should.be.exactly(football);
|
||||
stub(conn, 'request');
|
||||
conn.ping();
|
||||
conn.request.callCount.should.eql(1);
|
||||
});
|
||||
|
||||
it('sets a timer for the request');
|
||||
it('aborts the request if it takes too long');
|
||||
it('ignores the response from the request if it already aborted');
|
||||
});
|
||||
|
||||
describe('#setStatus', function () {
|
||||
@ -75,79 +101,79 @@ describe('Connection Abstract', function () {
|
||||
conn.status.should.eql('closed');
|
||||
});
|
||||
|
||||
it('sets a timeout when set to dead, and removed when alive', function () {
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
stub.autoRelease(clock);
|
||||
var conn = new ConnectionAbstract(host);
|
||||
// it('sets a timeout when set to dead, and removed when alive', function () {
|
||||
// var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
// stub.autoRelease(clock);
|
||||
// var conn = new ConnectionAbstract(host);
|
||||
|
||||
var start = _.size(clock.timeouts);
|
||||
conn.setStatus('dead');
|
||||
_.size(clock.timeouts).should.be.eql(start + 1);
|
||||
// var start = _.size(clock.timeouts);
|
||||
// conn.setStatus('dead');
|
||||
// _.size(clock.timeouts).should.be.eql(start + 1);
|
||||
|
||||
conn.setStatus('alive');
|
||||
_.size(clock.timeouts).should.eql(start);
|
||||
clock.restore();
|
||||
});
|
||||
// conn.setStatus('alive');
|
||||
// _.size(clock.timeouts).should.eql(start);
|
||||
// clock.restore();
|
||||
// });
|
||||
|
||||
});
|
||||
|
||||
describe('#resuscitate', function () {
|
||||
it('should not ping the connection unless it is still dead', function () {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
// describe('#resuscitate', function () {
|
||||
// it('should not ping the connection unless it is still dead', function () {
|
||||
// var conn = new ConnectionAbstract(host);
|
||||
|
||||
conn.setStatus('alive');
|
||||
stub(conn, 'ping', function () {
|
||||
throw new Error('ping should not have been called');
|
||||
});
|
||||
// conn.setStatus('alive');
|
||||
// stub(conn, 'ping', function () {
|
||||
// throw new Error('ping should not have been called');
|
||||
// });
|
||||
|
||||
conn.resuscitate();
|
||||
});
|
||||
// conn.resuscitate();
|
||||
// });
|
||||
|
||||
it('should ping the connection after the deadTimeout, and set the status to "alive" on pong', function (done) {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
var clock;
|
||||
stub.autoRelease(clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'));
|
||||
// it('should ping the connection after the deadTimeout, and set the status to "alive" on pong', function (done) {
|
||||
// var conn = new ConnectionAbstract(host);
|
||||
// var clock;
|
||||
// stub.autoRelease(clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'));
|
||||
|
||||
// schedules the resuscitate
|
||||
conn.setStatus('dead');
|
||||
// // schedules the resuscitate
|
||||
// conn.setStatus('dead');
|
||||
|
||||
// override the ping method to just callback without an error
|
||||
stub(conn, 'ping', function (cb) {
|
||||
cb();
|
||||
});
|
||||
// // override the ping method to just callback without an error
|
||||
// stub(conn, 'ping', function (cb) {
|
||||
// cb();
|
||||
// });
|
||||
|
||||
// will be called after the ping calls back
|
||||
stub(conn, 'setStatus', function (status) {
|
||||
status.should.eql('alive');
|
||||
done();
|
||||
});
|
||||
// // will be called after the ping calls back
|
||||
// stub(conn, 'setStatus', function (status) {
|
||||
// status.should.eql('alive');
|
||||
// done();
|
||||
// });
|
||||
|
||||
// fast forward the clock
|
||||
clock.tick(conn.deadTimeout);
|
||||
});
|
||||
// // fast forward the clock
|
||||
// clock.tick(conn.deadTimeout);
|
||||
// });
|
||||
|
||||
it('should ping the connection after the deadTimeout, and set the status to "dead" on error', function (done) {
|
||||
var conn = new ConnectionAbstract(host);
|
||||
var clock;
|
||||
stub.autoRelease(clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'));
|
||||
// it('should ping the connection after the deadTimeout, and set the status to "dead" on error', function (done) {
|
||||
// var conn = new ConnectionAbstract(host);
|
||||
// var clock;
|
||||
// stub.autoRelease(clock = sinon.useFakeTimers('setTimeout', 'clearTimeout'));
|
||||
|
||||
// schedules the resuscitate
|
||||
conn.setStatus('dead');
|
||||
// // schedules the resuscitate
|
||||
// conn.setStatus('dead');
|
||||
|
||||
// override the ping method to just callback without an error
|
||||
stub(conn, 'ping', function (cb) {
|
||||
cb(new Error('server still down'));
|
||||
});
|
||||
// // override the ping method to just callback without an error
|
||||
// stub(conn, 'ping', function (cb) {
|
||||
// cb(new Error('server still down'));
|
||||
// });
|
||||
|
||||
// will be called after the ping calls back
|
||||
stub(conn, 'setStatus', function (status) {
|
||||
status.should.eql('dead');
|
||||
done();
|
||||
});
|
||||
// // will be called after the ping calls back
|
||||
// stub(conn, 'setStatus', function (status) {
|
||||
// status.should.eql('dead');
|
||||
// done();
|
||||
// });
|
||||
|
||||
// fast forward the clock
|
||||
clock.tick(conn.deadTimeout);
|
||||
});
|
||||
});
|
||||
// // fast forward the clock
|
||||
// clock.tick(conn.deadTimeout);
|
||||
// });
|
||||
// });
|
||||
|
||||
});
|
||||
|
||||
@ -4,6 +4,7 @@ var ConnectionAbstract = require('../../src/lib/connection');
|
||||
var _ = require('lodash');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var should = require('should');
|
||||
var sinon = require('sinon');
|
||||
|
||||
function listenerCount(emitter, event) {
|
||||
if (EventEmitter.listenerCount) {
|
||||
@ -156,13 +157,14 @@ describe('Connection Pool', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('should automatically select the first dead connection when there no living connections', function (done) {
|
||||
it('should automatically select the dead connection with the shortest timeout when there no living connections',
|
||||
function (done) {
|
||||
pool.setHosts([]);
|
||||
pool._conns.alive = [];
|
||||
pool._conns.dead = [1, 2, 3];
|
||||
|
||||
pool.select(function (err, selection) {
|
||||
selection.should.be.exactly(1);
|
||||
// selection.should.be.exactly(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@ -182,7 +184,7 @@ describe('Connection Pool', function () {
|
||||
host2
|
||||
]);
|
||||
|
||||
connection = pool.index[host2.toString()];
|
||||
connection = pool.index[host.toString()];
|
||||
connection2 = pool.index[host2.toString()];
|
||||
|
||||
pool._conns.alive.should.have.length(2);
|
||||
@ -200,16 +202,18 @@ describe('Connection Pool', function () {
|
||||
pool._conns.dead.should.have.length(1);
|
||||
});
|
||||
|
||||
it('moves a dead connection to the end of the dead list when it re-dies', function () {
|
||||
connection.setStatus('dead');
|
||||
connection2.setStatus('dead');
|
||||
it('clears and resets the timeout when a connection redies', function () {
|
||||
var clock = sinon.useFakeTimers('setTimeout', 'clearTimeout');
|
||||
|
||||
connection.setStatus('dead');
|
||||
_.size(clock.timeouts).should.eql(1);
|
||||
var id = _(clock.timeouts).keys().first();
|
||||
|
||||
// connection is at the front of the line
|
||||
pool._conns.dead[0].should.be.exactly(connection);
|
||||
// it re-dies
|
||||
connection.setStatus('dead');
|
||||
// connection2 is now at the front of the list
|
||||
pool._conns.dead[0].should.be.exactly(connection2);
|
||||
_.size(clock.timeouts).should.eql(1);
|
||||
_(clock.timeouts).keys().first().should.not.eql(id);
|
||||
clock.restore();
|
||||
});
|
||||
|
||||
it('does nothing when a connection is re-alive', function () {
|
||||
@ -239,4 +243,65 @@ describe('Connection Pool', function () {
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#getConnections', function () {
|
||||
it('will return all values from the alive list by default', function () {
|
||||
var pool = new ConnectionPool({});
|
||||
pool._conns.alive = new Array(1000);
|
||||
var length = pool._conns.alive.length;
|
||||
while (length--) {
|
||||
pool._conns.alive[length] = length;
|
||||
}
|
||||
|
||||
var result = pool.getConnections();
|
||||
result.should.have.length(1000);
|
||||
_.reduce(result, function (sum, num) {
|
||||
return sum += num;
|
||||
}, 0).should.eql(499500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#calcDeadTimeout', function () {
|
||||
it('should be configurable via config.calcDeadTimeout', function () {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'flat'
|
||||
});
|
||||
pool.calcDeadTimeout.should.be.exactly(ConnectionPool.calcDeadTimeoutOptions.flat);
|
||||
pool.close();
|
||||
});
|
||||
it('"flat" always returns the base timeout', function () {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'flat'
|
||||
});
|
||||
pool.calcDeadTimeout(0, 1000).should.eql(1000);
|
||||
pool.calcDeadTimeout(10, 5000).should.eql(5000);
|
||||
pool.calcDeadTimeout(25, 10000).should.eql(10000);
|
||||
});
|
||||
it('"exponential" always increases the timeout based on the attempts', function () {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'exponential'
|
||||
});
|
||||
pool.calcDeadTimeout(0, 1000).should.eql(1000);
|
||||
pool.calcDeadTimeout(10, 5000).should.be.above(5000);
|
||||
pool.calcDeadTimeout(25, 10000).should.be.above(10000);
|
||||
});
|
||||
it('"exponential" produces predicatable results', function () {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'exponential'
|
||||
});
|
||||
pool.calcDeadTimeout(0, 1000).should.eql(1000);
|
||||
pool.calcDeadTimeout(4, 10000).should.eql(40000);
|
||||
// maxes out at 30 minutes by default
|
||||
pool.calcDeadTimeout(25, 30000).should.eql(18e5);
|
||||
});
|
||||
it('"exponential" repects config.maxDeadtimeout', function () {
|
||||
var pool = new ConnectionPool({
|
||||
calcDeadTimeout: 'exponential',
|
||||
maxDeadTimeout: 10000
|
||||
});
|
||||
pool.calcDeadTimeout(0, 1000).should.eql(1000);
|
||||
pool.calcDeadTimeout(10, 1000).should.eql(10000);
|
||||
pool.calcDeadTimeout(100, 1000).should.eql(10000);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -21,6 +21,10 @@ function shortCircuitRequest(tran, delay) {
|
||||
});
|
||||
}
|
||||
|
||||
function getConnection(transport, status) {
|
||||
return transport.connectionPool.getConnections(status || 'alive', 1).pop();
|
||||
}
|
||||
|
||||
describe('Transport Class', function () {
|
||||
|
||||
describe('Constructor', function () {
|
||||
@ -333,7 +337,7 @@ describe('Transport Class', function () {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
var conn = trans.connectionPool.getConnection();
|
||||
var conn = getConnection(trans);
|
||||
var body = {
|
||||
_id: 'simple body',
|
||||
name: 'ഢധയമബ'
|
||||
@ -352,7 +356,7 @@ describe('Transport Class', function () {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
var conn = trans.connectionPool.getConnection();
|
||||
var conn = getConnection(trans);
|
||||
var body = [
|
||||
{ _id: 'simple body'},
|
||||
{ name: 'ഢധയമബ' }
|
||||
@ -378,7 +382,7 @@ describe('Transport Class', function () {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
var conn = trans.connectionPool.getConnection();
|
||||
var conn = getConnection(trans);
|
||||
var body = {
|
||||
_id: 'circular body'
|
||||
};
|
||||
@ -434,7 +438,7 @@ describe('Transport Class', function () {
|
||||
var trans = new Transport({
|
||||
hosts: 'localhost'
|
||||
});
|
||||
var conn = trans.connectionPool.getConnection();
|
||||
var conn = getConnection(trans);
|
||||
|
||||
stub(conn, 'request', function () {
|
||||
done();
|
||||
@ -459,7 +463,7 @@ describe('Transport Class', function () {
|
||||
}
|
||||
|
||||
var trans = new Transport({
|
||||
hosts: _.map(new Array(retries + 1), function (i) {
|
||||
hosts: _.map(new Array(retries + 1), function (val, i) {
|
||||
return 'localhost/' + i;
|
||||
}),
|
||||
maxRetries: retries,
|
||||
@ -759,7 +763,7 @@ describe('Transport Class', function () {
|
||||
host: 'localhost'
|
||||
});
|
||||
|
||||
var con = tran.connectionPool.getConnection();
|
||||
var con = getConnection(tran);
|
||||
stub(con, 'request', function () {
|
||||
throw new Error('Request should not have been called.');
|
||||
});
|
||||
@ -772,7 +776,7 @@ describe('Transport Class', function () {
|
||||
host: 'localhost'
|
||||
});
|
||||
|
||||
var con = tran.connectionPool.getConnection();
|
||||
var con = getConnection(tran);
|
||||
stub(con, 'request', function () {
|
||||
process.nextTick(function () {
|
||||
ret.abort();
|
||||
@ -789,7 +793,7 @@ describe('Transport Class', function () {
|
||||
host: 'localhost'
|
||||
});
|
||||
|
||||
var con = tran.connectionPool.getConnection();
|
||||
var con = getConnection(tran);
|
||||
stub(con, 'request', function (params, cb) {
|
||||
cb();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user