typescriptintermediate
Node.js Token Bucket Rate Limiter
Implement an in-memory token bucket rate limiter for controlling API request throughput.
typescriptPress ⌘/Ctrl + Shift + C to copy
class TokenBucket {
private tokens: number;
private lastRefill: number;
constructor(
private maxTokens: number,
private refillRate: number // tokens per second
) {
this.tokens = maxTokens;
this.lastRefill = Date.now();
}
private refill(): void {
const now = Date.now();
const elapsed = (now - this.lastRefill) / 1000;
this.tokens = Math.min(
this.maxTokens,
this.tokens + elapsed * this.refillRate
);
this.lastRefill = now;
}
consume(tokens = 1): boolean {
this.refill();
if (this.tokens >= tokens) {
this.tokens -= tokens;
return true;
}
return false;
}
get remaining(): number {
this.refill();
return Math.floor(this.tokens);
}
}
// Per-IP rate limiter
const buckets = new Map<string, TokenBucket>();
function getRateLimiter(key: string): TokenBucket {
if (!buckets.has(key)) {
buckets.set(key, new TokenBucket(100, 10)); // 100 burst, 10/sec
}
return buckets.get(key)!;
}
// Usage in HTTP handler
function handleRequest(ip: string): { allowed: boolean; remaining: number } {
const limiter = getRateLimiter(ip);
const allowed = limiter.consume();
return { allowed, remaining: limiter.remaining };
}Use Cases
- Protecting APIs from abuse and DDoS
- Fair usage enforcement across clients
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Webhook Handler with Signature Verification
Process incoming webhooks with HMAC signature verification, replay protection, and idempotency.
Best for: GitHub/Stripe webhook processing
#nodejs#webhook
typescriptadvanced
Sliding Window Rate Limiter
Implements sliding window rate limiting that distributes limits more evenly than fixed windows.
Best for: API rate limiting
#rate-limiting#security
typescriptintermediate
Node.js Crypto Utility Functions
Common cryptographic operations: hashing, HMAC, encryption, random tokens, and password hashing.
Best for: Secure password storage and verification
#nodejs#crypto
typescriptintermediate
Request Validation Schema Builder
Build a lightweight request validation layer with type inference for API endpoints.
Best for: API request body validation
#nodejs#validation