blob: 1af472e1ffd1208f88cce9bf46ab627c15fbaa39 [file] [log] [blame]
{
"_from": "retry@0.9.0",
"_id": "retry@0.9.0",
"_location": "/npm/retry",
"_nodeVersion": "4.2.1",
"_npmUser": {
"email": "tim@debuggable.com",
"name": "tim-kos"
},
"_npmVersion": "2.1.7",
"_phantomChildren": {},
"_requiredBy": [
"/npm"
],
"_resolved": "https://registry.npmjs.org/retry/-/retry-0.9.0.tgz",
"_shasum": "6f697e50a0e4ddc8c8f7fb547a9b60dead43678d",
"_shrinkwrap": null,
"author": {
"email": "tim@debuggable.com",
"name": "Tim Koschützki",
"url": "http://debuggable.com/"
},
"bugs": {
"url": "https://github.com/tim-kos/node-retry/issues"
},
"dependencies": {},
"description": "Abstraction for exponential and custom retry strategies for failed operations.",
"devDependencies": {
"fake": "0.2.0",
"far": "0.0.1"
},
"directories": {
"lib": "./lib"
},
"dist": {
"shasum": "6f697e50a0e4ddc8c8f7fb547a9b60dead43678d",
"tarball": "http://registry.npmjs.org/retry/-/retry-0.9.0.tgz"
},
"engines": {
"node": "*"
},
"gitHead": "1b621cf499ef7647d005e3650006b93a8dbeb986",
"homepage": "https://github.com/tim-kos/node-retry",
"license": "MIT",
"main": "index",
"maintainers": [
{
"email": "tim@debuggable.com",
"name": "tim-kos"
}
],
"name": "retry",
"optionalDependencies": {},
"readme": "# retry\n\nAbstraction for exponential and custom retry strategies for failed operations.\n\n## Installation\n\n npm install retry\n\n## Current Status\n\nThis module has been tested and is ready to be used.\n\n## Tutorial\n\nThe example below will retry a potentially failing `dns.resolve` operation\n`10` times using an exponential backoff strategy. With the default settings, this\nmeans the last attempt is made after `17 minutes and 3 seconds`.\n\n``` javascript\nvar dns = require('dns');\nvar retry = require('retry');\n\nfunction faultTolerantResolve(address, cb) {\n var operation = retry.operation();\n\n operation.attempt(function(currentAttempt) {\n dns.resolve(address, function(err, addresses) {\n if (operation.retry(err)) {\n return;\n }\n\n cb(err ? operation.mainError() : null, addresses);\n });\n });\n}\n\nfaultTolerantResolve('nodejs.org', function(err, addresses) {\n console.log(err, addresses);\n});\n```\n\nOf course you can also configure the factors that go into the exponential\nbackoff. See the API documentation below for all available settings.\ncurrentAttempt is an int representing the number of attempts so far.\n\n``` javascript\nvar operation = retry.operation({\n retries: 5,\n factor: 3,\n minTimeout: 1 * 1000,\n maxTimeout: 60 * 1000,\n randomize: true,\n});\n```\n\n## API\n\n### retry.operation([options])\n\nCreates a new `RetryOperation` object. `options` is the same as `retry.timeouts()`'s `options`, with two additions:\n\n* `forever`: Whether to retry forever, defaults to `false`.\n* `unref`: Wether to [unref](https://nodejs.org/api/timers.html#timers_unref) the setTimeout's, defaults to `false`.\n\n### retry.timeouts([options])\n\nReturns an array of timeouts. All time `options` and return values are in\nmilliseconds. If `options` is an array, a copy of that array is returned.\n\n`options` is a JS object that can contain any of the following keys:\n\n* `retries`: The maximum amount of times to retry the operation. Default is `10`.\n* `factor`: The exponential factor to use. Default is `2`.\n* `minTimeout`: The number of milliseconds before starting the first retry. Default is `1000`.\n* `maxTimeout`: The maximum number of milliseconds between two retries. Default is `Infinity`.\n* `randomize`: Randomizes the timeouts by multiplying with a factor between `1` to `2`. Default is `false`.\n\nThe formula used to calculate the individual timeouts is:\n\n```\nvar Math.min(random * minTimeout * Math.pow(factor, attempt), maxTimeout);\n```\n\nHave a look at [this article][article] for a better explanation of approach.\n\nIf you want to tune your `factor` / `times` settings to attempt the last retry\nafter a certain amount of time, you can use wolfram alpha. For example in order\nto tune for `10` attempts in `5 minutes`, you can use this equation:\n\n![screenshot](https://github.com/tim-kos/node-retry/raw/master/equation.gif)\n\nExplaining the various values from left to right:\n\n* `k = 0 ... 9`: The `retries` value (10)\n* `1000`: The `minTimeout` value in ms (1000)\n* `x^k`: No need to change this, `x` will be your resulting factor\n* `5 * 60 * 1000`: The desired total amount of time for retrying in ms (5 minutes)\n\nTo make this a little easier for you, use wolfram alpha to do the calculations:\n\n<http://www.wolframalpha.com/input/?i=Sum%5B1000*x^k%2C+{k%2C+0%2C+9}%5D+%3D+5+*+60+*+1000>\n\n[article]: http://dthain.blogspot.com/2009/02/exponential-backoff-in-distributed.html\n\n### retry.createTimeout(attempt, opts)\n\nReturns a new `timeout` (integer in milliseconds) based on the given parameters.\n\n`attempt` is an integer representing for which retry the timeout should be calculated. If your retry operation was executed 4 times you had one attempt and 3 retries. If you then want to calculate a new timeout, you should set `attempt` to 4 (attempts are zero-indexed).\n\n`opts` can include `factor`, `minTimeout`, `randomize` (boolean) and `maxTimeout`. They are documented above.\n\n`retry.createTimeout()` is used internally by `retry.timeouts()` and is public for you to be able to create your own timeouts for reinserting an item, see [issue #13](https://github.com/tim-kos/node-retry/issues/13).\n\n### retry.wrap(obj, [options], [methodNames])\n\nWrap all functions of the `obj` with retry. Optionally you can pass operation options and\nan array of method names which need to be wrapped.\n\n```\nretry.wrap(obj)\n\nretry.wrap(obj, ['method1', 'method2']);\n\nretry.wrap(obj, {retries: 3});\n\nretry.wrap(obj, {retries: 3}, ['method1', 'method2']);\n```\nThe `options` object can take any options that the usual call to `retry.operation` can take.\n\n### new RetryOperation(timeouts, [options])\n\nCreates a new `RetryOperation` where `timeouts` is an array where each value is\na timeout given in milliseconds.\n\nAvailable options:\n* `forever`: Whether to retry forever, defaults to `false`.\n* `unref`: Wether to [unref](https://nodejs.org/api/timers.html#timers_unref) the setTimeout's, defaults to `false`.\n\nIf `forever` is true, the following changes happen:\n* `RetryOperation.errors()` will only output an array of one item: the last error.\n* `RetryOperation` will repeatedly use the last item in the `timeouts` array.\n\n#### retryOperation.errors()\n\nReturns an array of all errors that have been passed to\n`retryOperation.retry()` so far.\n\n#### retryOperation.mainError()\n\nA reference to the error object that occured most frequently. Errors are\ncompared using the `error.message` property.\n\nIf multiple error messages occured the same amount of time, the last error\nobject with that message is returned.\n\nIf no errors occured so far, the value is `null`.\n\n#### retryOperation.attempt(fn, timeoutOps)\n\nDefines the function `fn` that is to be retried and executes it for the first\ntime right away. The `fn` function can receive an optional `currentAttempt` callback that represents the number of attempts to execute `fn` so far.\n\nOptionally defines `timeoutOps` which is an object having a property `timeout` in miliseconds and a property `cb` callback function.\nWhenever your retry operation takes longer than `timeout` to execute, the timeout callback function `cb` is called.\n\n\n#### retryOperation.try(fn)\n\nThis is an alias for `retryOperation.attempt(fn)`. This is deprecated. Please use `retryOperation.attempt(fn)` instead.\n\n#### retryOperation.start(fn)\n\nThis is an alias for `retryOperation.attempt(fn)`. This is deprecated. Please use `retryOperation.attempt(fn)` instead.\n\n#### retryOperation.retry(error)\n\nReturns `false` when no `error` value is given, or the maximum amount of retries\nhas been reached.\n\nOtherwise it returns `true`, and retries the operation after the timeout for\nthe current attempt number.\n\n#### retryOperation.attempts()\n\nReturns an int representing the number of attempts it took to call `fn` before it was successful.\n\n## License\n\nretry is licensed under the MIT license.\n\n\n# Changelog\n\n0.8.0 Implementing retry.wrap.\n\n0.7.0 Some bug fixes and made retry.createTimeout() public. Fixed issues [#10](https://github.com/tim-kos/node-retry/issues/10), [#12](https://github.com/tim-kos/node-retry/issues/12), and [#13](https://github.com/tim-kos/node-retry/issues/13).\n\n0.6.0 Introduced optional timeOps parameter for the attempt() function which is an object having a property timeout in milliseconds and a property cb callback function. Whenever your retry operation takes longer than timeout to execute, the timeout callback function cb is called.\n\n0.5.0 Some minor refactoring.\n\n0.4.0 Changed retryOperation.try() to retryOperation.attempt(). Deprecated the aliases start() and try() for it.\n\n0.3.0 Added retryOperation.start() which is an alias for retryOperation.try().\n\n0.2.0 Added attempts() function and parameter to retryOperation.try() representing the number of attempts it took to call fn().\n",
"readmeFilename": "Readme.md",
"repository": {
"type": "git",
"url": "git://github.com/tim-kos/node-retry.git"
},
"scripts": {},
"version": "0.9.0"
}