Download src/gemini/guard/ratelimit.ts from TTOPM/belel-protocol: direct link, hf CLI and curl.
- Browser
- Download file 608 Bytes
-
https://huggingface.co/TTOPM/belel-protocol/resolve/main/src/gemini/guard/ratelimit.ts
- Command line
-
hf download hf://TTOPM/belel-protocol/src/gemini/guard/ratelimit.ts
-
curl -L -o ratelimit.ts https://huggingface.co/TTOPM/belel-protocol/resolve/main/src/gemini/guard/ratelimit.ts
608 Bytes
| import cfg from "../config/gemini.config.json"; | |
| const bucket = new Map<string, { tokens: number; ts: number }>(); | |
| export function rateCheck(key: string) { | |
| const now = Date.now(); | |
| const limit = cfg.rateLimit.tokensPerMinute; | |
| const burst = cfg.rateLimit.burst; | |
| const win = 60_000; | |
| const entry = bucket.get(key) ?? { tokens: limit * burst, ts: now }; | |
| const refill = Math.floor((now - entry.ts) / win) * limit; | |
| entry.tokens = Math.min(entry.tokens + refill, limit * burst); | |
| entry.ts = now; | |
| if (entry.tokens <= 0) return false; | |
| entry.tokens -= 1000; | |
| bucket.set(key, entry); | |
| return true; | |
| } | |