From 5affd9f061e75f6575a2fe6715f9e8757cfe87e8 Mon Sep 17 00:00:00 2001 From: "Emma [it/its]@Rory&" Date: Thu, 14 Dec 2023 07:20:46 +0100 Subject: Cleanup --- LibMatrix/Services/HomeserverProviderService.cs | 47 ++++++++++++++++--------- LibMatrix/Services/HomeserverResolverService.cs | 25 +++++++------ 2 files changed, 45 insertions(+), 27 deletions(-) (limited to 'LibMatrix/Services') diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index a42077a..983f469 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -6,20 +6,20 @@ using Microsoft.Extensions.Logging; namespace LibMatrix.Services; -public class HomeserverProviderService(ILogger logger, HomeserverResolverService homeserverResolverService) { - private static Dictionary _authenticatedHomeserverSemaphore = new(); - private static Dictionary _authenticatedHomeserverCache = new(); +public class HomeserverProviderService(ILogger logger) { + private static readonly Dictionary AuthenticatedHomeserverSemaphore = new(); + private static readonly Dictionary AuthenticatedHomeserverCache = new(); - private static Dictionary _remoteHomeserverSemaphore = new(); - private static Dictionary _remoteHomeserverCache = new(); + private static readonly Dictionary RemoteHomeserverSemaphore = new(); + private static readonly Dictionary RemoteHomeserverCache = new(); public async Task GetAuthenticatedWithToken(string homeserver, string accessToken, string? proxy = null) { var cacheKey = homeserver + accessToken + proxy; - var sem = _authenticatedHomeserverSemaphore.GetOrCreate(cacheKey, _ => new SemaphoreSlim(1, 1)); + var sem = AuthenticatedHomeserverSemaphore.GetOrCreate(cacheKey, _ => new SemaphoreSlim(1, 1)); await sem.WaitAsync(); AuthenticatedHomeserverGeneric? hs; - lock (_authenticatedHomeserverCache) { - if (_authenticatedHomeserverCache.TryGetValue(cacheKey, out hs)) { + lock (AuthenticatedHomeserverCache) { + if (AuthenticatedHomeserverCache.TryGetValue(cacheKey, out hs)) { sem.Release(); return hs; } @@ -30,8 +30,8 @@ public class HomeserverProviderService(ILogger logger var rhs = await RemoteHomeserver.Create(homeserver, proxy); var clientVersions = await rhs.GetClientVersionsAsync(); if (proxy is not null) - Console.WriteLine($"Homeserver {homeserver} proxied via {proxy}..."); - Console.WriteLine($"{homeserver}: " + clientVersions.ToJson()); + logger.LogInformation($"Homeserver {homeserver} proxied via {proxy}..."); + logger.LogInformation($"{homeserver}: " + clientVersions.ToJson()); if (clientVersions.UnstableFeatures.TryGetValue("gay.rory.mxapiextensions.v0", out bool a) && a) hs = await AuthenticatedHomeserverGeneric.Create(homeserver, accessToken, proxy); @@ -43,18 +43,31 @@ public class HomeserverProviderService(ILogger logger hs = await AuthenticatedHomeserverGeneric.Create(homeserver, accessToken, proxy); } - lock (_authenticatedHomeserverCache) - _authenticatedHomeserverCache[cacheKey] = hs; + lock (AuthenticatedHomeserverCache) + AuthenticatedHomeserverCache[cacheKey] = hs; sem.Release(); return hs; } public async Task GetRemoteHomeserver(string homeserver, string? proxy = null) { - var hs = await RemoteHomeserver.Create(homeserver, proxy); - // hs._httpClient.Dispose(); - // hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.ServerName) }; - // hs._httpClient.Timeout = TimeSpan.FromSeconds(120); + var cacheKey = homeserver + proxy; + var sem = RemoteHomeserverSemaphore.GetOrCreate(cacheKey, _ => new SemaphoreSlim(1, 1)); + await sem.WaitAsync(); + RemoteHomeserver? hs; + lock (RemoteHomeserverCache) { + if (RemoteHomeserverCache.TryGetValue(cacheKey, out hs)) { + sem.Release(); + return hs; + } + } + + hs = await RemoteHomeserver.Create(homeserver, proxy); + + lock (RemoteHomeserverCache) + RemoteHomeserverCache[cacheKey] = hs; + sem.Release(); + return hs; } @@ -68,4 +81,4 @@ public class HomeserverProviderService(ILogger logger var data = await resp.Content.ReadFromJsonAsync(); return data!; } -} +} \ No newline at end of file diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index f9f92d6..9f937c5 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -11,15 +11,15 @@ public class HomeserverResolverService(ILogger? logge Timeout = TimeSpan.FromMilliseconds(10000) }; - private static readonly ConcurrentDictionary _wellKnownCache = new(); - private static readonly ConcurrentDictionary _wellKnownSemaphores = new(); + private static readonly ConcurrentDictionary WellKnownCache = new(); + private static readonly ConcurrentDictionary WellKnownSemaphores = new(); public async Task ResolveHomeserverFromWellKnown(string homeserver) { if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); - _wellKnownSemaphores.TryAdd(homeserver, new(1, 1)); - await _wellKnownSemaphores[homeserver].WaitAsync(); - if (_wellKnownCache.TryGetValue(homeserver, out var known)) { - _wellKnownSemaphores[homeserver].Release(); + WellKnownSemaphores.TryAdd(homeserver, new(1, 1)); + await WellKnownSemaphores[homeserver].WaitAsync(); + if (WellKnownCache.TryGetValue(homeserver, out var known)) { + WellKnownSemaphores[homeserver].Release(); return known; } @@ -28,8 +28,8 @@ public class HomeserverResolverService(ILogger? logge Client = await _tryResolveFromClientWellknown(homeserver), Server = await _tryResolveFromServerWellknown(homeserver) }; - _wellKnownCache.TryAdd(homeserver, res); - _wellKnownSemaphores[homeserver].Release(); + WellKnownCache.TryAdd(homeserver, res); + WellKnownSemaphores[homeserver].Release(); return res; } @@ -40,7 +40,9 @@ public class HomeserverResolverService(ILogger? logge var hs = resp.GetProperty("m.homeserver").GetProperty("base_url").GetString(); return hs; } - catch { } + catch { + // ignored + } logger?.LogInformation("No client well-known..."); return null; @@ -51,11 +53,14 @@ public class HomeserverResolverService(ILogger? logge try { var resp = await _httpClient.GetFromJsonAsync($"{homeserver}/.well-known/matrix/server"); var hs = resp.GetProperty("m.server").GetString(); + if(hs is null) throw new InvalidDataException("m.server is null"); if (!hs.StartsWithAnyOf("http://", "https://")) hs = $"https://{hs}"; return hs; } - catch { } + catch { + // ignored + } // fallback: most servers host these on the same location var clientUrl = await _tryResolveFromClientWellknown(homeserver); -- cgit 1.4.1