eh-api-client
Advanced tools
Comparing version 0.51.0 to 0.52.0
@@ -25,2 +25,9 @@ var | ||
retryDelay: 100, | ||
/** | ||
* | ||
* @param {*} err | ||
* @param {*} params | ||
* @param {*} params.retryOnTransientError allows retrying and request types | ||
* @returns | ||
*/ | ||
retryStrategy: function(err, params) { | ||
@@ -32,3 +39,3 @@ const method = params && params.method && params.method.toLowerCase() || '' | ||
} | ||
if(method === "get") { | ||
if(method === "get" || params.retryOnTransientError) { | ||
// https://man7.org/linux/man-pages/man3/errno.3.html | ||
@@ -35,0 +42,0 @@ return err.code === "ECONNRESET" || err.code === "ETIMEDOUT" || err.code === "ESOCKETTIMEDOUT" || err.code === 'ECONNREFUSED'; |
@@ -179,3 +179,4 @@ var | ||
headers: {}, | ||
formData: (options.formData ? options.formData: null) | ||
formData: (options.formData ? options.formData: null), | ||
retryOnTransientError: options.retryOnTransientError | ||
}; | ||
@@ -321,2 +322,5 @@ | ||
if(retryOptions && !bodyStream) { | ||
// check if retry strategy supports this error | ||
// also the options.retryOnTransientError says that request should be retried on transient error | ||
// by default only read-only requests can be retried | ||
var strategySupported = retryOptions.retryStrategy(err, params); | ||
@@ -323,0 +327,0 @@ var maxAttemptsReached = numTry >= retryOptions.maxAttempts; |
{ | ||
"name": "eh-api-client", | ||
"version": "0.51.0", | ||
"version": "0.52.0", | ||
"description": "Node.js rest client", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,2 +20,17 @@ ## Require | ||
## Request options | ||
You can pass request options in the first argument of request. Options are: | ||
- qs - query string parameters | ||
- url - request URL path | ||
- formData - form data | ||
- encoding | ||
- filter - data filter rules | ||
- range - data range rule | ||
- order - order rule | ||
- headers | ||
- timeout - request timeout | ||
- notFoundIsNull - if specified if request returns 404 status code then null will be returned instead of throwing error | ||
- retryOnTransientError - if specified then request will be retried if it fails on transient errors. By default only read only requests can be retried. With this option you can retry any request. | ||
## Events | ||
@@ -22,0 +37,0 @@ |
@@ -65,90 +65,2 @@ var | ||
describe.skip("use with retry if network error(Default behavior)", function() { | ||
var client = new Factory(url); | ||
var s; | ||
var requestsProcessed = 0; | ||
before(function(done) { | ||
createServer(function(_s) { | ||
s = _s; | ||
s.on("request", function(req, res) { | ||
requestsProcessed++; | ||
res.end("done"); | ||
}); | ||
s.timeout = SERVER_TIMEOUT; | ||
done(); | ||
}); | ||
}); | ||
after(function(done) { | ||
s.close(done); | ||
}); | ||
it("ECONNRESET should be processed and request retried", function(done) { | ||
var gotTryMoreThan1 = false; | ||
async.timesSeries(10, function(n, next) { | ||
client.get("/", function(err, data, res) { | ||
should.not.exists(err); | ||
data.should.equal("done"); | ||
if(res.retryInfo.try > 1) { | ||
gotTryMoreThan1 = true; | ||
} | ||
setTimeout(next, SERVER_TIMEOUT - 5); | ||
}); | ||
}, function() { | ||
gotTryMoreThan1.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it("count processed requests by server should match count of requests by client", function() { | ||
requestsProcessed.should.equal(10); | ||
}); | ||
}); | ||
describe.skip("POST use with retry if network error(Default behavior)", function() { | ||
var client = new Factory(url); | ||
var s; | ||
var requestsProcessed = 0; | ||
before(function(done) { | ||
createServer(function(_s) { | ||
s = _s; | ||
s.on("request", function(req, res) { | ||
requestsProcessed++; | ||
res.end("done"); | ||
}); | ||
s.timeout = SERVER_TIMEOUT; | ||
done(); | ||
}); | ||
}); | ||
after(function(done) { | ||
s.close(done); | ||
}); | ||
it("request shouldn't be retried", function(done) { | ||
var gotTryMoreThan1 = false; | ||
var gotReset; | ||
async.timesSeries(10, function(n, next) { | ||
client.post("/", {}, function(err, data, res) { | ||
if(err && err.message === "ECONNRESET") { | ||
gotReset = true; | ||
} | ||
else { | ||
data.should.equal("done"); | ||
if(res.retryInfo.try > 1) { | ||
gotTryMoreThan1 = true; | ||
} | ||
} | ||
setTimeout(next, SERVER_TIMEOUT - 5); | ||
}); | ||
}, function() { | ||
gotTryMoreThan1.should.equal(false); | ||
gotReset.should.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it("count processed requests by server should match count of requests by client", function() { | ||
requestsProcessed.should.be.lessThan(10); | ||
}); | ||
}); | ||
describe("clients should share the factory's connection pool", function() { | ||
@@ -155,0 +67,0 @@ var factory = new Factory(url); |
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
47918
18
61
1511
7