From 56016e97232efd75c4c4dd9404948c480f1d77ca Mon Sep 17 00:00:00 2001 From: bluepilledgreat <97983689+bluepilledgreat@users.noreply.github.com> Date: Fri, 23 Aug 2024 18:35:22 +0100 Subject: [PATCH] use cancellation tokens for connectivity check --- Bloxstrap/RobloxDeployment.cs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Bloxstrap/RobloxDeployment.cs b/Bloxstrap/RobloxDeployment.cs index b350eea..fd33de7 100644 --- a/Bloxstrap/RobloxDeployment.cs +++ b/Bloxstrap/RobloxDeployment.cs @@ -18,24 +18,26 @@ { "https://s3.amazonaws.com/setup.roblox.com", 4 } }; - private static async Task TestConnection(string url, int priority) + private static async Task TestConnection(string url, int priority, CancellationToken token) { string LOG_IDENT = $"RobloxDeployment::TestConnection.{url}"; - await Task.Delay(priority * 1000); - - if (BaseUrl is not null) - return null; + await Task.Delay(priority * 1000, token); App.Logger.WriteLine(LOG_IDENT, "Connecting..."); try { - var response = await App.HttpClient.GetAsync($"{url}/version"); + var response = await App.HttpClient.GetAsync($"{url}/version", token); if (!response.IsSuccessStatusCode) throw new HttpResponseException(response); } + catch (TaskCanceledException) + { + App.Logger.WriteLine(LOG_IDENT, "Connectivity test cancelled."); + throw; + } catch (Exception ex) { App.Logger.WriteException(LOG_IDENT, ex); @@ -59,8 +61,11 @@ if (!String.IsNullOrEmpty(BaseUrl)) return null; + CancellationTokenSource tokenSource = new CancellationTokenSource(); + CancellationToken token = tokenSource.Token; + var exceptions = new List(); - 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..."); @@ -79,6 +84,9 @@ break; } + // stop other running connectivity tests + tokenSource.Cancel(); + if (String.IsNullOrEmpty(BaseUrl)) return exceptions[0];