feat(chat): sliding window message frequency and flood limiter - #138
feat(chat): sliding window message frequency and flood limiter#138Rodrigoue9 wants to merge 8 commits into
Conversation
| * Bitcoindefi/OpenAO - Chat Flood Rate Limiter | ||
| */ | ||
| export class ChatRateLimiter { | ||
| private userMessageTimestamps = new Map<string, number[]>(); |
There was a problem hiding this comment.
⚠️ Performance: ChatRateLimiter map grows unbounded, never evicts users
userMessageTimestamps retains an entry for every userId that ever sent a message and is never cleaned up, even after a user's timestamps all age out of the window. On a long-running multiplayer server this map grows without bound (the exact buffer saturation the limiter is meant to prevent). Add eviction: after filtering, delete the key when the array is empty, and/or periodically sweep stale entries.
Persist the filtered array so stale timestamps don't accumulate; consider a periodic sweep that deletes keys whose arrays are empty to bound memory.:
const timestamps = (this.userMessageTimestamps.get(userId) || []).filter(t => now - t < this.windowMs);
if (timestamps.length >= this.maxMessagesPerWindow) {
this.userMessageTimestamps.set(userId, timestamps);
return { allowed: false, remaining: 0 };
}
timestamps.push(now);
this.userMessageTimestamps.set(userId, timestamps);
return { allowed: true, remaining: this.maxMessagesPerWindow - timestamps.length };
- Apply fix
Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎
Code Review
|
| Auto-apply | Compact |
|
|
Important
Your trial ends in 7 days — upgrade now to keep code review, CI analysis, auto-apply, custom automations, and more.
Was this helpful? React with 👍 / 👎 | Gitar
Chat Flood Limiter
Ready for review! 🚀