From caf6d36bcdffdb5bf895e068a3797b3a6d4d34df Mon Sep 17 00:00:00 2001 From: Jkorf Date: Thu, 23 Jul 2026 13:16:59 +0200 Subject: [PATCH] Extracted ConnectionCanBeUsedFor method in SocketApiClient for easier custom logic implementation --- CryptoExchange.Net/Clients/SocketApiClient.cs | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/CryptoExchange.Net/Clients/SocketApiClient.cs b/CryptoExchange.Net/Clients/SocketApiClient.cs index aebfc00e..7d89f80f 100644 --- a/CryptoExchange.Net/Clients/SocketApiClient.cs +++ b/CryptoExchange.Net/Clients/SocketApiClient.cs @@ -625,6 +625,21 @@ namespace CryptoExchange.Net.Clients return Task.FromResult(CallResult.Ok()); } + /// + /// Whether the connection can be used for a new subscription or query with the provided parameters + /// + /// The connection to check + /// The address set by the request + /// Whether the request needs an authenticated connection + /// Topic of the request + /// True if connection can be used + protected virtual bool ConnectionCanBeUsedFor(SocketConnection connection, string address, bool authenticated, string? topic = null) + { + return connection.ConnectionUriString.Equals(address.TrimEnd('/'), StringComparison.Ordinal) + && connection.ApiClient.ClientName.Equals(ClientName, StringComparison.Ordinal) + && (AllowTopicsOnTheSameConnection || !connection.Topics.Contains(topic)); + } + /// /// Gets a connection for a new subscription or query. Can be an existing if there are open position or a new one. /// @@ -643,10 +658,7 @@ namespace CryptoExchange.Net.Clients string? topic = null, int individualSubscriptionCount = 1) { - var socketQuery = _socketConnections.Where(s => s.Value.ConnectionUriString.Equals(address.TrimEnd('/'), StringComparison.Ordinal) - && s.Value.ApiClient.ClientName.Equals(ClientName, StringComparison.Ordinal) - && (AllowTopicsOnTheSameConnection || !s.Value.Topics.Contains(topic))) - .Select(x => x.Value); // Don't ToList this so the query is executed again when called + var socketQuery = _socketConnections.Where(s => ConnectionCanBeUsedFor(s.Value, address, authenticated, topic)).Select(x => x.Value); // Don't ToList this so the query is executed again when called // If all current socket connections are reconnecting or resubscribing wait for that to finish as we can probably use the existing connection var delayStart = DateTime.UtcNow;