feat(reflector): 500ms sender-side debounce on rx fanout

Caps peer/<id>/client/<call>/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/<call>/rx (also retained, on-broker only) is unaffected
— it carries the full native rate.
This commit is contained in:
Audric committed 2026-04-28 21:16:40 +02:00
1 parent b5b54cb8d6
commit f4bc684d7b
2 files changed
+74 -1

No files matched your search

+53 -1
View File
@@ -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<milliseconds>(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<int>(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)
{
+21
View File
@@ -36,6 +36,8 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <sigc++/sigc++.h>
#include <sys/time.h>
#include <chrono>
#include <map>
#include <set>
#include <vector>
#include <string>
@@ -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/<id>/client/<call>/rx wire emit is capped at 2 Hz per callsign.
// Local client/<call>/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<std::string, RxDebounceEntry> 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);