Skip to content

Commit

Permalink
Improve handling of disconnections
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed Jul 18, 2024
1 parent d3af65e commit af18760
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions QuantConnect.AlpacaBrokerage/AlpacaBrokerage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using QuantConnect.Configuration;
using QuantConnect.Brokerages.CrossZero;

Expand Down Expand Up @@ -203,6 +204,13 @@ private void StreamingClient_OnError(IStreamingClient client, Exception obj)
private void StreamingClient_SocketClosed(IStreamingClient client)
{
Log.Trace($"{nameof(StreamingClient_SocketClosed)}({client.GetType().Name}): SocketClosed");
if (_connected)
{
_connected = false;
// let consumers know, we will try to reconnect internally, if we can't lean will kill us
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Disconnect, "Disconnected", "Brokerage Disconnected"));
RunReconnectionLogic(5);
}
}

private void StreamingClient_SocketOpened(IStreamingClient client)
Expand Down Expand Up @@ -559,6 +567,33 @@ public override void Connect()
_connected = true;
}

private void RunReconnectionLogic(int secondsDelay)
{
Task.Delay(TimeSpan.FromSeconds(secondsDelay)).ContinueWith(_ =>
{
try
{
Connect();
// resubscribe required? should/can we unsubscribe? TODO sanity test
Subscribe(_subscriptionManager.GetSubscribedSymbols());
}
catch (Exception ex)
{
Log.Error(ex);
}
if (!IsConnected)
{
RunReconnectionLogic(60);
}
else
{
// let consumers know we are reconnected, avoid lean killing us
OnMessage(new BrokerageMessageEvent(BrokerageMessageType.Reconnect, "Reconnected", "Brokerage Reconnected"));
}
});
}

/// <summary>
/// Disconnects the client from the broker's remote servers
/// </summary>
Expand Down

0 comments on commit af18760

Please sign in to comment.