Skip to content

Commit 57ea4e7

Browse files
Improved auto-reconnect logic to better handle long retry intervals
1 parent 506862f commit 57ea4e7

File tree

3 files changed

+22
-18
lines changed

3 files changed

+22
-18
lines changed

src/lib/transport/DataSubscriber.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ void SubscriberConnector::AutoReconnect(DataSubscriber* subscriber)
8989
}
9090

9191
// Apply exponential back-off algorithm for retry attempt delays
92-
int32_t retryInterval = connector.m_connectAttempt > 0 ? connector.m_retryInterval * int32_t(pow(2, connector.m_connectAttempt - 1)) : 0;
92+
const int32_t exponent = (connector.m_connectAttempt > 13 ? 13 : connector.m_connectAttempt) - 1;
93+
int32_t retryInterval = connector.m_connectAttempt > 0 ? connector.m_retryInterval * int32_t(pow(2, exponent)) : 0;
9394

9495
if (retryInterval > connector.m_maxRetryInterval)
9596
retryInterval = connector.m_maxRetryInterval;
@@ -150,7 +151,7 @@ bool SubscriberConnector::Connect(DataSubscriber& subscriber, bool autoReconnect
150151

151152
m_cancel = false;
152153

153-
while (!subscriber.m_disconnecting)
154+
while (!subscriber.m_disposing)
154155
{
155156
if (m_maxRetries != -1 && m_connectAttempt >= m_maxRetries)
156157
{
@@ -181,10 +182,11 @@ bool SubscriberConnector::Connect(DataSubscriber& subscriber, bool autoReconnect
181182
errorMessage = current_exception_diagnostic_information(true);
182183
}
183184

184-
if (!connected && !subscriber.m_disconnecting)
185+
if (!connected && !subscriber.m_disposing)
185186
{
186187
// Apply exponential back-off algorithm for retry attempt delays
187-
int32_t retryInterval = m_connectAttempt > 0 ? m_retryInterval * int32_t(pow(2, m_connectAttempt - 1)) : 0;
188+
const int32_t exponent = (m_connectAttempt > 13 ? 13 : m_connectAttempt) - 1;
189+
int32_t retryInterval = m_connectAttempt > 0 ? m_retryInterval * int32_t(pow(2, exponent)) : 0;
188190
autoReconnecting = true;
189191

190192
if (retryInterval > m_maxRetryInterval)
@@ -324,6 +326,7 @@ DataSubscriber::DataSubscriber() :
324326
m_compressMetadata(true),
325327
m_compressSignalIndexCache(true),
326328
m_disconnecting(false),
329+
m_disposing(false),
327330
m_userData(nullptr),
328331
m_totalCommandChannelBytesReceived(0UL),
329332
m_totalDataChannelBytesReceived(0UL),
@@ -348,6 +351,7 @@ DataSubscriber::DataSubscriber() :
348351
// Destructor calls disconnect to clean up after itself.
349352
DataSubscriber::~DataSubscriber()
350353
{
354+
m_disposing = true;
351355
Disconnect();
352356
}
353357

src/lib/transport/DataSubscriber.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ namespace transport
183183
bool m_compressMetadata;
184184
bool m_compressSignalIndexCache;
185185
volatile bool m_disconnecting;
186+
volatile bool m_disposing;
186187
void* m_userData;
187188

188189
// Statistics counters

src/samples/InstanceSubscribe/SubscriberHandler.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,7 @@ SubscriptionInfo SubscriberHandler::CreateSubscriptionInfo()
4242

4343
// TODO: Modify subscription info properties as desired...
4444

45-
// To set up a remotely synchronized subscription, set this flag
46-
// to true and add the framesPerSecond parameter to the
47-
// ExtraConnectionStringParameters. Additionally, the following
48-
// example demonstrates the use of some other useful parameters
49-
// when setting up remotely synchronized subscriptions.
50-
51-
//info.RemotelySynchronized = true;
52-
//info.ExtraConnectionStringParameters = "framesPerSecond=30;timeResolution=10000;downsamplingMethod=Closest";
53-
//info.LagTime = 3.0;
54-
//info.LeadTime = 1.0;
55-
//info.UseLocalClockAsRealTime = false;
56-
57-
// Other example properties (see SubscriptionInfo class in DataSubscriber.h for all properties)
45+
// See SubscriptionInfo class in DataSubscriber.h for all properties
5846
//info.Throttled = false;
5947
//info.IncludeTime = true;
6048
//info.UseMillisecondResolution = true;
@@ -66,8 +54,19 @@ void SubscriberHandler::SetupSubscriberConnector(SubscriberConnector& connector)
6654
{
6755
SubscriberInstance::SetupSubscriberConnector(connector);
6856

69-
// TODO: Modify connector properties as desired...
57+
// TODO: Customize subscriber connector properties as desired...
58+
59+
//// Enable auto-reconnect sequence:
60+
//connector.SetAutoReconnect(true);
61+
62+
//// Set maximum number to attempt reconnection, -1 means never stop retrying connection attempts:
7063
//connector.SetMaxRetries(-1);
64+
//
65+
//// Set number of initial milliseconds to wait before retrying connection attempt:
66+
//connector.SetRetryInterval(2000);
67+
//
68+
//// Set maximum number of milliseconds to wait before retrying connection attempt, connection retry attempts use exponential back-off algorithm up to this defined maximum:
69+
//connector.SetMaxRetryInterval(120000);
7170
}
7271

7372
void SubscriberHandler::StatusMessage(const string& message)

0 commit comments

Comments
 (0)