getConfigWithRetry function

Future<CDNConfig> getConfigWithRetry({
  1. CDNConfigOptions? options,
  2. int maxRetries = 2,
  3. int retryDelay = 1000,
})

Gets configuration with retry logic Attempts to fetch config multiple times before falling back

Usage:

final config = await getConfigWithRetry(maxRetries: 3);

@param options Optional CDN configuration options @param maxRetries Maximum number of retry attempts (default: 2) @param retryDelay Delay between retries in milliseconds (default: 1000) @returns Future

Implementation

Future<CDNConfig> getConfigWithRetry({
  CDNConfigOptions? options,
  int maxRetries = 2,
  int retryDelay = 1000,
}) async {
  int attempts = 0;
  Exception? lastError;

  while (attempts <= maxRetries) {
    try {
      return await fetchConfigFromCDN(options);
    } catch (error) {
      lastError = error as Exception;
      attempts++;

      if (attempts <= maxRetries) {
        // print('[CDN Config] ⚠️ Attempt $attempts failed, retrying in ${retryDelay}ms...');
        await Future.delayed(Duration(milliseconds: retryDelay));
      }
    }
  }

  // All retries failed, return local config
  // print('[CDN Config] ❌ All $maxRetries retry attempts failed: $lastError');
  return await _loadLocalConfig();
}