1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-10 17:36:19 +00:00

Prevent reconnect spamming when invalid checksum

This commit is contained in:
Jkorf 2021-09-20 11:19:42 +02:00
parent 84c16a5dc5
commit f9957cba16
3 changed files with 28 additions and 6 deletions

View File

@ -3576,6 +3576,11 @@
The topic of the update, what symbol/asset etc.. The topic of the update, what symbol/asset etc..
</summary> </summary>
</member> </member>
<member name="P:CryptoExchange.Net.Sockets.DataEvent`1.Event">
<summary>
The event that triggered the update
</summary>
</member>
<member name="P:CryptoExchange.Net.Sockets.DataEvent`1.OriginalData"> <member name="P:CryptoExchange.Net.Sockets.DataEvent`1.OriginalData">
<summary> <summary>
The original data that was received, only available when OutputOriginalData is set to true in the client options The original data that was received, only available when OutputOriginalData is set to true in the client options
@ -3594,13 +3599,14 @@
<param name="data">The new data</param> <param name="data">The new data</param>
<returns></returns> <returns></returns>
</member> </member>
<member name="M:CryptoExchange.Net.Sockets.DataEvent`1.As``1(``0,System.String)"> <member name="M:CryptoExchange.Net.Sockets.DataEvent`1.As``1(``0,System.String,System.String)">
<summary> <summary>
Create a new DataEvent with data in the from of type K based on the current DataEvent. OriginalData and Timestamp will be copied over Create a new DataEvent with data in the from of type K based on the current DataEvent. OriginalData and Timestamp will be copied over
</summary> </summary>
<typeparam name="K">The type of the new data</typeparam> <typeparam name="K">The type of the new data</typeparam>
<param name="data">The new data</param> <param name="data">The new data</param>
<param name="topic">The new topic</param> <param name="topic">The new topic</param>
<param name="event">The new event</param>
<returns></returns> <returns></returns>
</member> </member>
<member name="T:CryptoExchange.Net.Sockets.MessageEvent"> <member name="T:CryptoExchange.Net.Sockets.MessageEvent">
@ -3794,6 +3800,11 @@
Socket subscription Socket subscription
</summary> </summary>
</member> </member>
<member name="P:CryptoExchange.Net.Sockets.SocketSubscription.Id">
<summary>
Subscription id
</summary>
</member>
<member name="E:CryptoExchange.Net.Sockets.SocketSubscription.Exception"> <member name="E:CryptoExchange.Net.Sockets.SocketSubscription.Exception">
<summary> <summary>
Exception event Exception event
@ -3940,7 +3951,9 @@
<member name="M:CryptoExchange.Net.Sockets.WebsocketFactory.CreateWebsocket(CryptoExchange.Net.Logging.Log,System.String,System.Collections.Generic.IDictionary{System.String,System.String},System.Collections.Generic.IDictionary{System.String,System.String})"> <member name="M:CryptoExchange.Net.Sockets.WebsocketFactory.CreateWebsocket(CryptoExchange.Net.Logging.Log,System.String,System.Collections.Generic.IDictionary{System.String,System.String},System.Collections.Generic.IDictionary{System.String,System.String})">
<inheritdoc /> <inheritdoc />
</member> </member>
<member name="T:System.Diagnostics.CodeAnalysis.AllowNullAttribute"> </members>
</doc>
System.Diagnostics.CodeAnalysis.AllowNullAttribute">
<summary> <summary>
Specifies that <see langword="null"/> is allowed as an input even if the Specifies that <see langword="null"/> is allowed as an input even if the
corresponding type disallows it. corresponding type disallows it.

View File

@ -39,6 +39,7 @@ namespace CryptoExchange.Net.OrderBook
private readonly bool strictLevels; private readonly bool strictLevels;
private readonly bool validateChecksum; private readonly bool validateChecksum;
private bool _stopProcessing;
private Task? _processTask; private Task? _processTask;
private readonly AutoResetEvent _queueEvent; private readonly AutoResetEvent _queueEvent;
private readonly ConcurrentQueue<object> _processQueue; private readonly ConcurrentQueue<object> _processQueue;
@ -255,7 +256,7 @@ namespace CryptoExchange.Net.OrderBook
{ {
log.Write(LogLevel.Warning, $"{Id} order book {Symbol} disconnected"); log.Write(LogLevel.Warning, $"{Id} order book {Symbol} disconnected");
Status = OrderBookStatus.Disconnected; Status = OrderBookStatus.Disconnected;
StopAsync(); _ = StopAsync();
}; };
subscription.ConnectionRestored += async time => await ResyncAsync().ConfigureAwait(false); subscription.ConnectionRestored += async time => await ResyncAsync().ConfigureAwait(false);
@ -379,6 +380,12 @@ namespace CryptoExchange.Net.OrderBook
if (Status == OrderBookStatus.Disconnected) if (Status == OrderBookStatus.Disconnected)
break; break;
if (_stopProcessing)
{
log.Write(LogLevel.Trace, $"Skipping message because of resubscribing");
continue;
}
if (item is InitialOrderBookItem iobi) if (item is InitialOrderBookItem iobi)
ProcessInitialOrderBookItem(iobi); ProcessInitialOrderBookItem(iobi);
if (item is ProcessQueueItem pqi) if (item is ProcessQueueItem pqi)
@ -473,10 +480,8 @@ namespace CryptoExchange.Net.OrderBook
if(!checksumResult) 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"); log.Write(LogLevel.Warning, $"{Id} order book {Symbol} out of sync. Resyncing");
_stopProcessing = true;
Resubscribe(); Resubscribe();
return; return;
} }
@ -490,6 +495,7 @@ namespace CryptoExchange.Net.OrderBook
{ {
await subscription!.UnsubscribeAsync().ConfigureAwait(false); await subscription!.UnsubscribeAsync().ConfigureAwait(false);
Reset(); Reset();
_stopProcessing = false;
if (!await subscription!.ResubscribeAsync().ConfigureAwait(false)) if (!await subscription!.ResubscribeAsync().ConfigureAwait(false))
{ {
// Resubscribing failed, reconnect the socket // Resubscribing failed, reconnect the socket

View File

@ -7,6 +7,9 @@ namespace CryptoExchange.Net.Sockets
/// </summary> /// </summary>
public class SocketSubscription public class SocketSubscription
{ {
/// <summary>
/// Subscription id
/// </summary>
public int Id { get; } public int Id { get; }
/// <summary> /// <summary>
/// Exception event /// Exception event