retryWithDelay<T> function

Future<T> retryWithDelay<T>(
  1. Future<T> fn(), {
  2. int maxRetries = 3,
  3. int initialDelayMs = 1000,
  4. double backoffMultiplier = 2.0,
})

Retry a function with exponential backoff

Implementation

Future<T> retryWithDelay<T>(
  Future<T> Function() fn, {
  int maxRetries = 3,
  int initialDelayMs = 1000,
  double backoffMultiplier = 2.0,
}) async {
  int attempt = 0;
  int delayMs = initialDelayMs;

  while (true) {
    try {
      return await fn();
    } catch (e) {
      attempt++;
      if (attempt >= maxRetries) {
        rethrow;
      }

      logDebug('Retry attempt $attempt after error: $e');
      await delay(delayMs);
      delayMs = (delayMs * backoffMultiplier).round();
    }
  }
}