execute<T> method

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

Executes an operation with retry logic

Implementation

Future<T> execute<T>(Future<T> Function() operation) async {
  int retryCount = 0;
  Duration backoff = initialBackoff;
  final random = Random();

  while (true) {
    try {
      return await operation();
    } catch (e) {
      // Check if we've reached the maximum number of retries
      if (retryCount >= maxRetries) {
        rethrow;
      }

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

      // Calculate the next backoff duration
      final nextBackoff = _calculateNextBackoff(backoff, retryCount, random);

      // Wait for the backoff duration
      await Future.delayed(nextBackoff);

      // Increment the retry count and update the backoff
      retryCount++;
      backoff = Duration(
        milliseconds: (backoff.inMilliseconds * backoffMultiplier).toInt(),
      );
      if (backoff > maxBackoff) {
        backoff = maxBackoff;
      }
    }
  }
}