Skip to content

RLWrapperTimeouts

Roman edited this page Nov 9, 2025 · 1 revision

RLWrapperTimeouts

It wraps any limiter by adding timeout functionality. If the wrapped limiter operation takes longer than the specified timeout, it will reject with an error.

Usage without insuranceLimiter

const limiter = new RateLimiterValkey({
  storeClient: valkeyClient,
  points: 1,
  duration: 1,
});

const limiterWrapped = new RLWrapperTimeouts({
  limiter,
  timeoutMs: 1000,
});

If the operation takes longer than timeoutMs milliseconds, it will reject with "Operation timed out" error.

Usage with insuranceLimiter

When a timeout occurs or the wrapped limiter fails, the wrapper will automatically fallback to the insuranceLimiter if it's set:

const primaryLimiter = new RateLimiterValkey({
  storeClient: valkeyClient,
  points: 1,
  duration: 1,
});

const insuranceLimiter = new RateLimiterMemory({
  points: 1,
  duration: 1,
});

const limiterWrapped = new RLWrapperTimeouts({
  limiter: primaryLimiter,
  timeoutMs: 500,
  insuranceLimiter: insuranceLimiter,
});

If primaryLimiter times out or fails, it will automatically use insuranceLimiter.

Automatic insuranceLimiter inheritance

If the wrapped limiter is also a RateLimiterInsuredAbstract (like RateLimiterStoreAbstract), the wrapper will automatically inherit its insuranceLimiter:

const limiter = new RateLimiterValkey({
  storeClient: valkeyClient,
  points: 1,
  duration: 1,
  insuranceLimiter: new RateLimiterMemory({
    points: 1,
    duration: 1,
  }),
});

const limiterWrapped = new RLWrapperTimeouts({
  limiter,
  timeoutMs: 1000,
});

limiterWrapped automatically has access to the insuranceLimiter from the wrapped limiter. If timeout occurs, it will use the insuranceLimiter.

Wrapped limiter has the same methods as any other limiter from this package.

Clone this wiki locally