Skip to content

Commit

Permalink
FabBackend: fail fast on unresponsive backend
Browse files Browse the repository at this point in the history
  • Loading branch information
PBrunot committed Aug 16, 2024
1 parent ef7535f commit 4a8ca30
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
5 changes: 4 additions & 1 deletion conf/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,14 @@ namespace fabomatic
static constexpr std::string_view request_topic{"/request"};

/// @brief Number of tries to get a reply from the backend
static constexpr auto MAX_TRIES{2};
static constexpr auto MAX_TRIES{1};

/// @brief Timeout for a single backend reply request.
static constexpr auto TIMEOUT_REPLY_SERVER{2s};

/// @brief Once backend is unresponsive, wait at least this period before to try again
static constexpr auto FAIL_FAST_PERIOD {45s};

/// @brief MQTT port for broker
static constexpr auto PORT_NUMBER{1883};

Expand Down
1 change: 1 addition & 0 deletions include/FabBackend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ namespace fabomatic
[[nodiscard]] auto hasBufferedMsg() const -> bool;
[[nodiscard]] auto transmitBuffer() -> bool;
[[nodiscard]] auto saveBuffer() -> bool;
[[nodiscard]] auto shouldFailFast() const -> bool;

[[nodiscard]] auto checkBackendRequest() -> std::optional<std::unique_ptr<MQTTInterface::BackendRequest>>;

Expand Down
7 changes: 6 additions & 1 deletion src/AuthProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ namespace fabomatic
ESP_LOGD(TAG, "tryLogin called for %s", uid_str.c_str());

if (!server.isOnline())
server.connect();
{
if (!server.shouldFailFast())
{
server.connect();
}
}

if (server.isOnline())
{
Expand Down
35 changes: 29 additions & 6 deletions src/FabBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -457,12 +457,17 @@ namespace fabomatic
// To preserve chronological order, we cannot send new messages until the old ones have been sent.
ESP_LOGW(TAG, "Online with pending messages that could not be transmitted, retrying...");

Tasks::delay(250ms);

if (!isOnline())
if (!shouldFailFast())
{
connect();
}
else
{
ESP_LOGW(TAG, "processQuery: failing fast due to unresponsive backend.");
break;
}

Tasks::delay(250ms);
nb_tries++;
}

Expand Down Expand Up @@ -514,12 +519,20 @@ namespace fabomatic
// To preserve chronological order, we cannot send new messages until the old ones have been sent.
ESP_LOGW(TAG, "Online with pending messages that could not be transmitted, retrying...");

Tasks::delay(250ms);

if (!isOnline())
{
connect();
if (!shouldFailFast())
{
connect();
}
else
{
ESP_LOGW(TAG, "processQuery: failing fast due to unresponsive backend.");
break;
}
}

Tasks::delay(250ms);
nb_tries++;
}

Expand Down Expand Up @@ -721,4 +734,14 @@ namespace fabomatic
return std::nullopt;
}

/**
* @brief when server is unresponsive, wait for a configurable time before to try again
*/
auto FabBackend::shouldFailFast() const -> bool
{
return this->mqtt_connected &&
this->last_unresponsive.has_value() &&
(Tasks::arduinoNow() - this->last_unresponsive.value()) <= conf::mqtt::FAIL_FAST_PERIOD;
}

} // namespace fabomatic
4 changes: 3 additions & 1 deletion test/test_mqtt/test_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace fabomatic::tests

void busyWait(std::chrono::seconds d = 3s)
{
auto start = fabomatic::Tasks::arduinoNow();
const auto start = fabomatic::Tasks::arduinoNow();
while (fabomatic::Tasks::arduinoNow() - start <= d)
{
test_scheduler.execute();
Expand Down Expand Up @@ -367,6 +367,8 @@ namespace fabomatic::tests
// Renable backend
broker.generateReplies(true);

TEST_ASSERT_TRUE_MESSAGE(server.connect(), "(degraded) force reconnection");

auto alive_resp = server.alive();
TEST_ASSERT_TRUE_MESSAGE(alive_resp, "(degraded) Server alive failed");
TEST_ASSERT_FALSE_MESSAGE(server.hasBufferedMsg(), "(degraded) Buffered messages not sent");
Expand Down

0 comments on commit 4a8ca30

Please sign in to comment.