From a680417d93e042b8c3f74482787b87c641ead987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rnar=20=C3=98sttveit?= Date: Mon, 3 Jun 2024 12:34:20 +0200 Subject: [PATCH] workaround for podman host-gateway timeout when selecting frontend version --- .../Implementation/LocalFrontendService.cs | 51 +++++++++++-------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/Services/LocalFrontend/Implementation/LocalFrontendService.cs b/src/Services/LocalFrontend/Implementation/LocalFrontendService.cs index 2570b081..cbc10964 100644 --- a/src/Services/LocalFrontend/Implementation/LocalFrontendService.cs +++ b/src/Services/LocalFrontend/Implementation/LocalFrontendService.cs @@ -3,45 +3,52 @@ using LocalTest.Models; using LocalTest.Services.LocalFrontend.Interface; using Microsoft.Extensions.Options; +using static System.Linq.Enumerable; namespace LocalTest.Services.LocalFrontend; -public class LocalFrontendService: ILocalFrontendService +public class LocalFrontendService : ILocalFrontendService { private readonly HttpClient _httpClient; private static readonly Range PortRange = 8080..8090; private readonly string _localFrontedBaseUrl; - public LocalFrontendService(IHttpClientFactory httpClientFactory, IOptions localPlatformSettings) + public LocalFrontendService( + IHttpClientFactory httpClientFactory, + IOptions localPlatformSettings + ) { _httpClient = httpClientFactory.CreateClient(); - _localFrontedBaseUrl = $"{localPlatformSettings.Value.LocalFrontendProtocol}://{localPlatformSettings.Value.LocalFrontendHostname}"; + _httpClient.Timeout = TimeSpan.FromMilliseconds(500); + _localFrontedBaseUrl = + $"{localPlatformSettings.Value.LocalFrontendProtocol}://{localPlatformSettings.Value.LocalFrontendHostname}"; } public async Task> GetLocalFrontendDevPorts() { - var ports = new List(); - for (int i = PortRange.Start.Value; i < PortRange.End.Value; i++) + var tasks = Range(PortRange.Start.Value, PortRange.End.Value).Select(port => TestFrontendDevPort(port)); + var result = await Task.WhenAll(tasks); + return result.Where(x => x != null).Select(x => x!.Value).ToList(); + } + + private async Task TestFrontendDevPort(int port) + { + try { - try + var response = await _httpClient.GetAsync($"{_localFrontedBaseUrl}:{port.ToString()}/"); + if (response.Headers.TryGetValues("X-Altinn-Frontend-Branch", out var values)) { - var response = - await _httpClient.GetAsync($"{_localFrontedBaseUrl}:{i.ToString()}/"); - if (response.Headers.TryGetValues("X-Altinn-Frontend-Branch", out var values)) + return new LocalFrontendInfo() { - - ports.Add(new LocalFrontendInfo() - { - Port = i.ToString(), - Branch = values?.First() ?? "Unknown" - }); - } - } - catch(HttpRequestException) - { - + Port = port.ToString(), + Branch = values?.First() ?? "Unknown" + }; } } - return ports; + catch (TaskCanceledException) { } + catch (HttpRequestException) { } + + return null; } -} \ No newline at end of file +} +