Skip to content

Commit

Permalink
- Rename/add variables
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaBear84 committed Nov 5, 2023
1 parent 38f7515 commit 8955730
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/OpenDirectoryDownloader/OpenDirectoryIndexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public partial class OpenDirectoryIndexer
public Task IndexingTask { get; set; }

private bool FirstRequest { get; set; } = true;
private static bool RateLimited { get; set; }
private static bool RateLimitedOrConnectionIssues { get; set; }

private SocketsHttpHandler SocketsHttpHandler { get; set; }
private HttpClient HttpClient { get; set; }
Expand All @@ -65,6 +65,8 @@ public partial class OpenDirectoryIndexer

private GoogleDriveIndexer GoogleDriveIndexer { get; set; }

private const int FallbackRetryCount = 4;

private readonly AsyncRetryPolicy RetryPolicy = Policy
.Handle<Exception>()
.WaitAndRetryAsync(100,
Expand All @@ -90,7 +92,7 @@ public partial class OpenDirectoryIndexer
{
Program.Logger.Warning("[{thread}] Rate limited (try {retryCount}). Url '{relativeUrl}'. Waiting {waitTime:F0} seconds.", threadName, retryCount, relativeUrl, waitTime);
}
else if (retryCount <= 4 && ex is TaskCanceledException taskCanceledException && taskCanceledException.InnerException is TimeoutException timeoutException)
else if (retryCount <= FallbackRetryCount && ex is TaskCanceledException taskCanceledException && taskCanceledException.InnerException is TimeoutException timeoutException)
{
Program.Logger.Warning("[{thread}] Timeout (try {retryCount}). Url '{relativeUrl}'. Waiting {waitTime:F0} seconds.", threadName, retryCount, relativeUrl, waitTime);
}
Expand All @@ -111,19 +113,19 @@ public partial class OpenDirectoryIndexer
else if (httpRequestException.StatusCode == HttpStatusCode.ServiceUnavailable || httpRequestException.StatusCode == HttpStatusCode.TooManyRequests)
{
Program.Logger.Warning("[{thread}] HTTP {httpStatusCode}. Rate limited (try {retryCount}). Url '{relativeUrl}'. Waiting {waitTime:F0} seconds.", threadName, httpStatusCode, retryCount, relativeUrl, waitTime);
RateLimited = true;
RateLimitedOrConnectionIssues = true;
}
else if (httpRequestException.StatusCode == HttpStatusCode.LoopDetected)
{
// It could be that a Retry-After header is returned, which should be the seconds of time to wait, but this could be as high as 14.400 which is 4 hours!
// But a request will probably be successful after just a couple of seconds
Program.Logger.Warning("[{thread}] HTTP {httpStatusCode}. Rate limited / out of capacity (try {retryCount}). Url '{relativeUrl}'. Waiting {waitTime:F0} seconds.", threadName, httpStatusCode, retryCount, relativeUrl, waitTime);
RateLimited = true;
RateLimitedOrConnectionIssues = true;
}
else if (ex.Message.Contains("No connection could be made because the target machine actively refused it."))
{
Program.Logger.Warning("[{thread}] HTTP {httpStatusCode}. Rate limited? (try {retryCount}). Url '{relativeUrl}'. Waiting {waitTime:F0} seconds.", threadName, httpStatusCode, retryCount, relativeUrl, waitTime);
RateLimited = true;
RateLimitedOrConnectionIssues = true;
}
else if (!Session.GDIndex && (httpRequestException.StatusCode == HttpStatusCode.NotFound || ex.Message == "No such host is known."))
{
Expand All @@ -140,7 +142,7 @@ public partial class OpenDirectoryIndexer
Program.Logger.Warning("[{thread}] HTTP {httpStatusCode}. Error '{error}' retrieving on try {retryCount}) for '{relativeUrl}'. Skipping..", threadName, httpStatusCode, ex.Message, retryCount, relativeUrl);
(context["CancellationTokenSource"] as CancellationTokenSource).Cancel();
}
else if (retryCount <= 4)
else if (retryCount <= FallbackRetryCount)
{
Program.Logger.Warning("[{thread}] HTTP {httpStatusCode}. Error '{error}' retrieving on try {retryCount}) for '{relativeUrl}'. Waiting {waitTime:F0} seconds.", threadName, httpStatusCode, GetExceptionWithInner(ex), retryCount, relativeUrl, waitTime);
}
Expand All @@ -152,7 +154,7 @@ public partial class OpenDirectoryIndexer
}
else
{
if (retryCount <= 4)
if (retryCount <= FallbackRetryCount)
{
Program.Logger.Warning("[{thread}] Error '{error}' retrieving on try {retryCount} for url '{relativeUrl}'. Waiting {waitTime:F0} seconds.", threadName, GetExceptionWithInner(ex), retryCount, relativeUrl, waitTime);
}
Expand Down Expand Up @@ -762,7 +764,7 @@ private async Task WebDirectoryProcessor(ConcurrentQueue<WebDirectory> queue, st

do
{
if (RateLimited && RunningWebDirectoryThreads + 1 > 5)
if (RateLimitedOrConnectionIssues && RunningWebDirectoryThreads + 1 > 5)
{
Program.Logger.Warning($"Decrease threads because of rate limiting");
break;
Expand Down

0 comments on commit 8955730

Please sign in to comment.