Skip to content

Implement exponential retry/back-off strategy to avoid router overloading. #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/AIoTC_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,12 @@
#define HAS_TCP
#endif

/******************************************************************************
* CONSTANTS
******************************************************************************/

#define AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms (1000UL)
#define AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms (32000UL)
#define AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms (10000UL)

#endif /* ARDUINO_AIOTC_CONFIG_H_ */
33 changes: 21 additions & 12 deletions src/ArduinoIoTCloudTCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,6 @@
extern RTC_HandleTypeDef RTCHandle;
#endif

/******************************************************************************
GLOBAL CONSTANTS
******************************************************************************/

static const int TIMEOUT_FOR_LASTVALUES_SYNC = 10000;

/******************************************************************************
LOCAL MODULE FUNCTIONS
******************************************************************************/
Expand All @@ -74,7 +68,9 @@ extern "C" unsigned long getTime()

ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
: _state{State::ConnectPhy}
, _lastSyncRequestTickTime{0}
, _next_connection_attempt_tick{0}
, _last_connection_attempt_cnt{0}
, _last_sync_request_tick{0}
, _mqtt_data_buf{0}
, _mqtt_data_len{0}
, _mqtt_data_request_retransmit{false}
Expand Down Expand Up @@ -337,9 +333,13 @@ void ArduinoIoTCloudTCP::printDebugInfo()
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy()
{
if (_connection->check() == NetworkConnectionState::CONNECTED)
return State::SyncTime;
else
return State::ConnectPhy;
{
bool const is_retry_attempt = (_last_connection_attempt_cnt > 0);
if (!is_retry_attempt || (is_retry_attempt && (millis() > _next_connection_attempt_tick)))
return State::SyncTime;
}

return State::ConnectPhy;
}

ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
Expand All @@ -352,9 +352,18 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime()
ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker()
{
if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort))
{
_last_connection_attempt_cnt = 0;
return State::SubscribeMqttTopics;
}

_last_connection_attempt_cnt++;
unsigned long reconnection_retry_delay = (1 << _last_connection_attempt_cnt) * AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms;
reconnection_retry_delay = min(reconnection_retry_delay, static_cast<unsigned long>(AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms));
_next_connection_attempt_tick = millis() + reconnection_retry_delay;

DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort);
DEBUG_ERROR("ArduinoIoTCloudTCP::%s %d connection attempt at tick time %d", __FUNCTION__, _last_connection_attempt_cnt, _next_connection_attempt_tick);
return State::ConnectPhy;
}

Expand Down Expand Up @@ -410,11 +419,11 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues()

/* Check whether or not we need to send a new request. */
unsigned long const now = millis();
if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC)
if ((now - _last_sync_request_tick) > AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms)
{
DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values requested", __FUNCTION__, now);
requestLastValue();
_lastSyncRequestTickTime = now;
_last_sync_request_tick = now;
}

return State::RequestLastValues;
Expand Down
4 changes: 3 additions & 1 deletion src/ArduinoIoTCloudTCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass

State _state;

int _lastSyncRequestTickTime;
unsigned long _next_connection_attempt_tick;
unsigned int _last_connection_attempt_cnt;
unsigned long _last_sync_request_tick;
String _brokerAddress;
uint16_t _brokerPort;
uint8_t _mqtt_data_buf[MQTT_TRANSMIT_BUFFER_SIZE];
Expand Down
10 changes: 10 additions & 0 deletions src/utility/watchdog/Watchdog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ void wifi_nina_feed_watchdog()
{
samd_watchdog_reset();
}

void mkr_gsm_feed_watchdog()
{
samd_watchdog_reset();
}

void mkr_nb_feed_watchdog()
{
samd_watchdog_reset();
}
#endif /* ARDUINO_ARCH_SAMD */

#ifdef ARDUINO_ARCH_MBED
Expand Down