feat(rfid): more rfid topics while monitoring the system run
This commit is contained in:
19
server/services/rfid/utils/rateLimit.ts
Normal file
19
server/services/rfid/utils/rateLimit.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
const lastRunMap = new Map();
|
||||
|
||||
/**
|
||||
* Returns true if the function with a given key was called within the last `seconds`.
|
||||
* @param {string} key - Unique key to track cooldown per function/context.
|
||||
* @param {number} seconds - Cooldown in seconds.
|
||||
* @returns {boolean} - true if should skip (still in cooldown), false if allowed.
|
||||
*/
|
||||
export function shouldSkipByCooldown(key: any, seconds: any) {
|
||||
const now = Date.now();
|
||||
const cooldown = seconds * 1000;
|
||||
|
||||
if (lastRunMap.has(key) && now - lastRunMap.get(key) < cooldown) {
|
||||
return true;
|
||||
}
|
||||
|
||||
lastRunMap.set(key, now);
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user