From f9957cba1619396a8be530c23c1c77d315c503c0 Mon Sep 17 00:00:00 2001 From: Jkorf Date: Mon, 20 Sep 2021 11:19:42 +0200 Subject: [PATCH] Prevent reconnect spamming when invalid checksum --- CryptoExchange.Net/CryptoExchange.Net.xml | 17 +++++++++++++++-- CryptoExchange.Net/OrderBook/SymbolOrderBook.cs | 14 ++++++++++---- .../Sockets/SocketSubscription.cs | 3 +++ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/CryptoExchange.Net/CryptoExchange.Net.xml b/CryptoExchange.Net/CryptoExchange.Net.xml index 32d62e2..9dd4dc3 100644 --- a/CryptoExchange.Net/CryptoExchange.Net.xml +++ b/CryptoExchange.Net/CryptoExchange.Net.xml @@ -3576,6 +3576,11 @@ The topic of the update, what symbol/asset etc.. + + + The event that triggered the update + + The original data that was received, only available when OutputOriginalData is set to true in the client options @@ -3594,13 +3599,14 @@ The new data - + Create a new DataEvent with data in the from of type K based on the current DataEvent. OriginalData and Timestamp will be copied over The type of the new data The new data The new topic + The new event @@ -3794,6 +3800,11 @@ Socket subscription + + + Subscription id + + Exception event @@ -3940,7 +3951,9 @@ - + + +System.Diagnostics.CodeAnalysis.AllowNullAttribute"> Specifies that is allowed as an input even if the corresponding type disallows it. diff --git a/CryptoExchange.Net/OrderBook/SymbolOrderBook.cs b/CryptoExchange.Net/OrderBook/SymbolOrderBook.cs index e5ad1fb..4a7c942 100644 --- a/CryptoExchange.Net/OrderBook/SymbolOrderBook.cs +++ b/CryptoExchange.Net/OrderBook/SymbolOrderBook.cs @@ -39,6 +39,7 @@ namespace CryptoExchange.Net.OrderBook private readonly bool strictLevels; private readonly bool validateChecksum; + private bool _stopProcessing; private Task? _processTask; private readonly AutoResetEvent _queueEvent; private readonly ConcurrentQueue _processQueue; @@ -255,7 +256,7 @@ namespace CryptoExchange.Net.OrderBook { log.Write(LogLevel.Warning, $"{Id} order book {Symbol} disconnected"); Status = OrderBookStatus.Disconnected; - StopAsync(); + _ = StopAsync(); }; subscription.ConnectionRestored += async time => await ResyncAsync().ConfigureAwait(false); @@ -379,6 +380,12 @@ namespace CryptoExchange.Net.OrderBook if (Status == OrderBookStatus.Disconnected) break; + if (_stopProcessing) + { + log.Write(LogLevel.Trace, $"Skipping message because of resubscribing"); + continue; + } + if (item is InitialOrderBookItem iobi) ProcessInitialOrderBookItem(iobi); if (item is ProcessQueueItem pqi) @@ -473,10 +480,8 @@ namespace CryptoExchange.Net.OrderBook if(!checksumResult) { - // Reconnects the socket, also closing other subscriptions on that socket. - // Should maybe only reconnect the specific subscription? - log.Write(LogLevel.Warning, $"{Id} order book {Symbol} out of sync. Resyncing"); + _stopProcessing = true; Resubscribe(); return; } @@ -490,6 +495,7 @@ namespace CryptoExchange.Net.OrderBook { await subscription!.UnsubscribeAsync().ConfigureAwait(false); Reset(); + _stopProcessing = false; if (!await subscription!.ResubscribeAsync().ConfigureAwait(false)) { // Resubscribing failed, reconnect the socket diff --git a/CryptoExchange.Net/Sockets/SocketSubscription.cs b/CryptoExchange.Net/Sockets/SocketSubscription.cs index 4fcb816..d79e78a 100644 --- a/CryptoExchange.Net/Sockets/SocketSubscription.cs +++ b/CryptoExchange.Net/Sockets/SocketSubscription.cs @@ -7,6 +7,9 @@ namespace CryptoExchange.Net.Sockets /// public class SocketSubscription { + /// + /// Subscription id + /// public int Id { get; } /// /// Exception event