Skip to content

Commit

Permalink
Fixed reconnection bug for query connections without active subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Dec 23, 2024
1 parent f90e033 commit 34d6568
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Kraken.Net/Clients/SpotApi/KrakenSocketClientSpotApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion Kraken.Net/Objects/Options/KrakenSocketOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public KrakenSocketOptions()
/// </summary>
public SocketApiOptions SpotOptions { get; private set; } = new SocketApiOptions()
{
SocketNoDataTimeout = TimeSpan.FromSeconds(10)
};

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions Kraken.Net/Objects/Sockets/Queries/KrakenSpotPingQuery.cs
Original file line number Diff line number Diff line change
@@ -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<KrakenSocketResponseV2<object>>
{
public override HashSet<string> ListenerIdentifiers { get; set; }

public KrakenSpotPingQuery() : base(new KrakenSocketRequestV2 { Method = "ping", RequestId = ExchangeHelpers.NextId() }, false)
{
RequestTimeout = TimeSpan.FromSeconds(5);
ListenerIdentifiers = new HashSet<string>() { ((KrakenSocketRequestV2)Request).RequestId.ToString() };
}
}
}

0 comments on commit 34d6568

Please sign in to comment.