Skip to content

Commit

Permalink
Stop retrying on 404s
Browse files Browse the repository at this point in the history
  • Loading branch information
hallipr committed May 21, 2024
1 parent 0febd98 commit dc306c5
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ private static string GetUrlContents(string url)
using HttpClient client = new HttpClient();
while (attempts <= maxRetries)
{
HttpResponseMessage response = null;
try
{
HttpResponseMessage response = client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
response = client.GetAsync(url).ConfigureAwait(false).GetAwaiter().GetResult();
if (response.StatusCode == HttpStatusCode.OK)
{
// This writeline is probably unnecessary but good to have if there are previous attempts that failed
Expand All @@ -76,13 +77,19 @@ private static string GetUrlContents(string url)
// HttpRequestException means the request failed due to an underlying issue such as network connectivity,
// DNS failure, server certificate validation or timeout.
Console.WriteLine($"GetUrlContents attempt number {attempts}. HttpRequestException trying to fetch {url}. Exception message = {httpReqEx.Message}");
if (attempts == maxRetries)
{
// At this point the retries have been exhausted, let this rethrow
throw;
}

}

// Skip retries on a NotFound response
if (response?.StatusCode == HttpStatusCode.NotFound)
{
return null;
}

if (attempts < maxRetries)
{
System.Threading.Thread.Sleep(delayTimeInMs);
}
System.Threading.Thread.Sleep(delayTimeInMs);
attempts++;
}
// This will only get hit if the final retry is non-OK status code
Expand Down

0 comments on commit dc306c5

Please sign in to comment.