Skip to main content
Glama
rate-limiter.ts1.34 kB
export class RateLimiter { private requests: number[] = []; private maxRequests: number; private windowMs: number; private enabled: boolean; constructor(maxRequests: number, windowMs: number, enabled: boolean = true) { this.maxRequests = maxRequests; this.windowMs = windowMs; this.enabled = enabled; } checkLimit(): void { if (!this.enabled) return; const now = Date.now(); const windowStart = now - this.windowMs; this.requests = this.requests.filter(timestamp => timestamp > windowStart); if (this.requests.length >= this.maxRequests) { const oldestRequest = this.requests[0]; const resetIn = Math.ceil((oldestRequest + this.windowMs - now) / 1000); throw new Error( `Rate limit exceeded. Maximum ${this.maxRequests} requests per ${this.windowMs / 1000} seconds. ` + `Try again in ${resetIn} seconds.` ); } this.requests.push(now); } reset(): void { this.requests = []; } getStats(): { current: number; max: number; windowMs: number } { const now = Date.now(); const windowStart = now - this.windowMs; const currentRequests = this.requests.filter(timestamp => timestamp > windowStart).length; return { current: currentRequests, max: this.maxRequests, windowMs: this.windowMs }; } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/bluwork/postgres-scout-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server