Comparing version 0.4.7 to 0.4.8
@@ -33,2 +33,3 @@ var Stream = require('stream') | ||
this.address = this.ip | ||
this.keepAlive = options.keepAlive | ||
@@ -42,3 +43,3 @@ this.pinger = new Pinger(this.ping.bind(this)) | ||
this.pingTimeout = options.pingTimeout || 2000 | ||
if (options.keepAlive) { | ||
if (this.keepAlive) { | ||
if (protocol === http) { | ||
@@ -77,2 +78,14 @@ this.agent = new KeepAlive(options.agentOptions) | ||
Endpoint.prototype.connected = function () { | ||
return this.agent.sockets[this.name] && this.agent.sockets[this.name].length | ||
} | ||
Endpoint.prototype.ready = function () { | ||
return this.healthy | ||
&& (this.keepAlive ? | ||
this.connected() > this.pending : | ||
this.pending === 0 | ||
) | ||
} | ||
Endpoint.prototype.stats = function () { | ||
@@ -79,0 +92,0 @@ var socketNames = Object.keys(this.agent.sockets) |
@@ -82,2 +82,3 @@ module.exports = function (inherits, EventEmitter, Endpoint, RequestSet) { | ||
} | ||
Pool.prototype.healthyNodes = Pool.prototype.healthy_nodes | ||
@@ -175,11 +176,16 @@ Pool.prototype.onRetry = function (err) { | ||
var len = this.nodes.length | ||
var h = 0 | ||
var h = [] | ||
var sum = 0 | ||
var totalPending = 0 | ||
var r = Math.floor(Math.random() * len) | ||
for (var i = 0; i < len; i++) { | ||
var node = this.nodes[i] | ||
if (node.healthy) { | ||
h++ | ||
sum += node.busyness() | ||
r = (r + 1) % len | ||
var node = this.nodes[r] | ||
if (node.ready()) { | ||
return node //fast path | ||
} | ||
else if (node.healthy) { | ||
h.push(node) | ||
sum += node.pending | ||
} | ||
totalPending += node.pending | ||
@@ -190,11 +196,7 @@ } | ||
} | ||
if (h !== 0) { | ||
var avg = sum / h | ||
var r = Math.floor(Math.random() * len) | ||
for (i = 0; i < len; i++) { | ||
r = (r + 1) % len | ||
node = this.nodes[r] | ||
if (node.healthy && avg >= node.busyness()) { | ||
return node | ||
} | ||
var avg = sum / h.length | ||
while (h.length) { | ||
var node = h.pop() | ||
if (node.pending <= avg) { | ||
return node | ||
} | ||
@@ -205,2 +207,4 @@ } | ||
Pool.prototype.getNode = Pool.prototype.get_node //must keep the old _ api | ||
Pool.prototype.pending = function () { | ||
@@ -207,0 +211,0 @@ return this.nodes.reduce(function (a, b) { return a + b.pending }, 0) |
{ | ||
"name": "poolee", | ||
"version": "0.4.7", | ||
"version": "0.4.8", | ||
"description": "HTTP pool and load balancer", | ||
@@ -5,0 +5,0 @@ "homepage": "http://github.com/dannycoates/poolee", |
@@ -445,2 +445,44 @@ var assert = require("assert") | ||
// | ||
// ready | ||
// | ||
////////////////////////////////////////////////////////////////////////////// | ||
describe("ready()", function () { | ||
it('returns true when it is healthy and connected > pending with keepAlive on', | ||
function () { | ||
var e = new Endpoint(http, '127.0.0.1', 6969, {keepAlive: true}) | ||
e.pending = 1 | ||
e.agent.sockets[e.name] = [1,2] | ||
assert(e.ready()) | ||
} | ||
) | ||
it('returns false when it is healthy and connected = pending with keepAlive on', | ||
function () { | ||
var e = new Endpoint(http, '127.0.0.1', 6969, {keepAlive: true}) | ||
e.pending = 1 | ||
e.agent.sockets[e.name] = [1] | ||
assert(!e.ready()) | ||
} | ||
) | ||
it('returns true when it is healthy and pending = 0 with keepAlive off', | ||
function () { | ||
var e = new Endpoint(http, '127.0.0.1', 6969) | ||
e.pending = 0 | ||
assert(e.ready()) | ||
} | ||
) | ||
it('returns false when it is healthy and pending > 0 with keepAlive off', | ||
function () { | ||
var e = new Endpoint(http, '127.0.0.1', 6969) | ||
e.pending = 1 | ||
assert(!e.ready()) | ||
} | ||
) | ||
}) | ||
// | ||
// setHealthy | ||
@@ -447,0 +489,0 @@ // |
@@ -14,3 +14,6 @@ var assert = require("assert") | ||
inherits(FakeEndpoint, EventEmitter) | ||
FakeEndpoint.prototype.pending = 1 | ||
FakeEndpoint.prototype.busyness = function () { return 1 } | ||
FakeEndpoint.prototype.connected = function () { return 0 } | ||
FakeEndpoint.prototype.ready = function () { return false } | ||
var overloaded = new FakeEndpoint() | ||
@@ -110,2 +113,17 @@ FakeEndpoint.overloaded = function () { return overloaded } | ||
}) | ||
it('returns a "ready" node when one is available', function () { | ||
var p = new Pool(http, ['127.0.0.1:8080', '127.0.0.1:8081', '127.0.0.1:8082']) | ||
var n = p.nodes[0] | ||
n.ready = function () { return true } | ||
assert.equal(p.get_node(), n); | ||
}) | ||
it('returns a healthy node when none are "ready"', function () { | ||
var p = new Pool(http, ['127.0.0.1:8080', '127.0.0.1:8081', '127.0.0.1:8082']) | ||
p.nodes[0].healthy = false | ||
p.nodes[1].healthy = false | ||
p.nodes[2].healthy = true | ||
assert(p.get_node().healthy); | ||
}) | ||
}) | ||
@@ -112,0 +130,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55185
1555