Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(main): allow retries to be limited #3

Merged
merged 1 commit into from
Aug 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var retryInstance = new Retry({
* **retryBase** – the base of the delay exponent. Defaults to `1.2`.
* **retryExponent** – the maximum exponent of the delay exponent. If retries are higher than `retryExponent`, `retryExponent` will be used rather than the retry number. Defaults to `33` which means on average max delay of 3m 25s.
* **retryDelay** – a function used to calculate the delay. Replaces the default exponent calculation. If it returns `false` the retries will be aborted.
* **retryLimit** – maximum amount of retries. Defaults to unlimited retries.
* **log** – a logger function. Defaults to `console.log()`.

## Methods
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var Retry = function (options) {
retryMin: 0,
retryBase: 1.2,
retryExponent: 33,
retryLimit: undefined,
retryDelay: function (retries) {
return resolvedOptions.retryMin + Math.floor(
1000 *
Expand Down Expand Up @@ -76,6 +77,12 @@ Retry.prototype._try = function () {
self.retrying = undefined;
self.abort = undefined;


if (self.options.retryLimit !== undefined && self.failures >= self.options.retryLimit) {
self.end();
return Promise.reject(new Error('Retry limit reached'));
}

return self._try();
});
};
Expand Down
23 changes: 23 additions & 0 deletions test/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,29 @@ describe('Retry', function () {
});
});

it('should abort retries after limit is reached', function () {
tryStub.rejects(new Error('foo'));

retryInstance = new Retry({
try: tryStub,
success: successSpy,
end: endSpy,
retryDelay: retryStub,
log: function () {},
retryLimit: 0
});

return retryInstance.try().catch(function (err) {
err.should.be.an('Error');
err.message.should.equal('Retry limit reached');

tryStub.should.have.been.calledOnce;
successSpy.should.not.have.been.called;
endSpy.should.not.have.been.called;
retryStub.should.not.have.been.called;
});
});

});

});