Skip to content
This repository was archived by the owner on Dec 27, 2022. It is now read-only.

changes to request and get_node #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ module.exports = function (inherits, EventEmitter, Pinger, EndpointError) {

this.setPending()
this.requests[req.id] = req
return req
}

Endpoint.prototype.setHealthy = function (newState) {
Expand Down
22 changes: 9 additions & 13 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ module.exports = function (inherits, EventEmitter, Endpoint, RequestSet) {
secure ? https : http,
[host + ':' + port]
)
p.request(options, data, cb)
return p.request(options, data, cb)
}

// Bound handlers
Expand Down Expand Up @@ -144,7 +144,7 @@ module.exports = function (inherits, EventEmitter, Endpoint, RequestSet) {
options.stream = (options.stream === undefined) ? callback.length === 2 : options.stream

var started = Date.now()
RequestSet.request(this, options, function (err, res, body) {
return RequestSet.request(this, options, function (err, res, body) {
options.success = !err
options.reused = res && res.socket && (res.socket._requestCount || 1) > 1
self.emit('timing', Date.now() - started, options)
Expand Down Expand Up @@ -185,31 +185,27 @@ module.exports = function (inherits, EventEmitter, Endpoint, RequestSet) {

Pool.prototype.get_node = function () {
var len = this.nodes.length
var h = []
var sum = 0
var totalPending = 0
var r = Math.floor(Math.random() * len)
var lowestPending = this.maxPending
var lowestPedingNode = this.nodes[(r + 1) % len];
for (var i = 0; i < len; i++) {
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
if (node.healthy && node.pending < lowestPending) {
lowestPending = node.pending
lowestPedingNode = node
}
totalPending += node.pending
}
if (totalPending >= this.maxPending) {
return Endpoint.overloaded()
}
var avg = sum / h.length
while (h.length) {
var node = h.pop()
if (node.pending <= avg) {
return node
}
if (lowestPending < this.maxPending) {
return lowestPedingNode
}
return Endpoint.unhealthy()
}
Expand Down
4 changes: 2 additions & 2 deletions lib/request_set.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,12 @@ function handleResponse(err, response, body) {
// callback: function (err, response, body) {}
RequestSet.request = function (pool, options, callback) {
var set = new RequestSet(pool, options, callback)
set.doRequest()
return set.doRequest()
}

RequestSet.prototype.doRequest = function () {
var node = this.pool.get_node()
node.request(this.options, handleResponse.bind(this))
return node.request(this.options, handleResponse.bind(this))
}

module.exports = RequestSet
Expand Down