From 7885cede659f3255be56f77c1eef2ada7387d6f1 Mon Sep 17 00:00:00 2001
From: chenyc <501753378@qq.com>
Date: 星期日, 22 三月 2026 16:23:21 +0800
Subject: [PATCH] 初始化项目
---
src/rateLimiter.js | 33 +++++++++++++++++++++++++++++++++
1 files changed, 33 insertions(+), 0 deletions(-)
diff --git a/src/rateLimiter.js b/src/rateLimiter.js
new file mode 100644
index 0000000..79d50fe
--- /dev/null
+++ b/src/rateLimiter.js
@@ -0,0 +1,33 @@
+class RateLimiter {
+ constructor() {
+ this.records = new Map(); // key -> lastTs
+ }
+
+ checkLimit(key, intervalMs) {
+ const now = Date.now();
+ const last = this.records.get(key) || 0;
+ const diff = now - last;
+ if (diff < intervalMs) {
+ return {
+ allowed: false,
+ waitMs: intervalMs - diff
+ };
+ }
+ this.records.set(key, now);
+ return { allowed: true, waitMs: 0 };
+ }
+
+ getStats() {
+ const result = [];
+ for (const [key, ts] of this.records.entries()) {
+ result.push({ key, lastAt: ts });
+ }
+ return result;
+ }
+
+ clear() {
+ this.records.clear();
+ }
+}
+
+module.exports = RateLimiter;
--
Gitblit v1.8.0