use cancellation tokens for connectivity check

This commit is contained in:
bluepilledgreat 2024-08-23 18:35:22 +01:00
parent d15e910904
commit 56016e9723

View File

@ -18,24 +18,26 @@
{ "https://s3.amazonaws.com/setup.roblox.com", 4 } { "https://s3.amazonaws.com/setup.roblox.com", 4 }
}; };
private static async Task<string?> TestConnection(string url, int priority) private static async Task<string?> TestConnection(string url, int priority, CancellationToken token)
{ {
string LOG_IDENT = $"RobloxDeployment::TestConnection.{url}"; string LOG_IDENT = $"RobloxDeployment::TestConnection.{url}";
await Task.Delay(priority * 1000); await Task.Delay(priority * 1000, token);
if (BaseUrl is not null)
return null;
App.Logger.WriteLine(LOG_IDENT, "Connecting..."); App.Logger.WriteLine(LOG_IDENT, "Connecting...");
try try
{ {
var response = await App.HttpClient.GetAsync($"{url}/version"); var response = await App.HttpClient.GetAsync($"{url}/version", token);
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
throw new HttpResponseException(response); throw new HttpResponseException(response);
} }
catch (TaskCanceledException)
{
App.Logger.WriteLine(LOG_IDENT, "Connectivity test cancelled.");
throw;
}
catch (Exception ex) catch (Exception ex)
{ {
App.Logger.WriteException(LOG_IDENT, ex); App.Logger.WriteException(LOG_IDENT, ex);
@ -59,8 +61,11 @@
if (!String.IsNullOrEmpty(BaseUrl)) if (!String.IsNullOrEmpty(BaseUrl))
return null; return null;
CancellationTokenSource tokenSource = new CancellationTokenSource();
CancellationToken token = tokenSource.Token;
var exceptions = new List<Exception>(); var exceptions = new List<Exception>();
var tasks = (from entry in BaseUrls select TestConnection(entry.Key, entry.Value)).ToList(); var tasks = (from entry in BaseUrls select TestConnection(entry.Key, entry.Value, token)).ToList();
App.Logger.WriteLine(LOG_IDENT, "Testing connectivity..."); App.Logger.WriteLine(LOG_IDENT, "Testing connectivity...");
@ -79,6 +84,9 @@
break; break;
} }
// stop other running connectivity tests
tokenSource.Cancel();
if (String.IsNullOrEmpty(BaseUrl)) if (String.IsNullOrEmpty(BaseUrl))
return exceptions[0]; return exceptions[0];