-
-
Save justinfagnani/9f251d19e1f6beae4d80 to your computer and use it in GitHub Desktop.
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, retry, 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); | |
typedef TryAgain(bool cont, Duration delay, [NewFuture task]); | |
onError(err, iteration, elapsed, tryAgain) { | |
tryAgain(is404(err), limited(err, iteration, elapsed), () => http.get(otherServer)); | |
} | |
retry.start(doSomething, onError: onError); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment