execute<T> method

Future<T> execute<T>(
  1. Future<T> fn()
)

Executes a function with retry logic

Implementation

Future<T> execute<T>(Future<T> Function() fn) async {
  var attempt = 0;
  var delay = initialDelayMs;

  while (true) {
    attempt++;

    try {
      return await fn();
    } catch (e) {
      // Check if we should retry
      if (attempt >= maxAttempts) {
        rethrow;
      }

      // Check if error is retryable
      if (!_isRetryable(e)) {
        rethrow;
      }

      // Add jitter to prevent thundering herd
      final jitter = Random().nextInt(100);
      await Future<void>.delayed(Duration(milliseconds: delay + jitter));

      // Exponential backoff
      delay = min((delay * backoffMultiplier).toInt(), maxDelayMs);
    }
  }
}