fix(repeater/bot): cooldown per sender, not per channel
Reported symptom: "!link arrived only on the second try". Cause was the throttle itself -- one global 10 s window per channel, so a second person asking inside it got silence. That reads as a broken bot rather than "wait ten seconds", and it happened twice while testing. Now each sender has its own window, keyed on the "<name>: " prefix, over an 8-slot LRU. Spoofable by design and that is fine: this is politeness throttling, and a channel message carries no identity beyond that prefix anyway. The airtime guarantee that the global window used to provide is kept explicitly as BOT_MIN_GAP_MS (2.5 s floor between any two replies, whoever asked), so N senders can no longer produce N simultaneous transmissions. Without that, going per-sender would have quietly traded politeness for responsiveness. `bot` now reports both numbers plus a throttled counter, so a suppressed reply is visible instead of looking like a dead node. Bench-verified on the discriminating case, which is the whole point: sender A asking twice in 3.5 s gets one answer, while sender B asking INSIDE A's window gets answered -- 3/3. Note on how that was measured, because it bit me: reading the console counter costs ~2.4 s (the CDC needs a settle delay), so a probe pair with a counter read between them is ~15 s apart and the 10 s window has legitimately expired. The first run of this test "failed" for exactly that reason. Read counters only before and after a pair, never inside it. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
27f1339ac9
commit
fc32a1a0a8
2 files changed
+64
-6
No files matched your search
@@ -48,11 +48,23 @@
|
|||||||
#ifndef BOT_COMMANDS
|
#ifndef BOT_COMMANDS
|
||||||
#define BOT_COMMANDS ""
|
#define BOT_COMMANDS ""
|
||||||
#endif
|
#endif
|
||||||
// The ONLY throttle on a node that is otherwise pure RX. Keep it generous:
|
// Throttle, PER SENDER. Measured reason it is not global: with one global
|
||||||
// every reply is a flood packet on a shared community mesh.
|
// window, two people asking within it meant the second got silence -- observed
|
||||||
|
// twice, and it reads as "the bot is broken" rather than "wait 10 s".
|
||||||
#ifndef BOT_REPLY_COOLDOWN_MS
|
#ifndef BOT_REPLY_COOLDOWN_MS
|
||||||
#define BOT_REPLY_COOLDOWN_MS 10000
|
#define BOT_REPLY_COOLDOWN_MS 10000
|
||||||
#endif
|
#endif
|
||||||
|
// Floor between ANY two replies, whoever asked. This is what still bounds the
|
||||||
|
// node's airtime on a shared community mesh once the main window went
|
||||||
|
// per-sender: N senders can no longer produce N simultaneous replies.
|
||||||
|
#ifndef BOT_MIN_GAP_MS
|
||||||
|
#define BOT_MIN_GAP_MS 2500
|
||||||
|
#endif
|
||||||
|
// Senders tracked for the per-sender window. Wraps LRU; overflowing just means
|
||||||
|
// the oldest sender's window is forgotten early, i.e. one extra reply.
|
||||||
|
#ifndef BOT_PEER_SLOTS
|
||||||
|
#define BOT_PEER_SLOTS 8
|
||||||
|
#endif
|
||||||
|
|
||||||
#define BOT_REPLY_LEN 120 // well under the 184 B packet payload
|
#define BOT_REPLY_LEN 120 // well under the 184 B packet payload
|
||||||
#define BOT_TEXT_LEN 160 // inbound text, after the "<sender>: " prefix
|
#define BOT_TEXT_LEN 160 // inbound text, after the "<sender>: " prefix
|
||||||
@@ -75,6 +87,8 @@ void MyMesh::botInit() {
|
|||||||
bot_next_reply_at = 0;
|
bot_next_reply_at = 0;
|
||||||
bot_replies_sent = 0;
|
bot_replies_sent = 0;
|
||||||
bot_ignored = 0;
|
bot_ignored = 0;
|
||||||
|
bot_throttled = 0;
|
||||||
|
memset(bot_peers, 0, sizeof(bot_peers));
|
||||||
bot_last_reply_secs = 0;
|
bot_last_reply_secs = 0;
|
||||||
memset(&bot_channel, 0, sizeof(bot_channel));
|
memset(&bot_channel, 0, sizeof(bot_channel));
|
||||||
|
|
||||||
@@ -298,6 +312,30 @@ void MyMesh::botSendReply(const mesh::Packet* pkt, const char* text) {
|
|||||||
bot_last_reply_secs = getRTCClock()->getCurrentTime();
|
bot_last_reply_secs = getRTCClock()->getCurrentTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Per-sender cooldown over a tiny LRU. Returns true and CLAIMS the window when
|
||||||
|
// this sender may be answered now.
|
||||||
|
bool MyMesh::botPeerAllowed(const char* name) {
|
||||||
|
uint32_t h = 2166136261u; // FNV-1a over the lowercased name
|
||||||
|
for (const char* p = name; *p; p++) h = (h ^ (uint8_t)tolower((unsigned char)*p)) * 16777619u;
|
||||||
|
if (h == 0) h = 1; // 0 marks a free slot
|
||||||
|
|
||||||
|
int free_slot = -1, oldest = 0;
|
||||||
|
for (int i = 0; i < BOT_PEER_SLOTS; i++) {
|
||||||
|
if (bot_peers[i].name_hash == h) {
|
||||||
|
if (!millisHasNowPassed(bot_peers[i].next_at)) return false; // still cooling
|
||||||
|
bot_peers[i].next_at = futureMillis(BOT_REPLY_COOLDOWN_MS);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (bot_peers[i].name_hash == 0 && free_slot < 0) free_slot = i;
|
||||||
|
// signed compare = wraparound-safe "which window expires soonest"
|
||||||
|
if ((long)(bot_peers[i].next_at - bot_peers[oldest].next_at) < 0) oldest = i;
|
||||||
|
}
|
||||||
|
int slot = (free_slot >= 0) ? free_slot : oldest;
|
||||||
|
bot_peers[slot].name_hash = h;
|
||||||
|
bot_peers[slot].next_at = futureMillis(BOT_REPLY_COOLDOWN_MS);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::GroupChannel& channel,
|
void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::GroupChannel& channel,
|
||||||
uint8_t* data, size_t len) {
|
uint8_t* data, size_t len) {
|
||||||
if (type != PAYLOAD_TYPE_GRP_TXT) return; // GRP_DATA (telemetry etc.) is not ours
|
if (type != PAYLOAD_TYPE_GRP_TXT) return; // GRP_DATA (telemetry etc.) is not ours
|
||||||
@@ -340,11 +378,21 @@ void MyMesh::onGroupDataRecv(mesh::Packet* packet, uint8_t type, const mesh::Gro
|
|||||||
snprintf(reply, sizeof(reply), "pong (%s)", path);
|
snprintf(reply, sizeof(reply), "pong (%s)", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Global floor first: it is the airtime guarantee and must hold regardless of
|
||||||
|
// how many distinct senders are asking.
|
||||||
if (bot_next_reply_at != 0 && !millisHasNowPassed(bot_next_reply_at)) {
|
if (bot_next_reply_at != 0 && !millisHasNowPassed(bot_next_reply_at)) {
|
||||||
MESH_DEBUG_PRINTLN("bot: reply suppressed, cooldown");
|
MESH_DEBUG_PRINTLN("bot: reply suppressed, global gap");
|
||||||
|
bot_throttled++;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bot_next_reply_at = futureMillis(BOT_REPLY_COOLDOWN_MS);
|
// Then this sender's own window. Name-based, so spoofable -- fine, this is a
|
||||||
|
// politeness throttle and channel messages carry no identity anyway.
|
||||||
|
if (!botPeerAllowed(sep ? text : "")) {
|
||||||
|
MESH_DEBUG_PRINTLN("bot: reply suppressed, sender cooldown");
|
||||||
|
bot_throttled++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bot_next_reply_at = futureMillis(BOT_MIN_GAP_MS);
|
||||||
if (bot_next_reply_at == 0) bot_next_reply_at = 1; // 0 means "not armed"
|
if (bot_next_reply_at == 0) bot_next_reply_at = 1; // 0 means "not armed"
|
||||||
|
|
||||||
botSendReply(packet, reply);
|
botSendReply(packet, reply);
|
||||||
@@ -360,11 +408,12 @@ bool MyMesh::botHandleCommand(const char* command, char* reply) {
|
|||||||
strcpy(reply, "bot: DISABLED (bad BOT_CHANNEL_PSK)");
|
strcpy(reply, "bot: DISABLED (bad BOT_CHANNEL_PSK)");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
sprintf(reply, "bot: chan %02X, trigger '%s', cmds '%s', ignore '%s', cooldown %d ms, replies %u, ignored %u, last %u",
|
sprintf(reply, "bot: chan %02X, trigger '%s', cmds '%s', ignore '%s', cooldown %d ms/odesilatel + %d ms floor, replies %u, throttled %u, ignored %u, last %u",
|
||||||
(uint32_t)bot_channel.hash[0], BOT_TRIGGER,
|
(uint32_t)bot_channel.hash[0], BOT_TRIGGER,
|
||||||
BOT_COMMANDS[0] ? BOT_COMMANDS : "(vse)",
|
BOT_COMMANDS[0] ? BOT_COMMANDS : "(vse)",
|
||||||
BOT_IGNORE_SENDERS[0] ? BOT_IGNORE_SENDERS : "(nikdo)",
|
BOT_IGNORE_SENDERS[0] ? BOT_IGNORE_SENDERS : "(nikdo)",
|
||||||
(int)BOT_REPLY_COOLDOWN_MS, (uint32_t)bot_replies_sent,
|
(int)BOT_REPLY_COOLDOWN_MS, (int)BOT_MIN_GAP_MS,
|
||||||
|
(uint32_t)bot_replies_sent, (uint32_t)bot_throttled,
|
||||||
(uint32_t)bot_ignored, (uint32_t)bot_last_reply_secs);
|
(uint32_t)bot_ignored, (uint32_t)bot_last_reply_secs);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -211,11 +211,19 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
|||||||
uint8_t pktfeed_stage_len; // 0 = nothing staged
|
uint8_t pktfeed_stage_len; // 0 = nothing staged
|
||||||
#endif
|
#endif
|
||||||
#ifdef BOT_CHANNEL_PSK
|
#ifdef BOT_CHANNEL_PSK
|
||||||
|
// Default zde, ne az v ChannelBot.h -- ten se includuje na konci MyMesh.cpp,
|
||||||
|
// tedy dlouho po teto deklaraci pole.
|
||||||
|
#ifndef BOT_PEER_SLOTS
|
||||||
|
#define BOT_PEER_SLOTS 8
|
||||||
|
#endif
|
||||||
mesh::GroupChannel bot_channel; // 1 B hash + 32 B secret, derived from BOT_CHANNEL_PSK
|
mesh::GroupChannel bot_channel; // 1 B hash + 32 B secret, derived from BOT_CHANNEL_PSK
|
||||||
bool bot_channel_valid; // false = PSK malformed, bot silently off
|
bool bot_channel_valid; // false = PSK malformed, bot silently off
|
||||||
unsigned long bot_next_reply_at; // cooldown gate, 0 = not armed
|
unsigned long bot_next_reply_at; // cooldown gate, 0 = not armed
|
||||||
uint32_t bot_replies_sent;
|
uint32_t bot_replies_sent;
|
||||||
uint32_t bot_ignored; // zprav zahozenych podle BOT_IGNORE_SENDERS
|
uint32_t bot_ignored; // zprav zahozenych podle BOT_IGNORE_SENDERS
|
||||||
|
uint32_t bot_throttled; // odpovedi potlacenych cooldownem
|
||||||
|
struct BotPeer { uint32_t name_hash; unsigned long next_at; };
|
||||||
|
BotPeer bot_peers[BOT_PEER_SLOTS]; // per-odesilatel okno, LRU
|
||||||
uint32_t bot_last_reply_secs; // RTC time of the last reply, 0 = never
|
uint32_t bot_last_reply_secs; // RTC time of the last reply, 0 = never
|
||||||
#endif
|
#endif
|
||||||
CayenneLPP telemetry;
|
CayenneLPP telemetry;
|
||||||
@@ -257,6 +265,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
|||||||
int botCommandReply(char* out, int max_len, const char* cmd, const mesh::Packet* pkt);
|
int botCommandReply(char* out, int max_len, const char* cmd, const mesh::Packet* pkt);
|
||||||
int botScanCommands(char* out, int max_len, const char* text, const mesh::Packet* pkt);
|
int botScanCommands(char* out, int max_len, const char* text, const mesh::Packet* pkt);
|
||||||
void botSendReply(const mesh::Packet* pkt, const char* text);
|
void botSendReply(const mesh::Packet* pkt, const char* text);
|
||||||
|
bool botPeerAllowed(const char* name);
|
||||||
bool botHandleCommand(const char* command, char* reply);
|
bool botHandleCommand(const char* command, char* reply);
|
||||||
#endif
|
#endif
|
||||||
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood);
|
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood);
|
||||||
|
|||||||
Reference in new issue
Block a user