UNPKG

random-org-mcp-server

Version:

MCP Server for api.random.org integration - Generate true random numbers, strings, UUIDs and more

32 lines 1.12 kB
export class RateLimiter { constructor(requestsPerSecond, burstSize) { this.maxTokens = burstSize; this.tokens = burstSize; this.refillRate = requestsPerSecond; this.lastRefill = Date.now(); } async waitForToken() { this.refillTokens(); if (this.tokens >= 1) { this.tokens -= 1; return; } // Calculate how long to wait for the next token const timeToWait = (1 / this.refillRate) * 1000; // Convert to milliseconds await new Promise(resolve => setTimeout(resolve, timeToWait)); // Try again after waiting return this.waitForToken(); } refillTokens() { const now = Date.now(); const timePassed = (now - this.lastRefill) / 1000; // Convert to seconds const tokensToAdd = timePassed * this.refillRate; this.tokens = Math.min(this.maxTokens, this.tokens + tokensToAdd); this.lastRefill = now; } getAvailableTokens() { this.refillTokens(); return Math.floor(this.tokens); } } //# sourceMappingURL=rateLimiter.js.map