/** 아주 작은 메모리 속 횟수 제한 (IP 별). 무차별 대입 · 요청 폭주로 로그인 · 등록을 막는 것을 줄인다 */ export class RateLimiter { private hits = new Map(); constructor( private max: number, private windowMs: number, ) {} /** 한 번 센다. 창 안에서 max 를 넘으면 false */ hit(key: string): boolean { const now = Date.now(); const list = (this.hits.get(key) ?? []).filter((t) => now - t < this.windowMs); if (list.length >= this.max) { this.hits.set(key, list); return false; } list.push(now); this.hits.set(key, list); if (this.hits.size > 10000) for (const [k, v] of this.hits) if (!v.some((t) => now - t < this.windowMs)) this.hits.delete(k); return true; } /** 세지 않고 막혔는지만 본다 */ blocked(key: string): boolean { const now = Date.now(); return (this.hits.get(key) ?? []).filter((t) => now - t < this.windowMs).length >= this.max; } }