completeWithRetry method

Future<CompletionResponse> completeWithRetry(
  1. CompletionRequest request, {
  2. int maxRetries = 3,
  3. Duration delay = const Duration(seconds: 1),
})

Complete with retry logic for better reliability

Implementation

Future<CompletionResponse> completeWithRetry(
  CompletionRequest request, {
  int maxRetries = 3,
  Duration delay = const Duration(seconds: 1),
}) async {
  Exception? lastException;

  for (int attempt = 0; attempt < maxRetries; attempt++) {
    try {
      return await complete(request);
    } catch (e) {
      lastException = e is Exception ? e : Exception(e.toString());

      if (attempt < maxRetries - 1) {
        await Future.delayed(delay * (attempt + 1));
      }
    }
  }

  throw lastException!;
}