executeWithRetry<T> method

  1. @override
Future<T> executeWithRetry<T>(
  1. Future<T> operation()
)

Concrete implementation of the executeWithRetry method from RetryableLlmProvider

Implementation

@override
Future<T> executeWithRetry<T>(Future<T> Function() operation) async {
  if (!config.retryOnFailure) {
    return await operation();
  }

  int attempts = 0;
  Duration currentDelay = config.retryDelay;

  while (true) {
    try {
      return await operation().timeout(config.timeout);
    } catch (e, stackTrace) {
      attempts++;

      if (attempts >= config.maxRetries) {
        logger.error('Operation failed after $attempts attempts: $e');
        throw Exception('Max retry attempts reached: $e\n$stackTrace');
      }

      logger.warning(
          'Attempt $attempts failed, retrying in ${currentDelay.inMilliseconds}ms: $e');
      await Future.delayed(currentDelay);

      // Apply exponential backoff if enabled
      if (config.useExponentialBackoff) {
        currentDelay = Duration(
          milliseconds: (currentDelay.inMilliseconds * 2)
              .clamp(0, config.maxRetryDelay.inMilliseconds),
        );
      }
    }
  }
}