nextDelay method

  1. @override
FutureOr<Duration?> nextDelay(
  1. RetryContext context
)
override

Returns the delay to wait before the next attempt, or null to stop retrying and let the original error propagate.

Called once per failed attempt with a fresh context.

Implementation

@override
FutureOr<Duration?> nextDelay(final RetryContext context) async {
  if (context.attempt > maxAttempts) {
    // No attempts remain.
    return null;
  }

  if (context.isProcedure &&
      context.isAmbiguous &&
      !retryProcedureOnAmbiguousFailure) {
    // Retrying a non-idempotent request the server may already have applied
    // could duplicate its side effect.
    return null;
  }

  int intervalInSeconds =
      _computeExponentialBackOff(context.attempt) + _jitter;

  final retryAfter = context.retryAfter;
  if (retryAfter != null) {
    final atLeastInSeconds = (retryAfter.inMilliseconds / 1000).ceil();
    if (atLeastInSeconds > intervalInSeconds) {
      intervalInSeconds = math.min(atLeastInSeconds, _maxServerWaitInSeconds);
    }
  }

  await onExecute?.call(
    RetryEvent(
      retryCount: context.attempt,
      intervalInSeconds: intervalInSeconds,
    ),
  );

  return Duration(seconds: intervalInSeconds);
}