From 34d6568b14d20566a0149326a9ec28623812b401 Mon Sep 17 00:00:00 2001 From: JKorf Date: Mon, 23 Dec 2024 21:43:04 +0100 Subject: [PATCH] Fixed reconnection bug for query connections without active subscription --- .../SpotApi/KrakenSocketClientSpotApi.cs | 14 ++++++++++++++ .../Objects/Options/KrakenSocketOptions.cs | 1 - .../Sockets/Queries/KrakenSpotPingQuery.cs | 18 ++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 Kraken.Net/Objects/Sockets/Queries/KrakenSpotPingQuery.cs diff --git a/Kraken.Net/Clients/SpotApi/KrakenSocketClientSpotApi.cs b/Kraken.Net/Clients/SpotApi/KrakenSocketClientSpotApi.cs index e848275..4100036 100644 --- a/Kraken.Net/Clients/SpotApi/KrakenSocketClientSpotApi.cs +++ b/Kraken.Net/Clients/SpotApi/KrakenSocketClientSpotApi.cs @@ -57,6 +57,20 @@ internal KrakenSocketClientSpotApi(ILogger logger, KrakenSocketOptions options) RateLimiter = KrakenExchange.RateLimiter.SpotSocket; SetDedicatedConnection(_privateBaseAddress, false); + + RegisterPeriodicQuery( + "Ping", + TimeSpan.FromSeconds(30), + x => new KrakenSpotPingQuery(), + (connection, result) => + { + if (result.Error?.Message.Equals("Query timeout") == true) + { + // Ping timeout, reconnect + _logger.LogWarning("[Sckt {SocketId}] Ping response timeout, reconnecting", connection.SocketId); + _ = connection.TriggerReconnectAsync(); + } + }); } #endregion diff --git a/Kraken.Net/Objects/Options/KrakenSocketOptions.cs b/Kraken.Net/Objects/Options/KrakenSocketOptions.cs index 1c5d5c0..cb7ee77 100644 --- a/Kraken.Net/Objects/Options/KrakenSocketOptions.cs +++ b/Kraken.Net/Objects/Options/KrakenSocketOptions.cs @@ -34,7 +34,6 @@ public KrakenSocketOptions() /// public SocketApiOptions SpotOptions { get; private set; } = new SocketApiOptions() { - SocketNoDataTimeout = TimeSpan.FromSeconds(10) }; /// diff --git a/Kraken.Net/Objects/Sockets/Queries/KrakenSpotPingQuery.cs b/Kraken.Net/Objects/Sockets/Queries/KrakenSpotPingQuery.cs new file mode 100644 index 0000000..4377003 --- /dev/null +++ b/Kraken.Net/Objects/Sockets/Queries/KrakenSpotPingQuery.cs @@ -0,0 +1,18 @@ +using CryptoExchange.Net.Objects.Sockets; +using CryptoExchange.Net.Sockets; +using Kraken.Net.Objects.Internal; +using Kraken.Net.Objects.Models.Socket; + +namespace Kraken.Net.Objects.Sockets.Queries +{ + internal class KrakenSpotPingQuery : Query> + { + public override HashSet ListenerIdentifiers { get; set; } + + public KrakenSpotPingQuery() : base(new KrakenSocketRequestV2 { Method = "ping", RequestId = ExchangeHelpers.NextId() }, false) + { + RequestTimeout = TimeSpan.FromSeconds(5); + ListenerIdentifiers = new HashSet() { ((KrakenSocketRequestV2)Request).RequestId.ToString() }; + } + } +}