Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
271 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
repo_token: GGrIvueHkse8Y8kJX73OvmWDsyXuySD3o |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/.nyc_output/ | ||
/coverage/ | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- stable | ||
- 4 | ||
after_success: | ||
- "npm run coveralls" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Jim Fitzpatrick | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,75 @@ | ||
# retriable-promise | ||
|
||
WIP | ||
Add retry logic to a Promise-returning function. | ||
|
||
[![npm Version][npm-badge]][npm] | ||
[![Build Status][build-badge]][build-status] | ||
[![Test Coverage][coverage-badge]][coverage-result] | ||
[![Dependency Status][dep-badge]][dep-status] | ||
|
||
## Installation | ||
|
||
Install using [npm][]: | ||
|
||
$ npm install retriable-promise | ||
|
||
## Usage | ||
|
||
__retriable-promise__ exports a function that accepts a Promise-returning | ||
function and an options object, returning a new function that decorates the | ||
given one with retry logic: | ||
|
||
retriable(func, options) | ||
|
||
```js | ||
const retriable = require('retriable-promise'); | ||
const api = require('./my-api-module'); | ||
|
||
const fetchStuff = retriable(api.fetchStuff, { | ||
// Retry 3 times, delayed 0/500/1000 ms respectively | ||
retries: [0, 500, 1000], | ||
|
||
// Only when the status code is 429 | ||
when(err) { | ||
return err.statusCode === 429; | ||
} | ||
}); | ||
``` | ||
|
||
## Available Options | ||
|
||
#### `options.retries` (Array, required) | ||
|
||
An array of integers, representing the delay (in ms) to wait before invoking | ||
each retry attempt. For example, passing `{ wait: [1000, 1000, 1000] }` would | ||
configure the returning function to retry up to 3 times, waiting 1 second | ||
before each subsequent attempt. | ||
|
||
#### `options.when` (Function) | ||
|
||
By default, retries will be invoked for all Promise rejections. In order to | ||
refine which failures are retriable, specify a `when` function that receives | ||
the Promise rejection value as an argument and returns `true` or `false` | ||
depending on whether a retry should be made. | ||
|
||
#### `options.Promise` (Function) | ||
|
||
The particular Promise implementation can be overridden by specifying | ||
`options.Promise`, which defaults to the native ES2015 `Promise`. Note that if | ||
this option is specified, the given function should conform to the native | ||
Promise constructor API, i.e., it is expected to take a callback function that | ||
itself receives `resolve` and `reject` callbacks and returns an appropriate | ||
promise instance. | ||
|
||
## License | ||
|
||
MIT | ||
|
||
[build-badge]: https://img.shields.io/travis/jimf/retriable-promise/master.svg | ||
[build-status]: https://travis-ci.org/jimf/retriable-promise | ||
[npm-badge]: https://img.shields.io/npm/v/retriable-promise.svg | ||
[npm]: https://www.npmjs.org/package/retriable-promise | ||
[coverage-badge]: https://img.shields.io/coveralls/jimf/retriable-promise.svg | ||
[coverage-result]: https://coveralls.io/r/jimf/retriable-promise | ||
[dep-badge]: https://img.shields.io/david/jimf/retriable-promise.svg | ||
[dep-status]: https://david-dm.org/jimf/retriable-promise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
function T () { return true } | ||
|
||
/** | ||
* Decorate a Promise-returning function to add retry behavior. | ||
* | ||
* @param {function} fn A Promise-returning function | ||
* @param {object} opts Configuration options | ||
* @param {number[]} opts.retries Array of retry delay timeouts (in ms) | ||
* @param {function} [opts.Promise] Promise implementation (defaults to native) | ||
* @return {function} | ||
*/ | ||
module.exports = function retriable (fn, opts) { | ||
return function () { | ||
var args = arguments | ||
var retries = opts.retries.slice(0) | ||
var when = opts.when || T | ||
var P = opts.Promise || Promise | ||
|
||
return new P(function (resolve, reject) { | ||
function fail (err) { | ||
if (when(err) && retries.length > 0) { | ||
setTimeout(run, retries.shift()) | ||
} else { | ||
reject(err) | ||
} | ||
} | ||
|
||
function run () { | ||
fn.apply(null, args) | ||
.then(resolve) | ||
.catch(fail) | ||
} | ||
|
||
run() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"name": "retriable-promise", | ||
"version": "0.1.0", | ||
"description": "Add retry logic to a promise-returning function", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/jimf/retriable-promise" | ||
}, | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"coveralls": "coveralls < coverage/lcov.info", | ||
"lint": "standard", | ||
"pretest": "npm run lint", | ||
"test": "nyc tape test/*.js" | ||
}, | ||
"nyc": { | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
] | ||
}, | ||
"keywords": [], | ||
"author": "Jim Fitzpatrick", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/jimf/retriable-promise/issues" | ||
}, | ||
"homepage": "https://github.com/jimf/retriable-promise", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"bluebird": "^3.5.1", | ||
"coveralls": "^3.0.0", | ||
"nyc": "^11.2.1", | ||
"standard": "^10.0.3", | ||
"tape": "^4.8.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
var test = require('tape') | ||
var BluebirdPromise = require('bluebird') | ||
var retriable = require('..') | ||
|
||
function resolve (value) { | ||
return { type: 'resolve', value } | ||
} | ||
|
||
function reject (value) { | ||
return { type: 'reject', value } | ||
} | ||
|
||
function createCallback (results, P = Promise) { | ||
var cb = function () { | ||
var result = results.shift() | ||
var args = Array.prototype.slice.call(arguments) | ||
cb.calls.push(args) | ||
return result.type === 'resolve' | ||
? P.resolve(result.value) | ||
: P.reject(result.value) | ||
} | ||
cb.calls = [] | ||
return cb | ||
} | ||
|
||
test('when promise resolves on initial attempt', function (t) { | ||
t.plan(2) | ||
var cb = createCallback([resolve(true)]) | ||
var wrapped = retriable(cb, { | ||
retries: [0, 0, 0] | ||
}) | ||
wrapped('dummy-arg').then(function (result) { | ||
t.equal(result, true, 'resolves with expected result') | ||
t.deepEqual(cb.calls, [['dummy-arg']], 'retries zero times') | ||
}) | ||
}) | ||
|
||
test('when a retry attempt succeeds', function (t) { | ||
t.plan(2) | ||
var cb = createCallback([reject(false), resolve(true)]) | ||
var wrapped = retriable(cb, { | ||
retries: [0, 0, 0] | ||
}) | ||
wrapped('dummy-arg').then(function (result) { | ||
t.equal(result, true, 'resolves with expected result') | ||
t.deepEqual(cb.calls, [['dummy-arg'], ['dummy-arg']], 'retries expected number of times') | ||
}) | ||
}) | ||
|
||
test('when no retry attempts succeed', function (t) { | ||
t.plan(2) | ||
var cb = createCallback([reject(false), reject(false), reject(false), reject(false)]) | ||
var wrapped = retriable(cb, { | ||
retries: [0, 0, 0] | ||
}) | ||
wrapped('dummy-arg').catch(function (result) { | ||
t.equal(result, false, 'rejects with expected result') | ||
t.deepEqual(cb.calls, [['dummy-arg'], ['dummy-arg'], ['dummy-arg'], ['dummy-arg']], | ||
'retries expected number of times') | ||
}) | ||
}) | ||
|
||
test('when promise rejects and does not pass "when" test', function (t) { | ||
t.plan(2) | ||
var cb = createCallback([reject(false)]) | ||
var wrapped = retriable(cb, { | ||
retries: [0, 0, 0], | ||
when: function (err) { | ||
return err === 'retriable-error' | ||
} | ||
}) | ||
wrapped('dummy-arg').catch(function (result) { | ||
t.equal(result, false, 'rejects with expected result') | ||
t.deepEqual(cb.calls, [['dummy-arg']], 'retries zero times') | ||
}) | ||
}) | ||
|
||
test('supports alternative Promise implementations', function (t) { | ||
t.plan(3) | ||
var cb = createCallback([reject(false), resolve(true)], BluebirdPromise) | ||
var wrapped = retriable(cb, { | ||
retries: [0, 0, 0], | ||
Promise: BluebirdPromise | ||
}) | ||
var p = wrapped('dummy-arg').then(function (result) { | ||
t.equal(result, true, 'resolves with expected result') | ||
t.deepEqual(cb.calls, [['dummy-arg'], ['dummy-arg']], 'retries expected number of times') | ||
}) | ||
t.ok(p instanceof BluebirdPromise, 'returns desired Promise instance') | ||
}) |