From 11c7786ea23d82d31cc54abe57d35fdd74cf1bd5 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 13 May 2024 23:54:10 +0200 Subject: Clearer error messages, fix bug in hs resolution --- LibMatrix/Services/HomeserverResolverService.cs | 29 +++++++++++++++---------- 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'LibMatrix/Services/HomeserverResolverService.cs') diff --git a/LibMatrix/Services/HomeserverResolverService.cs b/LibMatrix/Services/HomeserverResolverService.cs index 42ad0a1..05ce733 100644 --- a/LibMatrix/Services/HomeserverResolverService.cs +++ b/LibMatrix/Services/HomeserverResolverService.cs @@ -14,7 +14,7 @@ namespace LibMatrix.Services; public class HomeserverResolverService { private readonly MatrixHttpClient _httpClient = new() { - Timeout = TimeSpan.FromMilliseconds(10000) + Timeout = TimeSpan.FromSeconds(60) }; private static readonly SemaphoreCache WellKnownCache = new(); @@ -30,40 +30,45 @@ public class HomeserverResolverService { } } - private static SemaphoreSlim _wellKnownSemaphore = new(1, 1); + // private static SemaphoreSlim _wellKnownSemaphore = new(1, 1); - public async Task ResolveHomeserverFromWellKnown(string homeserver) { + public async Task ResolveHomeserverFromWellKnown(string homeserver, bool enableClient = true, bool enableServer = true) { ArgumentNullException.ThrowIfNull(homeserver); return await WellKnownCache.GetOrAdd(homeserver, async () => { - await _wellKnownSemaphore.WaitAsync(); + // await _wellKnownSemaphore.WaitAsync(); _logger.LogTrace($"Resolving homeserver well-knowns: {homeserver}"); - var client = _tryResolveClientEndpoint(homeserver); + var client = enableClient ? _tryResolveClientEndpoint(homeserver) : null; + var server = enableServer ? _tryResolveServerEndpoint(homeserver) : null; var res = new WellKnownUris(); // try { - res.Client = await client ?? throw new Exception("Could not resolve client URL."); + if (client != null) + res.Client = await client ?? throw new Exception($"Could not resolve client URL for {homeserver}."); // } // catch (Exception e) { // _logger.LogError(e, "Error resolving client well-known for {hs}", homeserver); // } - var server = _tryResolveServerEndpoint(homeserver); - // try { - res.Server = await server ?? throw new Exception("Could not resolve server URL."); + if (server != null) + res.Server = await server ?? throw new Exception($"Could not resolve server URL for {homeserver}."); // } // catch (Exception e) { // _logger.LogError(e, "Error resolving server well-known for {hs}", homeserver); // } _logger.LogInformation("Resolved well-knowns for {hs}: {json}", homeserver, res.ToJson(indent: false)); - _wellKnownSemaphore.Release(); + // _wellKnownSemaphore.Release(); return res; }); } + // private async Task InternalResolveHomeserverFromWellKnown(string homeserver) { + + // } + private async Task _tryResolveClientEndpoint(string homeserver) { ArgumentNullException.ThrowIfNull(homeserver); _logger.LogTrace("Resolving client well-known: {homeserver}", homeserver); @@ -90,7 +95,7 @@ public class HomeserverResolverService { if (!string.IsNullOrWhiteSpace(clientWellKnown?.Homeserver.BaseUrl)) return clientWellKnown.Homeserver.BaseUrl; - _logger.LogInformation("No client well-known..."); + _logger.LogInformation("No client well-known for {server}...", homeserver); return null; } @@ -129,7 +134,7 @@ public class HomeserverResolverService { if (clientUrl is not null && await _httpClient.CheckSuccessStatus($"{clientUrl}/_matrix/federation/v1/version")) return clientUrl; - _logger.LogInformation("No server well-known..."); + _logger.LogInformation("No server well-known for {server}...", homeserver); return null; } -- cgit 1.4.1