import { scope, TokenBucket } from 'go-go-scope';
async function rateLimitedAPI() {
await using s = scope();
// Allow 100 requests/second with burst capacity of 100
const bucket = s.tokenBucket({
capacity: 100,
refillRate: 100 // tokens per second
});
async function makeRequest(data: unknown) {
// Wait for token to be available
return bucket.acquire(1, async () => {
return fetch('/api/endpoint', {
method: 'POST',
body: JSON.stringify(data),
});
});
}
// Make many requests - automatically rate limited
const requests = Array.from({ length: 1000 }, (_, i) =>
makeRequest({ id: i })
);
const results = await Promise.all(requests);
console.log('Completed:', results.length);
}