about summary refs log tree commit diff
path: root/LibMatrix/Services/HomeserverProviderService.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--LibMatrix/Services/HomeserverProviderService.cs47
1 files changed, 30 insertions, 17 deletions
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<HomeserverProviderService> logger, HomeserverResolverService homeserverResolverService) {
-    private static Dictionary<string, SemaphoreSlim> _authenticatedHomeserverSemaphore = new();
-    private static Dictionary<string, AuthenticatedHomeserverGeneric> _authenticatedHomeserverCache = new();
+public class HomeserverProviderService(ILogger<HomeserverProviderService> logger) {
+    private static readonly Dictionary<string, SemaphoreSlim> AuthenticatedHomeserverSemaphore = new();
+    private static readonly Dictionary<string, AuthenticatedHomeserverGeneric> AuthenticatedHomeserverCache = new();
 
-    private static Dictionary<string, SemaphoreSlim> _remoteHomeserverSemaphore = new();
-    private static Dictionary<string, RemoteHomeserver> _remoteHomeserverCache = new();
+    private static readonly Dictionary<string, SemaphoreSlim> RemoteHomeserverSemaphore = new();
+    private static readonly Dictionary<string, RemoteHomeserver> RemoteHomeserverCache = new();
 
     public async Task<AuthenticatedHomeserverGeneric> 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<HomeserverProviderService> 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<AuthenticatedHomeserverMxApiExtended>(homeserver, accessToken, proxy);
@@ -43,18 +43,31 @@ public class HomeserverProviderService(ILogger<HomeserverProviderService> logger
                 hs = await AuthenticatedHomeserverGeneric.Create<AuthenticatedHomeserverGeneric>(homeserver, accessToken, proxy);
         }
 
-        lock (_authenticatedHomeserverCache)
-            _authenticatedHomeserverCache[cacheKey] = hs;
+        lock (AuthenticatedHomeserverCache)
+            AuthenticatedHomeserverCache[cacheKey] = hs;
         sem.Release();
 
         return hs;
     }
 
     public async Task<RemoteHomeserver> 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<HomeserverProviderService> logger
         var data = await resp.Content.ReadFromJsonAsync<LoginResponse>();
         return data!;
     }
-}
+}
\ No newline at end of file