From d0b8a44dc706cde6ec57fd48894b8531b8c4ab59 Mon Sep 17 00:00:00 2001 From: Audric Date: Wed, 15 Apr 2026 09:34:42 +0200 Subject: [PATCH] Refresh live-state TTLs every 30s; expose Redis metrics in /status --- src/svxlink/reflector/RedisStore.cpp | 21 ++++++++++++++++++++- src/svxlink/reflector/RedisStore.h | 1 + src/svxlink/reflector/Reflector.cpp | 9 +++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/svxlink/reflector/RedisStore.cpp b/src/svxlink/reflector/RedisStore.cpp index 09786d2..c7b103f 100644 --- a/src/svxlink/reflector/RedisStore.cpp +++ b/src/svxlink/reflector/RedisStore.cpp @@ -29,6 +29,7 @@ void onAsyncDisconnectThunk(const redisAsyncContext* ac, int status) { RedisStore::RedisStore(const Config& cfg) : m_cfg(cfg) {} RedisStore::~RedisStore() { + delete m_heartbeat_timer; m_heartbeat_timer = nullptr; delete m_drain_timer; m_drain_timer = nullptr; delete m_live_queue; m_live_queue = nullptr; if (m_async) { redisAsyncFree(m_async); m_async = nullptr; } @@ -81,6 +82,10 @@ bool RedisStore::connect(void) { m_drain_timer->expired.connect( sigc::mem_fun(*this, &RedisStore::drainLiveQueue)); + m_heartbeat_timer = new Async::Timer(30000, Async::Timer::TYPE_PERIODIC); + m_heartbeat_timer->expired.connect( + sigc::mem_fun(*this, &RedisStore::refreshLiveExpire)); + std::cout << "RedisStore: connected sync+async, subscribed to " << channelName() << std::endl; return true; @@ -226,6 +231,7 @@ void RedisStore::pushLiveTalker(uint32_t tg, const std::string& callsign, RedisLiveQueue::Op op; op.op = RedisLiveQueue::OpType::HSET; op.key = keyFor("live:talker:" + std::to_string(tg)); + m_live_keys.insert(op.key); op.fields = { {"callsign", callsign}, {"started_at", std::to_string(std::time(nullptr))}, @@ -240,6 +246,7 @@ void RedisStore::clearLiveTalker(uint32_t tg) { RedisLiveQueue::Op op; op.op = RedisLiveQueue::OpType::DEL; op.key = keyFor("live:talker:" + std::to_string(tg)); + m_live_keys.erase(op.key); m_live_queue->push(std::move(op)); } @@ -251,6 +258,7 @@ void RedisStore::pushLiveClient(const std::string& callsign, RedisLiveQueue::Op op; op.op = RedisLiveQueue::OpType::HSET; op.key = keyFor("live:client:" + callsign); + m_live_keys.insert(op.key); op.fields = { {"connected_at", std::to_string(std::time(nullptr))}, {"ip", ip}, @@ -266,6 +274,7 @@ void RedisStore::clearLiveClient(const std::string& callsign) { RedisLiveQueue::Op op; op.op = RedisLiveQueue::OpType::DEL; op.key = keyFor("live:client:" + callsign); + m_live_keys.erase(op.key); m_live_queue->push(std::move(op)); } @@ -276,6 +285,7 @@ void RedisStore::pushLiveTrunk(const std::string& section, RedisLiveQueue::Op op; op.op = RedisLiveQueue::OpType::HSET; op.key = keyFor("live:trunk:" + section); + m_live_keys.insert(op.key); op.fields = { {"state", state}, {"peer_id", peer_id}, @@ -368,7 +378,16 @@ void RedisStore::drainLiveQueue(Async::Timer*) { redisAsyncCommand(m_async, nullptr, nullptr, "PUBLISH %s tick", ch.c_str()); } -void RedisStore::refreshLiveExpire(Async::Timer*) {} +void RedisStore::refreshLiveExpire(Async::Timer*) { + if (!m_live_queue) return; + for (const std::string& k : m_live_keys) { + RedisLiveQueue::Op op; + op.op = RedisLiveQueue::OpType::EXPIRE; + op.key = k; + op.ttl_s = 60; + m_live_queue->push(std::move(op)); + } +} std::string RedisStore::keyFor(const std::string& suffix) const { if (m_cfg.key_prefix.empty()) return suffix; diff --git a/src/svxlink/reflector/RedisStore.h b/src/svxlink/reflector/RedisStore.h index 50d0c50..c3f3517 100644 --- a/src/svxlink/reflector/RedisStore.h +++ b/src/svxlink/reflector/RedisStore.h @@ -94,6 +94,7 @@ class RedisStore : public sigc::trackable Async::Timer* m_heartbeat_timer = nullptr; int m_reconnect_backoff_s = 1; uint64_t m_dropped_live_writes = 0; + std::set m_live_keys; bool connectSync(void); bool connectAsync(void); diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index 1613b79..28ef084 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -1763,6 +1763,15 @@ void Reflector::refreshStatus(void) m_status["satellite_server"] = sat_srv; } } + if (m_redis != nullptr) + { + Json::Value redis_status(Json::objectValue); + redis_status["live_queue_size"] = + static_cast(m_redis->liveQueueSize()); + redis_status["dropped_live_writes"] = + static_cast(m_redis->droppedLiveWrites()); + m_status["redis"] = redis_status; + } } /* Reflector::refreshStatus */