Last active
August 29, 2015 14:05
-
-
Save yjbanov/2d743a7ac12c59f00b95 to your computer and use it in GitHub Desktop.
Quiver delay function composition 2
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
/// Returns a [Duration] if another attempt needs to be made after | |
/// waiting for the returned [Duration]. Returns the error object | |
/// that instructs the retry mechanism to stop trying and report error | |
/// immediately. | |
typedef dynamic OnErrorFunc(Object err, int retry, Duration elapsed); | |
/// `onError` returns either [Duration] or an error object. If [Duration] | |
/// is returned, the `attempt` function will be retried after waiting | |
/// for the returned duration, otherwise the retry mechanism gives up | |
/// and evaluates the [Future] with the the error object. | |
Future start( | |
AttemptFunc attempt, { | |
OnErrorFunc onError: retriesInfinite, | |
Stopwatch runTime}) | |
---------------------------------------------------------------------------- | |
import 'package:quiver/retry.dart' as retry; | |
delayFn(err, iteration, elapsed) => | |
is503(err) | |
? retry.randomDelay(new Duration(seconds: 1), 0.8) | |
: retry.capDelay(retry.randomDelay(new Duration(seconds: 1), 0.4), | |
const Duration(milliseconds: 1200)); | |
var limited = retry.makeRetriesLimited(10, new Duration(seconds: 10), delayFn); | |
onError(err, iteration, elapsed) => | |
is404(err) ? err : limited(err, iteration, elapsed); | |
retry.start(doSomething, onError: onError); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment