Skip to content

Rate Limiter

The rate limiter controls the number of outgoing requests per time window, preventing you from accidentally overwhelming upstream services or triggering their rate-limit responses.


Basic usage

typescript
// Max 100 requests per minute — reject excess immediately
client.rateLimit({ permitLimit: 100, windowMs: 60_000 })

With queuing

typescript
// Queue excess requests; wait up to 5 s for a token
client.rateLimit({
  permitLimit: 100,
  windowMs: 60_000,
  queueRequests: true,
  queueTimeoutMs: 5_000,
})

Configuration

OptionTypeDefaultDescription
permitLimitnumberMax requests per window
windowMsnumberWindow length in ms
queueRequestsbooleanfalseQueue excess instead of rejecting
queueTimeoutMsnumberundefinedMax wait time for a queued token

Error types

ErrorWhen
Error('Rate limit exceeded')Limit hit and queueRequests is false
Error('Rate limit queue timeout')Queued request waited > queueTimeoutMs

With RetryAfterStrategy

Pair the rate limiter with RetryAfterStrategy to back off exactly as long as the server requests:

typescript
import { RetryAfterStrategy } from 'super-http'

client
  .rateLimit({ permitLimit: 100, windowMs: 60_000 })
  .retry(5, new RetryAfterStrategy())
// Server says "Retry-After: 30" → client waits 30 s exactly

Observability

typescript
client
  .on({
    onRateLimitReject: ({ permitLimit, windowMs }) =>
      metrics.increment('rate_limit.rejected', { permitLimit, windowMs }),
  })
  .rateLimit({ permitLimit: 200, windowMs: 60_000 })

Sizing

Upstream SLARecommended permitLimit
60 req/min55 (leave 8% headroom)
1000 req/min900
UnknownStart at 50 and tune up

TIP

Always leave a small headroom below the upstream limit to account for clock drift and bursts from other clients sharing the same API key.

Released under the MIT License.