Skip to content

Commit 0227ff1

Browse files
committed
fix(discord): cap dedup set on thread delete, add #[cfg(test)] to test mod
- Replace retain(|_| true) no-op with a size-capped clear: when threaded_message_ids exceeds MAX_DEDUP_MSG_IDS (2000) it is cleared. MESSAGE_UPDATE embed events arrive within seconds so old entries are always safe to discard; prevents unbounded growth on busy servers. - Add #[cfg(test)] to mod tests so empty_threads() helper is only compiled in test mode — removes the need for #[allow(dead_code)].
1 parent 525d7d8 commit 0227ff1

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

crates/openfang-channels/src/discord.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ const DISCORD_API_BASE: &str = "https://discord.com/api/v10";
2222
const MAX_BACKOFF: Duration = Duration::from_secs(60);
2323
const INITIAL_BACKOFF: Duration = Duration::from_secs(1);
2424
const DISCORD_MSG_LIMIT: usize = 2000;
25+
/// Maximum number of seen message IDs kept in the dedup set.
26+
/// MESSAGE_UPDATE (embed resolution) events arrive within seconds of the
27+
/// original CREATE; entries older than this cap are safe to discard.
28+
const MAX_DEDUP_MSG_IDS: usize = 2_000;
2529

2630
/// Discord Gateway opcodes.
2731
mod opcode {
@@ -578,7 +582,13 @@ impl ChannelAdapter for DiscordAdapter {
578582
// next message in the parent channel is treated fresh.
579583
if let Some(tid) = d["id"].as_str() {
580584
created_thread_ids.write().await.remove(tid);
581-
threaded_message_ids.write().await.retain(|_| true); // keep others
585+
// Prune the dedup set to prevent unbounded growth.
586+
// Entries older than MAX_DEDUP_MSG_IDS are safe to
587+
// discard — embed UPDATE events arrive within seconds.
588+
let mut ids = threaded_message_ids.write().await;
589+
if ids.len() > MAX_DEDUP_MSG_IDS {
590+
ids.clear();
591+
}
582592
debug!("Discord thread/channel deleted: {tid}");
583593
}
584594
}
@@ -895,11 +905,11 @@ fn thread_name_from_message(message: &ChannelMessage) -> String {
895905
}
896906
}
897907

908+
#[cfg(test)]
898909
mod tests {
899910
use super::*;
900911

901912
/// Convenience helper: empty thread-tracking map for tests that don't exercise threading.
902-
#[allow(dead_code)]
903913
fn empty_threads() -> Arc<RwLock<HashMap<String, String>>> {
904914
Arc::new(RwLock::new(HashMap::new()))
905915
}

0 commit comments

Comments
 (0)