diff options
author | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-30 03:36:58 +0200 |
---|---|---|
committer | TheArcaneBrony <myrainbowdash949@gmail.com> | 2023-06-30 03:36:58 +0200 |
commit | bb8c2637af3b7982e7a4b2fd15e2fbec613d0848 (patch) | |
tree | b8075ba7e507aad3f96f354712ad920ac421e474 /MatrixRoomUtils.Core/Services/HomeserverResolverService.cs | |
parent | Update stuff (diff) | |
download | MatrixUtils-bb8c2637af3b7982e7a4b2fd15e2fbec613d0848.tar.xz |
Todays progress
Diffstat (limited to 'MatrixRoomUtils.Core/Services/HomeserverResolverService.cs')
-rw-r--r-- | MatrixRoomUtils.Core/Services/HomeserverResolverService.cs | 14 |
1 files changed, 11 insertions, 3 deletions
diff --git a/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs b/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs index f6363ab..526a261 100644 --- a/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs +++ b/MatrixRoomUtils.Core/Services/HomeserverResolverService.cs @@ -6,11 +6,12 @@ using Microsoft.Extensions.Logging; namespace MatrixRoomUtils.Core.Services; public class HomeserverResolverService { - private readonly HttpClient _httpClient; + private readonly MatrixHttpClient _httpClient = new MatrixHttpClient(); private readonly ILogger<HomeserverResolverService> _logger; - public HomeserverResolverService(HttpClient httpClient, ILogger<HomeserverResolverService> logger) { - _httpClient = httpClient; + private static Dictionary<string, object> _wellKnownCache = new(); + + public HomeserverResolverService(ILogger<HomeserverResolverService> logger) { _logger = logger; } @@ -22,6 +23,12 @@ public class HomeserverResolverService { } private async Task<string> _resolveHomeserverFromWellKnown(string homeserver) { + if(homeserver is null) throw new ArgumentNullException(nameof(homeserver)); + if (_wellKnownCache.ContainsKey(homeserver)) { + if (_wellKnownCache[homeserver] is SemaphoreSlim s) await s.WaitAsync(); + if (_wellKnownCache[homeserver] is string p) return p; + } + _wellKnownCache[homeserver] = new SemaphoreSlim(1); string? result = null; _logger.LogInformation($"Attempting to resolve homeserver: {homeserver}"); if (!homeserver.StartsWith("http")) homeserver = "https://" + homeserver; @@ -34,6 +41,7 @@ public class HomeserverResolverService { if(result is not null) { _logger.LogInformation($"Resolved homeserver: {homeserver} -> {result}"); + _wellKnownCache.TryAdd(homeserver, result); return result; } |