requestretry
Advanced tools
Comparing version 1.12.2 to 1.12.3
@@ -0,1 +1,25 @@ | ||
<a name="1.12.2"></a> | ||
## 1.12.2 (2017-08-01) | ||
* Added .auth, .jar and .cookie implementations along with corresponding tests ([15afe79](https://github.com/FGRibreau/node-request-retry/commit/15afe79)) | ||
* formatting updated + version bump ([6ad6f09](https://github.com/FGRibreau/node-request-retry/commit/6ad6f09)) | ||
* Improve README. ([5052add](https://github.com/FGRibreau/node-request-retry/commit/5052add)) | ||
* minor fix ([5b3f6d1](https://github.com/FGRibreau/node-request-retry/commit/5b3f6d1)) | ||
* Release v1.12.1. ([a1d198a](https://github.com/FGRibreau/node-request-retry/commit/a1d198a)) | ||
* Release v1.12.2. ([eba306c](https://github.com/FGRibreau/node-request-retry/commit/eba306c)) | ||
* revert ([d6a840f](https://github.com/FGRibreau/node-request-retry/commit/d6a840f)) | ||
* split various.test.js in auth.test.js and cookie.test.js ([c80272b](https://github.com/FGRibreau/node-request-retry/commit/c80272b)) | ||
* Update package.json ([9619999](https://github.com/FGRibreau/node-request-retry/commit/9619999)) | ||
* Update README.md ([9a50640](https://github.com/FGRibreau/node-request-retry/commit/9a50640)) | ||
* Update README.md ([ada51e3](https://github.com/FGRibreau/node-request-retry/commit/ada51e3)) | ||
* Update README.md ([984fd17](https://github.com/FGRibreau/node-request-retry/commit/984fd17)) | ||
* Update README.md ([94b8c01](https://github.com/FGRibreau/node-request-retry/commit/94b8c01)) | ||
* chore(package): update dependencies ([85c62ac](https://github.com/FGRibreau/node-request-retry/commit/85c62ac)) | ||
* chore(package): update nyc to version 10.0.0 ([f652825](https://github.com/FGRibreau/node-request-retry/commit/f652825)) | ||
* chore(package): update nyc to version 9.0.1 ([e376616](https://github.com/FGRibreau/node-request-retry/commit/e376616)) | ||
* chore(package): update sinon to version 1.17.6 ([2735e7c](https://github.com/FGRibreau/node-request-retry/commit/2735e7c)) | ||
* docs(changelog): updated ([448b9e1](https://github.com/FGRibreau/node-request-retry/commit/448b9e1)) | ||
<a name="1.12.0"></a> | ||
@@ -2,0 +26,0 @@ # 1.12.0 (2016-09-07) |
@@ -127,2 +127,4 @@ 'use strict'; | ||
response.attempts = this.attempts; | ||
} else if (err) { | ||
err.attempts = this.attempts; | ||
} | ||
@@ -129,0 +131,0 @@ if (this.retryStrategy(err, response, body) && this.maxAttempts > 0) { |
{ | ||
"name": "requestretry", | ||
"description": "request-retry wrap nodejs request to retry http(s) requests in case of error", | ||
"version": "1.12.2", | ||
"version": "1.12.3", | ||
"author": { | ||
@@ -6,0 +6,0 @@ "name": "Francois-Guillaume Ribreau", |
@@ -15,2 +15,7 @@ <div align="center"> | ||
> ## ❤️ Shameless plug | ||
> - [**Charts, simple as a URL**. No more server-side rendering pain, 1 url = 1 chart](https://image-charts.com) | ||
> - [Looking for a free **Redis GUI**?](http://redsmin.com) [Or for **real-time alerting** & monitoring for Redis?](http://redsmin.com) | ||
## Installation | ||
@@ -24,3 +29,3 @@ | ||
Request-retry is a drop-in replacement for [request](https://github.com/mikeal/request) but adds two new options `maxAttempts` and `retryDelay`. It also adds one property to the response, `attempts`. It supports callbacks or promises. | ||
Request-retry is a drop-in replacement for [request](https://github.com/mikeal/request) but adds two new options `maxAttempts` and `retryDelay`. It also adds one property to the response (or the error object, upon a network error), `attempts`. It supports callbacks or promises. | ||
@@ -153,2 +158,30 @@ ### With callbacks | ||
Here is how to implement an exponential backoff strategy: | ||
```javascript | ||
/** | ||
* @param {Number} attempts The number of times that the request has been attempted. | ||
* @return {Number} number of milliseconds to wait before retrying again the request. | ||
*/ | ||
function getExponentialBackoff(attempts) { | ||
return (Math.pow(2, attempts) * 100) + Math.floor(Math.random() * 50); | ||
} | ||
function constructExponentialBackoffStrategy() { | ||
let attempts = 0; | ||
return () => { | ||
attempts += 1; | ||
return getExponentialBackoff(attempts); | ||
}; | ||
} | ||
request({ | ||
url: 'https://api.domain.com/v1/a/b' | ||
json:true, | ||
delayStrategy: constructExponentialBackoffStrategy() // need to invoke the function to return the closure. | ||
}, function(err, response, body){ | ||
// this callback will only be called when the request succeeded or after maxAttempts or on error | ||
}); | ||
``` | ||
## Modifying `request` options | ||
@@ -155,0 +188,0 @@ |
@@ -28,2 +28,14 @@ 'use strict'; | ||
it('should show 3 attempts after retry even when a network error happens and response object is undefined', function (done) { | ||
request({ | ||
url: 'http://www.whatever-non-existant-domain-here.com/', // return a Could not resolve host: error | ||
maxAttempts: 3, | ||
retryDelay: 100 | ||
}, function (err, response, body) { | ||
t.strictEqual(err.attempts, 3); | ||
t.strictEqual(response, undefined); | ||
done(); | ||
}); | ||
}); | ||
it('should call delay strategy 2 times after some retries', function (done) { | ||
@@ -30,0 +42,0 @@ var mockDelayStrategy = sinon.stub().returns(500); |
59824
766
215