BackOff<T> class
BackOff is used for holding options for retrying a function.
With the default configuration functions will be retried up-to 7 times (8 attempts in total), sleeping 1st, 2nd, 3rd, ..., 7th attempt:
- 400 ms ± 25%
- 800 ms ± 25%
- 1600 ms ± 25%
- 3200 ms ± 25%
- 6400 ms ± 25%
- 12800 ms ± 25%
- 25600 ms ± 25%
Usage Example:
final response = await BackOff(
() => http.get('https://google.com').timeout(
const Duration(seconds: 10),
),
retryIf: (error, stackTrace, attempt) => error is SocketException || error is TimeoutException,
initialDelay: const Duration(milliseconds: 200),
maxAttempts: 8,
percentageRandomization: 0.25,
maxDelay: const Duration(seconds: 30),
).call();
print(response.body);
Constructors
-
BackOff(FutureOr<
T> func(), {Duration initialDelay = const Duration(milliseconds: 200), double percentageRandomization = 0.25, Duration maxDelay = const Duration(seconds: 30), int maxAttempts = 8, FutureOr<bool> retryIf(Object error, StackTrace stackTrace, int attempt)?}) - BackOff is used for holding options for retrying a function.
Properties
-
func
→ FutureOr<
T> Function() -
The Function to execute. If the function throws an error, it will be
retried maxAttempts times with an increasing delay between each attempt
up to maxDelay.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- initialDelay → Duration
-
Delay factor to double after every attempt.
final
- maxAttempts → int
-
Maximum number of attempts before giving up, defaults to 8.
final
- maxDelay → Duration
-
Maximum delay between retries, defaults to 30 seconds.
final
- percentageRandomization → double
-
Percentage the delay should be randomized, given as fraction between
0 and 1, (0.0 to 1.0 recommended).
final
-
retryIf
→ FutureOr<
bool> Function(Object error, StackTrace stackTrace, int attempt)? -
Function to determine if a retry should be attempted.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
call(
) → Future< T> -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited