Skip to content

Instantly share code, notes, and snippets.

@eckardt
Created June 1, 2015 13:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eckardt/0afcfa5e7df4b6c58ce8 to your computer and use it in GitHub Desktop.
Save eckardt/0afcfa5e7df4b6c58ce8 to your computer and use it in GitHub Desktop.
Exponential backoff scheduler to be used with RxJS's retrywhen
// https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/retrywhen.md
function exponentialBackOffScheduler(options) {
return function(errors) {
return errors.scan(options, function(currentOptions, err) {
if (currentOptions.maxRetries <= 0) {
throw err;
}
return {
maxRetries: currentOptions.maxRetries - 1,
delay: currentOptions.delay * 2,
};
})
.map(_.property('delay'))
.scan(0, add)
.map(waitUntil)
.flatMap(_.identity);
}
function waitUntil(delay) {
return Rx.Observable.interval(delay).take(1);
}
function add(a, b) {
return a + b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment