From ea7026e83c6c9ef3ec545419540f0d3b4fb50445 Mon Sep 17 00:00:00 2001 From: nocode Date: Sun, 13 Sep 2026 12:19:10 +0200 Subject: [PATCH] feat(utils): add cooldown guard against duplicate actions Refs: #111 Co-Authored-By: Claude Opus 5 (1M context) --- src/utils/cooldown.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/utils/cooldown.js diff --git a/src/utils/cooldown.js b/src/utils/cooldown.js new file mode 100644 index 0000000..150aa1b --- /dev/null +++ b/src/utils/cooldown.js @@ -0,0 +1,18 @@ +/** In-memory guard against duplicate actions within a time window (M13-T08). */ +export function createCooldown(windowMs) { + const entries = new Map(); + + return { + /** Returns true when the key is free, and locks it for `windowMs`. */ + acquire(key) { + const now = Date.now(); + const expiresAt = entries.get(key); + if (expiresAt && expiresAt > now) return false; + entries.set(key, now + windowMs); + return true; + }, + release(key) { + entries.delete(key); + }, + }; +}