withRetry method
Adds retry logic to an existing runnable.
This method create a RunnableRetry instance, if the current Runnable throws an exception during invocation, it will be retried based on the configuration provided. By default the runnable will be retried 3 times with exponential delay between each retry.
maxRetries
- max attempts to retry the runnable.retryIf
- evaluator function to check whether to retry based the exception thrown.delayDurations
- by default runnable will be retried based on an exponential backoff strategy with base delay as 1 second. But you can override this behavior by providing an optional list of Durations.addJitter
- whether to add jitter to the delay.
Implementation
RunnableRetry<RunInput, RunOutput> withRetry({
final int maxRetries = 3,
final FutureOr<bool> Function(Object e)? retryIf,
final List<Duration?>? delayDurations,
final bool addJitter = false,
}) {
return RunnableRetry<RunInput, RunOutput>(
runnable: this,
defaultOptions: defaultOptions,
retryOptions: RetryOptions(
maxRetries: maxRetries,
retryIf: retryIf,
delayDurations: delayDurations,
addJitter: addJitter,
),
);
}