Skip to content

Commit a130355

Browse files
author
Brian Deitte
committed
Add the attempts property for retry attempt info. Add a test for it. Also fix maxAttempts being off by one as shown by the tests.
1 parent bf0ff38 commit a130355

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ When the connection fails with one of `ECONNRESET`, `ENOTFOUND`, `ESOCKETTIMEDOU
66

77
## Usage
88

9-
Request-retry is a drop-in replacement for [request](https://github.com/mikeal/request) but adds two new options `maxAttempts` and `retryDelay`.
9+
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`.
1010

1111
```javascript
1212
var request = require('requestretry');
@@ -21,6 +21,9 @@ request({
2121
retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
2222
}, function(err, response, body){
2323
// this callback will only be called when the request succeeded or after maxAttempts or on error
24+
if (response) {
25+
console.log('The number of request attempts: ' + response.attempts);
26+
}
2427
});
2528
```
2629

@@ -62,7 +65,6 @@ var request = require('requestretry').request.defaults({my: options});
6265
## Todo
6366

6467
- Tests
65-
- Use an EventEmitter to notify retries
6668

6769
## [Changelog](CHANGELOG.md)
6870

‎index.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var DEFAULTS = {
2020
function Request(options, f, maxAttempts, retryDelay) {
2121
this.maxAttempts = maxAttempts;
2222
this.retryDelay = retryDelay;
23+
this.attempts = 0;
2324

2425
/**
2526
* Option object
@@ -42,9 +43,13 @@ Request.request = request;
4243

4344
Request.prototype._tryUntilFail = function () {
4445
this.maxAttempts--;
46+
this.attempts++;
4547

4648
this._req = Request.request(this.options, function (err, response, body) {
47-
if (this.retryStrategy(err, response) && this.maxAttempts >= 0) {
49+
if (response) {
50+
response.attempts = this.attempts;
51+
}
52+
if (this.retryStrategy(err, response) && this.maxAttempts > 0) {
4853
this._timeout = setTimeout(this._tryUntilFail.bind(this), this.retryDelay);
4954
return;
5055
}

‎test/abort.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var t = require('chai').assert;
66
describe('Request-retry', function () {
77
it('should return a RequestRetry handler object with a abort method', function (done) {
88
var o = request({
9-
url: 'http://filltext.com/?rows=1&delay=10', // wait for 4s
9+
url: 'http://filltext.com/?rows=1&delay=10', // wait for 10s
1010
json: true
1111
}, function (err) {
1212
t.strictEqual(err.toString(), 'Error: Aborted');

0 commit comments

Comments
 (0)