Skip to content

Commit

Permalink
Merge pull request #53 from /issues/51-RetryRequestNoCache
Browse files Browse the repository at this point in the history
  • Loading branch information
tpill90 authored May 3, 2024
2 parents 980e0aa + 73b596a commit df28d77
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions EpicPrefill/Handlers/DownloadHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ await _ansiConsole.CreateSpectreProgress(TransferSpeedUnit.Bits).StartAsync(asyn
failedRequests = await AttemptDownloadAsync(ctx, "Downloading..", queuedRequests, new Uri(allManifestUrls.First().ManifestDownloadUrl));

// Handle any failed requests
while (failedRequests.Any() && retryCount < 3)
while (failedRequests.Any() && retryCount < 2)
{
retryCount++;
await Task.Delay(2000 * retryCount);
failedRequests = await AttemptDownloadAsync(ctx, $"Retrying {retryCount}..", failedRequests.ToList(), new Uri(allManifestUrls.First().ManifestDownloadUrl));
var upstreamCdn = new Uri(allManifestUrls.First().ManifestDownloadUrl);
failedRequests = await AttemptDownloadAsync(ctx, $"Retrying {retryCount}..", failedRequests.ToList(), upstreamCdn, forceRecache: true);
}
});

Expand All @@ -70,13 +71,15 @@ await _ansiConsole.CreateSpectreProgress(TransferSpeedUnit.Bits).StartAsync(asyn
return false;
}


//TODO I don't like the number of parameters here, should maybe rethink the way this is written.
/// <summary>
/// Attempts to download the specified requests. Returns a list of any requests that have failed.
/// Attempts to download the specified requests. Returns a list of any requests that have failed for any reason.
/// </summary>
/// <param name="forceRecache">When specified, will cause the cache to delete the existing cached data for a request, and redownload it again.</param>
/// <returns>A list of failed requests</returns>
[SuppressMessage("Reliability", "CA2016:Forward the 'CancellationToken' parameter to methods", Justification = "Don't have a need to cancel")]
private async Task<ConcurrentBag<QueuedRequest>> AttemptDownloadAsync(ProgressContext ctx, string taskTitle, List<QueuedRequest> requestsToDownload, Uri upstreamCdn)
private async Task<ConcurrentBag<QueuedRequest>> AttemptDownloadAsync(ProgressContext ctx, string taskTitle, List<QueuedRequest> requestsToDownload,
Uri upstreamCdn, bool forceRecache = false)
{
double requestTotalSize = requestsToDownload.Sum(e => (long)e.DownloadSizeBytes);
var progressTask = ctx.AddTask(taskTitle, new ProgressTaskSettings { MaxValue = requestTotalSize });
Expand All @@ -89,6 +92,10 @@ private async Task<ConcurrentBag<QueuedRequest>> AttemptDownloadAsync(ProgressCo
try
{
var url = Path.Join($"http://{_lancacheAddress}", chunk.DownloadUrl);
if (forceRecache)
{
url += "?nocache=1";
}

using var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
requestMessage.Headers.Host = upstreamCdn.Host;
Expand All @@ -102,9 +109,10 @@ private async Task<ConcurrentBag<QueuedRequest>> AttemptDownloadAsync(ProgressCo
{
}
}
catch
catch (Exception e)
{
failedRequests.Add(chunk);
FileLogger.LogExceptionNoStackTrace($"Request {chunk.DownloadUrl}", e);
}
progressTask.Increment(chunk.DownloadSizeBytes);
});
Expand Down

0 comments on commit df28d77

Please sign in to comment.