eh-api-client
Advanced tools
Comparing version 0.19.0 to 0.20.0
34
index.js
var | ||
request = require("requestretry"), | ||
request = require("request"), | ||
_ = require("lodash"), | ||
@@ -12,3 +12,6 @@ Agent = require("http").Agent, | ||
retryDelay: 100, | ||
retryStrategy: request.RetryStrategies.NetworkError | ||
retryStrategy: function(err) { | ||
// only retry if got an ECONNRESET error | ||
return err.code === "ECONNRESET"; | ||
} | ||
}; | ||
@@ -91,2 +94,29 @@ this.agent = new Agent({ | ||
Factory.prototype.getPoolStats = function() { | ||
var hostsSocketsCount = {}; | ||
var host; | ||
if(typeof this.agent.freeSockets === "object") { | ||
for(host in this.agent.freeSockets) { | ||
if(!hostsSocketsCount[host]) { | ||
hostsSocketsCount[host] = 0; | ||
} | ||
hostsSocketsCount[host] += this.agent.freeSockets[host].length; | ||
} | ||
} | ||
if(typeof this.agent.sockets === "object") { | ||
for(host in this.agent.sockets) { | ||
if(!hostsSocketsCount[host]) { | ||
hostsSocketsCount[host] = 0; | ||
} | ||
hostsSocketsCount[host] += this.agent.sockets[host].length; | ||
} | ||
} | ||
var lines = []; | ||
for(host in hostsSocketsCount) { | ||
lines.push(host + ": " + hostsSocketsCount[host]); | ||
} | ||
return lines.join("\n"); | ||
}; | ||
module.exports = Factory; |
var | ||
Q = require("q"), | ||
request = require("requestretry"), | ||
request = require("request"), | ||
_ = require("lodash"); | ||
@@ -174,7 +174,31 @@ | ||
Client.prototype._tryRequest = function(params, numTry, cb) { | ||
var retryOptions = this._factory.retryOptions; | ||
var self = this; | ||
return request(params, function(err, res) { | ||
var args = [].slice.call(arguments); | ||
if(err && retryOptions) { | ||
numTry++; | ||
if(numTry >= retryOptions.maxAttempts || !retryOptions.retryStrategy(err)) { | ||
err.retryInfo = { | ||
try: numTry | ||
}; | ||
return cb(err); | ||
} | ||
// retry | ||
return setTimeout(function() { | ||
self._tryRequest(params, numTry, cb); | ||
}, retryOptions.retryDelay); | ||
} | ||
res.retryInfo = { | ||
try: numTry | ||
}; | ||
cb.apply(null, args); | ||
}); | ||
}; | ||
Client.prototype._makeRequest = function(params, cb) { | ||
params.agent = this._factory.agent; | ||
_.extend(params, this._factory.retryOptions); | ||
_.extend(params, this._factory.requestOptions); | ||
return request(params, cb); | ||
return this._tryRequest(params, 1, cb); | ||
}; | ||
@@ -181,0 +205,0 @@ |
{ | ||
"name": "eh-api-client", | ||
"version": "0.19.0", | ||
"version": "0.20.0", | ||
"description": "Node.js rest client", | ||
@@ -18,3 +18,3 @@ "main": "index.js", | ||
"q": "^1.4.1", | ||
"requestretry": "^1.4.0" | ||
"request": "^2.69.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "devDependencies": { |
@@ -6,4 +6,4 @@ var | ||
http = require("http"), | ||
async = require("async"), | ||
request = require("requestretry"); | ||
async = require("async"); | ||
//request = require("requestretry"); | ||
@@ -33,3 +33,5 @@ var SERVER_PORT = 8591; | ||
client.setRetryOptions({ | ||
retryStrategy: request.RetryStrategies.HTTPError | ||
retryStrategy: function() { | ||
return false; | ||
} | ||
}); | ||
@@ -85,2 +87,3 @@ var s; | ||
it("ECONNRESET should be processed and request retried", function(done) { | ||
var gotTryMoreThan1 = false; | ||
async.timesSeries(10, function(n, next) { | ||
@@ -90,5 +93,11 @@ client.get("/", function(err, data, res) { | ||
data.should.equal("done"); | ||
if(res.retryInfo.try > 1) { | ||
gotTryMoreThan1 = true; | ||
} | ||
setTimeout(next, SERVER_TIMEOUT - 5); | ||
}); | ||
}, done); | ||
}, function() { | ||
gotTryMoreThan1.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
@@ -100,2 +109,42 @@ | ||
}); | ||
describe("clients should share the factory's connection pool", function() { | ||
var factory = new Factory(url); | ||
var s; | ||
var requestsProcessed = 0; | ||
var connectionsMade = 0; | ||
before(function(done) { | ||
createServer(function(_s) { | ||
s = _s; | ||
s.on("request", function(req, res) { | ||
requestsProcessed++; | ||
res.end("done"); | ||
}); | ||
s.on("connection", function(req, res) { | ||
connectionsMade++; | ||
}); | ||
s.timeout = 1000; | ||
done(); | ||
}); | ||
}); | ||
after(function(done) { | ||
s.close(done); | ||
}); | ||
it("made 30 requests from different clients", function(done) { | ||
async.timesSeries(30, function(time, next) { | ||
var client = factory.getClient(time, "app"); | ||
client.get("/", function(err) { | ||
should.not.exists(err); | ||
next(); | ||
}); | ||
}, done); | ||
}); | ||
it("should be 30 requests with only one connection", function() { | ||
connectionsMade.should.equal(1); | ||
requestsProcessed.should.equal(30); | ||
factory.getPoolStats().should.equal("localhost:" + SERVER_PORT + ":: 1"); | ||
}); | ||
}); | ||
}); |
@@ -6,4 +6,3 @@ var | ||
http = require("http"), | ||
async = require("async"), | ||
request = require("requestretry"); | ||
async = require("async"); | ||
@@ -32,6 +31,2 @@ var SERVER_PORT = 8591; | ||
}); | ||
// retry only on network error | ||
client.setRetryOptions({ | ||
retryStrategy: request.RetryStrategies.NetworkError | ||
}); | ||
var s; | ||
@@ -72,6 +67,2 @@ before(function(done) { | ||
}); | ||
// retry only on network error | ||
client.setRetryOptions({ | ||
retryStrategy: request.RetryStrategies.HttpError | ||
}); | ||
var s; | ||
@@ -78,0 +69,0 @@ before(function(done) { |
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
24078
831
+ Addedrequest@^2.69.0
- Removedrequestretry@^1.4.0
- Removedlodash@4.17.21(transitive)
- Removedrequestretry@1.13.0(transitive)
- Removedwhen@3.7.8(transitive)