Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
FGRibreau committed Mar 23, 2014
1 parent f3b0566 commit 370095e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
# RetriableRequest
# Request-retry [![Deps](https://david-dm.org/FGRibreau/request-retry.png)](https://david-dm.org/FGRibreau/request-retry)

When the connection fails with one of `ECONNRESET`, `ENOTFOUND`, `ESOCKETTIMEDOUT`, `ETIMEDOUT`, `ECONNREFUSED` or when an HTTP 5xx error occurrs, the request will automatically be re-attempted as these are often recoverable errors and will go away on retry.

# Usage

'ECONNRESET', 'ENOTFOUND', 'ESOCKETTIMEDOUT', 'ETIMEDOUT', 'ECONNREFUSED'
Request-retry is a drop-in replacement for Request, it just adds two new options `maxAttempts` and `retryDelay`.

## Installation
```javascript
var request = require('requestretry');

request({
url: 'https://api.domain.com/v1/a/b'
json:true,

Install with [npm](https://npmjs.org/package/retriablerequest).
// The above parameters are specific to Request-retry:
maxAttempts: 5, // (default) try 5 times
retryDelay: 5000 // (default) wait for 5s before trying again
}, function(err, response, body){
// this callback will only be called when the request succeeded or after maxAttempts.
});
```

## Installation

npm install --save retriablerequest
Install with [npm](https://npmjs.org/package/requestretry).

npm install --save requestretry

## Changelog

v0.0.1: Initial commit
v1.0.0: Initial commit

Copyright 2014, [Francois-Guillaume Ribreau](http://) ([email protected])
Copyright 2014, [Francois-Guillaume Ribreau](http://fgribreau.com) ([email protected])
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ var DEFAULTS = {
};

function isRetriableRequest(err, response){
// Inspired from https://github.com/geoffreak/request-enhanced/blob/master/src/request-enhanced.coffee#L107
return (err && _.contains(RETRIABLE_ERRORS, err.code)) || (response && 500 <= response.statusCode && response.statusCode < 600);
}

function tryUntilFail(options, f, retryOptions){
retryOptions.maxAttempts--;

request(options, function(err, response, body){
if(isRetriableRequest(err, response)){
if(isRetriableRequest(err, response) && retryOptions.maxAttempts >= 0){
return setTimeout(tryUntilFail.bind(null, options, f, retryOptions), retryOptions.retryDelay);
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "retriablerequest",
"description": "Retriable Request",
"name": "requestretry",
"description": "request-retry wrap nodejs request to retry http(s) requests in case of error",
"author": {
"name": "Francois-Guillaume Ribreau",
"email": "[email protected]",
"url": "http://fgribreau.com"
},
"version": "0.0.1",
"repository": {
"url": "https://github.com/FGRibreau/node-retriablerequest"
"url": "https://github.com/FGRibreau/node-request-retry"
},
"main": "index.js",
"license": "MIT",
Expand Down

0 comments on commit 370095e

Please sign in to comment.