Created
June 1, 2015 13:52
-
-
Save eckardt/0afcfa5e7df4b6c58ce8 to your computer and use it in GitHub Desktop.
Exponential backoff scheduler to be used with RxJS's retrywhen
This file contains hidden or 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
// 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