Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(automation): stop rescanning the cooldown map on every reply

Yudhi Armyndharisfc724f284060
diff --git a/src/modules/automation/automation-rules.service.spec.ts b/src/modules/automation/automation-rules.service.spec.ts
index dafc60736..f39ebc173 100644
--- a/src/modules/automation/automation-rules.service.spec.ts
+++ b/src/modules/automation/automation-rules.service.spec.ts
@@ -309,6 +309,28 @@ describe('AutomationRulesService', () => {
expect(sends.map(s => s.chatId)).toEqual(['[email protected]', '[email protected]']);
});
+ it('cooldown: a sweep that frees nothing is not repeated on the next reply', async () => {
+ await service.create('sessA', { name: 'all', replyText: 'ack', cooldownSeconds: 60 });
+ const cooldowns = (service as unknown as { cooldowns: Map<string, number> }).cooldowns;
+ for (let i = 0; i < 10_000; i++) cooldowns.set(`pad:${i}`, Date.now());
+ const entries = cooldowns[Symbol.iterator].bind(cooldowns);
+ let scans = 0;
+ cooldowns[Symbol.iterator] = () => {
+ scans++;
+ return entries();
+ };
+
+ for (let i = 0; i < 5; i++) {
+ await service.evaluateInbound(
+ 'sessA',
+ inbound({ id: `wamid.${i}`, chatId: `62800${i}@c.us`, from: `62800${i}@c.us` }),
+ );
+ }
+
+ expect(sends).toHaveLength(5);
+ expect(scans).toBe(1);
+ });
+
it('cooldownSeconds 0 disables the quiet period', async () => {
await service.create('sessA', { name: 'all', replyText: 'ack', cooldownSeconds: 0 });
diff --git a/src/modules/automation/automation-rules.service.ts b/src/modules/automation/automation-rules.service.ts
index 64415248c..cb38db277 100644
--- a/src/modules/automation/automation-rules.service.ts
+++ b/src/modules/automation/automation-rules.service.ts
@@ -16,7 +16,11 @@ import {
UpdateAutomationRuleDto,
} from './dto/automation-rule.dto';
-/** Entries above this size trigger a sweep of expired cooldowns before inserting the next one. */
+/**
+ * The cooldown map is swept of entries older than the longest allowed cooldown once it reaches this
+ * size, and after a sweep only once it doubles again, so a sweep that frees nothing is not repeated
+ * on every reply.
+ */
const COOLDOWN_SWEEP_THRESHOLD = 10_000;
/**
@@ -55,6 +59,7 @@ export class AutomationRulesService {
* only drops an entry older than the longest cooldown a rule may have. Per-process.
*/
private readonly cooldowns = new Map<string, number>();
+ private nextCooldownSweepAt = COOLDOWN_SWEEP_THRESHOLD;
private messagePort?: PluginMessagePort;
@@ -256,10 +261,11 @@ export class AutomationRulesService {
private enterCooldown(rule: AutomationRule, chatId: string): void {
if (!rule.cooldownSeconds) return;
const now = Date.now();
- if (this.cooldowns.size >= COOLDOWN_SWEEP_THRESHOLD) {
+ if (this.cooldowns.size >= this.nextCooldownSweepAt) {
for (const [key, firedAt] of this.cooldowns) {
if (firedAt + AUTOMATION_COOLDOWN_MAX_SECONDS * 1000 <= now) this.cooldowns.delete(key);
}
+ this.nextCooldownSweepAt = Math.max(COOLDOWN_SWEEP_THRESHOLD, this.cooldowns.size * 2);
}
this.cooldowns.set(`${rule.id}:${chatId}`, now);
}