From f4bc684d7bc8868aacc79b43df323d939ce913e2 Mon Sep 17 00:00:00 2001 From: Audric Date: Tue, 28 Apr 2026 21:16:40 +0200 Subject: [PATCH] feat(reflector): 500ms sender-side debounce on rx fanout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Caps peer//client//rx wire emit at 2Hz per local callsign, preventing 50Hz native rx churn from saturating satellite/twin links and the receiving brokers' retained store. Local client//rx (also retained, on-broker only) is unaffected — it carries the full native rate. --- src/svxlink/reflector/Reflector.cpp | 54 ++++++++++++++++++++++++++++- src/svxlink/reflector/Reflector.h | 21 +++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/svxlink/reflector/Reflector.cpp b/src/svxlink/reflector/Reflector.cpp index 6bca9f2..83f11d0 100644 --- a/src/svxlink/reflector/Reflector.cpp +++ b/src/svxlink/reflector/Reflector.cpp @@ -3117,7 +3117,7 @@ void Reflector::publishRxUpdate(ReflectorClient* client) { m_mqtt->onRxUpdate(client->callsign(), client->rxStatusJson()); } - fanoutClientRx(client->callsign(), client->rxStatusJson()); + emitRxDebounced(client->callsign(), client->rxStatusJson()); publishClientStatus(client); } /* Reflector::publishRxUpdate */ @@ -3199,6 +3199,17 @@ void Reflector::fanoutClientDisconnected(const std::string& callsign) { m_twin_link->sendClientDisconnected(callsign); } + + auto it = m_rx_debounce.find(callsign); + if (it != m_rx_debounce.end()) + { + if (it->second.pending != nullptr) + { + delete it->second.pending; + it->second.pending = nullptr; + } + m_rx_debounce.erase(it); + } } @@ -3228,6 +3239,47 @@ void Reflector::fanoutClientRx(const std::string& callsign, } +void Reflector::emitRxDebounced(const std::string& callsign, + const Json::Value& rx_json) +{ + using namespace std::chrono; + auto& e = m_rx_debounce[callsign]; + auto now = steady_clock::now(); + auto since_ms = duration_cast(now - e.last_emit).count(); + + if (since_ms >= PEER_RX_DEBOUNCE_MS) + { + // Emit immediately + fanoutClientRx(callsign, rx_json); + e.last_emit = now; + if (e.pending != nullptr) + { + delete e.pending; + e.pending = nullptr; + } + } + else + { + // Coalesce: hold the latest value, schedule a one-shot residual timer + e.pending_value = rx_json; + if (e.pending == nullptr) + { + int residual = PEER_RX_DEBOUNCE_MS - static_cast(since_ms); + e.pending = new Async::Timer(residual, Async::Timer::TYPE_ONESHOT); + e.pending->expired.connect([this, callsign](Async::Timer*) { + auto it = m_rx_debounce.find(callsign); + if (it == m_rx_debounce.end()) return; + auto& e2 = it->second; + Json::Value v = std::move(e2.pending_value); + delete e2.pending; + e2.pending = nullptr; + emitRxDebounced(callsign, v); + }); + } + } +} /* Reflector::emitRxDebounced */ + + void Reflector::fanoutClientStatus(const std::string& callsign, const Json::Value& status_json) { diff --git a/src/svxlink/reflector/Reflector.h b/src/svxlink/reflector/Reflector.h index cfd2994..eb21970 100644 --- a/src/svxlink/reflector/Reflector.h +++ b/src/svxlink/reflector/Reflector.h @@ -36,6 +36,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include #include +#include +#include #include #include #include @@ -413,6 +415,25 @@ class Reflector : public sigc::trackable RedisStore* m_redis = nullptr; Async::Timer m_mqtt_status_timer; + // Per-local-callsign rx-debounce state used by fanoutClientRx so that + // peer//client//rx wire emit is capped at 2 Hz per callsign. + // Local client//rx (also retained, but on-broker only) is not + // affected — it carries the native ~50 Hz update rate. + struct RxDebounceEntry + { + std::chrono::steady_clock::time_point last_emit; + Async::Timer* pending = nullptr; + Json::Value pending_value; + }; + std::map m_rx_debounce; + static constexpr int PEER_RX_DEBOUNCE_MS = 500; + + // Schedule an rx fanout for a local callsign. Emits immediately if the + // last emit was >=500ms ago; otherwise coalesces and re-fires once the + // debounce window expires. + void emitRxDebounced(const std::string& callsign, + const Json::Value& rx_json); + Reflector(const Reflector&); Reflector& operator=(const Reflector&); void clientConnected(Async::FramedTcpConnection *con);