From 2981361c6d922c487c8fe9d48fe4944abde326da Mon Sep 17 00:00:00 2001 From: Audric Date: Wed, 15 Apr 2026 09:47:04 +0200 Subject: [PATCH] Redis: reconnect with exponential backoff and full reload on resume --- src/svxlink/reflector/RedisStore.cpp | 40 ++++++++++++++++++++++++++-- src/svxlink/reflector/RedisStore.h | 1 + 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/svxlink/reflector/RedisStore.cpp b/src/svxlink/reflector/RedisStore.cpp index c7b103f..5fe483d 100644 --- a/src/svxlink/reflector/RedisStore.cpp +++ b/src/svxlink/reflector/RedisStore.cpp @@ -29,6 +29,8 @@ void onAsyncDisconnectThunk(const redisAsyncContext* ac, int status) { RedisStore::RedisStore(const Config& cfg) : m_cfg(cfg) {} RedisStore::~RedisStore() { + m_shutting_down = true; + delete m_reconnect_timer; m_reconnect_timer = nullptr; delete m_heartbeat_timer; m_heartbeat_timer = nullptr; delete m_drain_timer; m_drain_timer = nullptr; delete m_live_queue; m_live_queue = nullptr; @@ -333,8 +335,42 @@ void RedisStore::subscribe(void) { redisAsyncCommand(m_async, onPubSubMessageThunk, this, "SUBSCRIBE %s", ch.c_str()); } -void RedisStore::scheduleReconnect(void) {} -void RedisStore::onAsyncDisconnect(int) {} +void RedisStore::onAsyncDisconnect(int /*status*/) { + if (m_shutting_down) return; + std::cerr << "*** WARN: Redis async disconnect" << std::endl; + // hiredis will free the context after this returns — just null out. + m_async = nullptr; + scheduleReconnect(); +} + +void RedisStore::scheduleReconnect(void) { + if (m_shutting_down) return; + delete m_reconnect_timer; + m_reconnect_timer = new Async::Timer(m_reconnect_backoff_s * 1000, + Async::Timer::TYPE_ONESHOT); + m_reconnect_timer->expired.connect([this](Async::Timer*) { + if (m_shutting_down) return; + if (connectAsync()) { + subscribe(); + // Rebuild sync context too — its TCP socket is likely dead. + if (m_sync) { redisFree(m_sync); m_sync = nullptr; } + if (!connectSync()) { + std::cerr << "*** WARN: Redis sync reconnect failed" << std::endl; + // async is up but sync isn't — schedule another retry so we fix sync. + m_reconnect_backoff_s = std::min(m_reconnect_backoff_s * 2, 30); + scheduleReconnect(); + return; + } + std::cout << "RedisStore: reconnected, re-subscribing and requesting " + "full reload" << std::endl; + m_reconnect_backoff_s = 1; + configChanged.emit("all"); + } else { + m_reconnect_backoff_s = std::min(m_reconnect_backoff_s * 2, 30); + scheduleReconnect(); + } + }); +} void RedisStore::onPubSubMessage(const std::string&, const std::string&) {} void RedisStore::drainLiveQueue(Async::Timer*) { if (!m_async || !m_live_queue) return; diff --git a/src/svxlink/reflector/RedisStore.h b/src/svxlink/reflector/RedisStore.h index c3f3517..1f8d8b7 100644 --- a/src/svxlink/reflector/RedisStore.h +++ b/src/svxlink/reflector/RedisStore.h @@ -93,6 +93,7 @@ class RedisStore : public sigc::trackable Async::Timer* m_reconnect_timer = nullptr; Async::Timer* m_heartbeat_timer = nullptr; int m_reconnect_backoff_s = 1; + bool m_shutting_down = false; uint64_t m_dropped_live_writes = 0; std::set m_live_keys;