diff options
Diffstat (limited to '')
-rw-r--r-- | LibMatrix/Services/HomeserverProviderService.cs | 11 | ||||
-rw-r--r-- | LibMatrix/Services/HomeserverResolverService.cs | 4 |
2 files changed, 7 insertions, 8 deletions
diff --git a/LibMatrix/Services/HomeserverProviderService.cs b/LibMatrix/Services/HomeserverProviderService.cs index 666d2a2..1f3bd37 100644 --- a/LibMatrix/Services/HomeserverProviderService.cs +++ b/LibMatrix/Services/HomeserverProviderService.cs @@ -35,20 +35,15 @@ public class HomeserverProviderService { } var domain = proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver); - var hc = new MatrixHttpClient { BaseAddress = new Uri(domain) }; AuthenticatedHomeserverGeneric hs; if (true) { - hs = new AuthenticatedHomeserverMxApiExtended(homeserver, accessToken); + hs = await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverMxApiExtended>(homeserver, accessToken); } else { - hs = new AuthenticatedHomeserverGeneric(homeserver, accessToken); + hs = await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverSynapse>(homeserver, accessToken); } - hs._httpClient = hc; - hs._httpClient.Timeout = TimeSpan.FromMinutes(15); - hs._httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); - // (() => hs.WhoAmI) = (await hs._httpClient.GetFromJsonAsync<WhoAmIResponse>("/_matrix/client/v3/account/whoami"))!; lock(_authenticatedHomeServerCache) @@ -59,7 +54,7 @@ public class HomeserverProviderService { } public async Task<RemoteHomeServer> GetRemoteHomeserver(string homeserver, string? proxy = null) { - var hs = new RemoteHomeServer(proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver)); + var hs = await RemoteHomeServer.Create(proxy ?? await _homeserverResolverService.ResolveHomeserverFromWellKnown(homeserver)); // hs._httpClient.Dispose(); // hs._httpClient = new MatrixHttpClient { BaseAddress = new Uri(hs.FullHomeServerDomain) }; // hs._httpClient.Timeout = TimeSpan.FromSeconds(120); diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index 685724b..75545db 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -12,6 +12,9 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge private static readonly Dictionary<string, SemaphoreSlim> _wellKnownSemaphores = new(); public async Task<string> ResolveHomeserverFromWellKnown(string homeserver) { + if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); + if(_wellKnownCache.TryGetValue(homeserver, out var known)) return known; + logger?.LogInformation("Resolving homeserver: {}", homeserver); var res = await _resolveHomeserverFromWellKnown(homeserver); if (!res.StartsWith("http")) res = "https://" + res; if (res.EndsWith(":443")) res = res[..^4]; @@ -21,6 +24,7 @@ public class HomeserverResolverService(ILogger<HomeserverResolverService>? logge private async Task<string> _resolveHomeserverFromWellKnown(string homeserver) { if (homeserver is null) throw new ArgumentNullException(nameof(homeserver)); var sem = _wellKnownSemaphores.GetOrCreate(homeserver, _ => new SemaphoreSlim(1, 1)); + if(_wellKnownCache.TryGetValue(homeserver, out var wellKnown)) return wellKnown; await sem.WaitAsync(); if (_wellKnownCache.TryGetValue(homeserver, out var known)) { sem.Release(); |