From e25666bbf408ff33c09dc8e2c5fe2d052363595f Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 29 Nov 2023 00:17:13 -0500 Subject: Integrate FBM core changes for immutable client instances --- .../src/FBMDataCacheExtensions.cs | 42 ++++++++++++++-------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs') diff --git a/lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs b/lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs index 708b3f5..bd86461 100644 --- a/lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs +++ b/lib/VNLib.Data.Caching.Extensions/src/FBMDataCacheExtensions.cs @@ -88,7 +88,7 @@ namespace VNLib.Data.Caching.Extensions return adapter; } - private static readonly ConditionalWeakTable ClientCacheConfig = new(); + private static readonly ConditionalWeakTable ClientCacheConfig = new(); /// /// Gets a preconfigured object caching @@ -358,7 +358,7 @@ namespace VNLib.Data.Caching.Extensions /// /// /// A fluent api configuration builder for the current client - public static CacheClientConfiguration GetCacheConfiguration(this FBMClient client) => ClientCacheConfig.GetOrCreateValue(client); + public static CacheClientConfiguration GetCacheConfiguration(this FBMClientFactory client) => ClientCacheConfig.GetOrCreateValue(client); /// /// Explicitly set the client cache configuration for the current client @@ -366,7 +366,7 @@ namespace VNLib.Data.Caching.Extensions /// /// The cache node configuration /// The config instance - public static CacheClientConfiguration SetCacheConfiguration(this FBMClient client, CacheClientConfiguration config) + public static CacheClientConfiguration SetCacheConfiguration(this FBMClientFactory client, CacheClientConfiguration config) { ClientCacheConfig.AddOrUpdate(client, config); return config; @@ -378,7 +378,7 @@ namespace VNLib.Data.Caching.Extensions /// /// The cache node configuration /// The config instance - public static CacheNodeConfiguration SetCacheConfiguration(this FBMClient client, CacheNodeConfiguration nodeConfig) + public static CacheNodeConfiguration SetCacheConfiguration(this FBMClientFactory client, CacheNodeConfiguration nodeConfig) { ClientCacheConfig.AddOrUpdate(client, nodeConfig); return nodeConfig; @@ -425,7 +425,7 @@ namespace VNLib.Data.Caching.Extensions /// /// A token to cancel the operation /// A task that completes when all nodes have been discovered - public static Task DiscoverAvailableNodesAsync(this FBMClient client, CancellationToken cancellation = default) + public static Task DiscoverAvailableNodesAsync(this FBMClientFactory client, CancellationToken cancellation = default) { //Get stored config CacheClientConfiguration conf = ClientCacheConfig.GetOrCreateValue(client); @@ -446,7 +446,7 @@ namespace VNLib.Data.Caching.Extensions /// /// /// - public static async Task ConnectToRandomCacheAsync(this FBMClient client, CancellationToken cancellation = default) + public static async Task ConnectToRandomCacheAsync(this FBMClientFactory client, CancellationToken cancellation = default) { //Get stored config CacheClientConfiguration conf = ClientCacheConfig.GetOrCreateValue(client); @@ -467,7 +467,7 @@ namespace VNLib.Data.Caching.Extensions /// /// Connects to the specified server on the configured cache client /// - /// + /// /// The server to connect to /// A token to cancel the operation /// A task that resolves when the client is connected to the cache server @@ -477,16 +477,28 @@ namespace VNLib.Data.Caching.Extensions /// /// /// - public static Task ConnectToCacheAsync(this FBMClient client, CacheNodeAdvertisment server, CancellationToken token = default) + public static async Task ConnectToCacheAsync(this FBMClientFactory factory, CacheNodeAdvertisment server, CancellationToken token = default) { - _ = client ?? throw new ArgumentNullException(nameof(client)); + _ = factory ?? throw new ArgumentNullException(nameof(factory)); _ = server ?? throw new ArgumentNullException(nameof(server)); //Get stored config - CacheClientConfiguration conf = ClientCacheConfig.GetOrCreateValue(client); + CacheClientConfiguration conf = ClientCacheConfig.GetOrCreateValue(factory); - //Connect to server (no server id because client not replication server) - return ConnectToCacheAsync(client, conf, server, token); + //Create new client + FBMClient client = factory.CreateClient(); + + try + { + //Connect to server (no server id because client not replication server) + await ConnectToCacheAsync(client, conf, server, token); + return client; + } + catch + { + client.Dispose(); + throw; + } } /// @@ -549,19 +561,19 @@ namespace VNLib.Data.Caching.Extensions client.LogDebug("Server negotiation validated, connecting to server"); //The client authorization header is the exact response - client.ClientSocket.Headers[HttpRequestHeader.Authorization] = response.Content!; + client.Headers[HttpRequestHeader.Authorization] = response.Content!; //See if the supplied config is for a cache node CacheNodeConfiguration? cnc = config as CacheNodeConfiguration; //Compute the signature of the upgrade token - client.ClientSocket.Headers[X_UPGRADE_SIG_HEADER] = config.AuthManager.GetBase64UpgradeSignature(response.Content, cnc != null); + client.Headers[X_UPGRADE_SIG_HEADER] = config.AuthManager.GetBase64UpgradeSignature(response.Content, cnc != null); //Check to see if adversize self is enabled if (cnc?.BroadcastAdverisment == true) { //Set advertisment header - client.ClientSocket.Headers[X_NODE_DISCOVERY_HEADER] = GetAdvertismentHeader(cnc); + client.Headers[X_NODE_DISCOVERY_HEADER] = GetAdvertismentHeader(cnc); } //build ws uri from the connect endpoint -- cgit