public void Download(CancellationToken cancellationToken) { try { _logger.LogDebug("Downloading..."); _logger.LogTrace("size = " + _size); for (int i = 0; i < _urls.Length; i++) { _logger.LogTrace("urls[" + i + "].Url = " + _urls[i].Url); _logger.LogTrace("urls[" + i + "].Country = " + _urls[i].Country); _logger.LogTrace("urls[" + i + "].PartSize = " + _urls[i].PartSize); } _logger.LogTrace("chunksData.ChunkSize = " + _chunksData.ChunkSize); _logger.LogTrace("chunksData.Chunks.Length = " + _chunksData.Chunks.Length); Assert.MethodCalledOnlyOnce(ref _downloadHasBeenCalled, "Download"); using (var fileStream = OpenFileStream(cancellationToken)) { bool retry; do { bool success = _urls.Any(url => TryDownload(url, fileStream, cancellationToken)); if (success) { retry = false; } else { _logger.LogWarning("All server requests have failed. Checking if retry is possible..."); _retryStrategy.OnRequestFailure(); _timeoutCalculator.OnRequestFailure(); retry = _retryStrategy.ShouldRetry; if (!retry) { throw new DownloadFailureException("Download failure."); } _logger.LogDebug(string.Format("Retry is possible. Waiting {0}ms until before attempt...", _retryStrategy.DelayBeforeNextTry)); Threading.CancelableSleep(_retryStrategy.DelayBeforeNextTry, cancellationToken); _logger.LogDebug("Trying to download data once again from each server..."); } } while (retry); } _logger.LogDebug("Downloading finished."); } catch (Exception e) { _logger.LogDebug("Download has failed.", e); throw; } }
public static void TryExecute(Action action, IRequestRetryStrategy retryStrategy, PatchKit.Unity.Patcher.Cancellation.CancellationToken cancellationToken) { do { try { action(); return; } catch (IOException e) { retryStrategy.OnRequestFailure(); if (!retryStrategy.ShouldRetry) { DebugLogger.LogError(string.Format("An IO Exception has occured: {0}. rethrowing.", e)); throw; } DebugLogger.LogWarning(string.Format("An IO Exception has occured: {0}. retrying...", e)); Threading.CancelableSleep(retryStrategy.DelayBeforeNextTry, cancellationToken); } } while (retryStrategy.ShouldRetry); }
private IApiResponse SendRequest(Func <Request> requestBuilder) { try { var request = requestBuilder(); IApiResponse apiResponse; bool retry; do { if (!TrySendRequest(_connectionSettings.MainServer, request, ServerType.MainServer, out apiResponse)) { if (_connectionSettings.CacheServers != null && request.Method == RequestMethod.Get) { foreach (var cacheServer in _connectionSettings.CacheServers) { if (TrySendRequest(cacheServer, request, ServerType.CacheServer, out apiResponse)) { break; } } } } if (apiResponse == null) { Logger.LogWarning( "Connection attempt to every server has failed. Checking whether retry is possible..."); RequestTimeoutCalculator.OnRequestFailure(); RequestRetryStrategy.OnRequestFailure(); retry = RequestRetryStrategy.ShouldRetry; if (!retry) { Logger.LogError("Retry is not possible."); throw new ApiConnectionException(request.MainServerExceptions, request.CacheServersExceptions); } Logger.LogDebug( string.Format( "Retry is possible. Waiting {0}ms before next attempt...", RequestRetryStrategy.DelayBeforeNextTry)); Thread.Sleep(RequestRetryStrategy.DelayBeforeNextTry); Logger.LogDebug("Trying to get response from servers once again..."); } else { retry = false; } } while (retry); Logger.LogDebug("Successfully got response."); Logger.LogTrace( string.Format("Response body: {0}", apiResponse.Body)); RequestTimeoutCalculator.OnRequestSuccess(); RequestRetryStrategy.OnRequestSuccess(); return(apiResponse); } catch (Exception e) { Logger.LogError("Failed to get response.", e); throw; } }
/// <summary> /// Retrieves specified resource from API. /// </summary> /// <param name="path">The path to the resource.</param> /// <param name="query">The query of the resource.</param> /// <returns>Response with resource result.</returns> /// <exception cref="ApiConnectionException">Could not connect to API.</exception> public IApiResponse GetResponse(string path, string query) { try { Logger.LogDebug("Getting response for path: '" + path + "' and query: '" + query + "'..."); var request = new Request { Path = path, Query = query, MainServerExceptions = new List <Exception>(), CacheServersExceptions = new List <Exception>() }; IApiResponse apiResponse; bool retry; do { if (!TryGetResponse(_connectionSettings.MainServer, request, ServerType.MainServer, out apiResponse)) { if (_connectionSettings.CacheServers != null) { foreach (var cacheServer in _connectionSettings.CacheServers) { if (TryGetResponse(cacheServer, request, ServerType.CacheServer, out apiResponse)) { break; } } } } if (apiResponse == null) { Logger.LogWarning( "Connection attempt to every server has failed. Checking whether retry is possible..."); RequestTimeoutCalculator.OnRequestFailure(); RequestRetryStrategy.OnRequestFailure(); retry = RequestRetryStrategy.ShouldRetry; if (!retry) { Logger.LogError("Retry is not possible."); throw new ApiConnectionException(request.MainServerExceptions, request.CacheServersExceptions); } Logger.LogDebug( "Retry is possible. Waiting " + RequestRetryStrategy.DelayBeforeNextTry + "ms before next attempt..."); Thread.Sleep(RequestRetryStrategy.DelayBeforeNextTry); Logger.LogDebug("Trying to get response from servers once again..."); } else { retry = false; } } while (retry); Logger.LogDebug("Successfully got response."); Logger.LogTrace("Response body: " + apiResponse.Body); RequestTimeoutCalculator.OnRequestSuccess(); RequestRetryStrategy.OnRequestSuccess(); return(apiResponse); } catch (Exception e) { Logger.LogError("Failed to get response.", e); throw; } }