Skip to content

Commit

Permalink
Add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed Mar 2, 2024
1 parent 113c3bc commit 3cf4b43
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Server/ReplayParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public static async Task ConsumeQueue(CancellationToken token)
// Consume the queue.
while (Queue.Count > 0)
{
var timeoutToken = new CancellationTokenSource(10000);
var tokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, timeoutToken.Token);
var startTime = DateTime.Now;

// Since replays are like 200mb long, we want to parrallelize this.
var tasks = new List<Task>();
for (var i = 0; i < 10; i++)
Expand Down Expand Up @@ -114,10 +118,19 @@ public static async Task ConsumeQueue(CancellationToken token)
{
Log.Error(e, "Error while parsing " + replay);
}
}, token));

// Wait for all tasks to finish.
await Task.WhenAll(tasks);
}, tokenSource.Token));
}

// If the download takes too long, cancel it.
// 10 minutes should be enough
await Task.WhenAny(Task.WhenAll(tasks), Task.Delay(600000, token));
await timeoutToken.CancelAsync();
// Cancel the timeout token, so the background tasks cancel as well.

// If we timed out, log a warning.
if (DateTime.Now - startTime > TimeSpan.FromMinutes(10))
{
Log.Warning("Parsing took too long for " + string.Join(", ", tasks.Select(x => x.Id)));
}
}

Expand Down Expand Up @@ -213,7 +226,11 @@ private static async Task RetrieveFilesRecursive(string directoryUrl, Cancellati
continue;
}
Log.Information("Adding " + href + " to the queue.");
Queue.Add(href);
// Check if it's already in the queue.
if (!Queue.Contains(href))
{
Queue.Add(href);
}
}
}
}
Expand Down

0 comments on commit 3cf4b43

Please sign in to comment.