API: utils/rateLimit

better-web-search-mcp


better-web-search-mcp / utils/rateLimit

utils/rateLimit#

Classes#

CoolingDownError#

Defined in: src/utils/rateLimit.ts:29

A provider was asked for something while it is cooling down.

Extends#

  • Error

Constructors#

Constructor#

new CoolingDownError(name, remainingMs): CoolingDownError

Defined in: src/utils/rateLimit.ts:33

Parameters#
ParameterType
namestring
remainingMsnumber
Returns#

CoolingDownError

Overrides#

Error.constructor

Properties#

PropertyModifierTypeDescriptionInherited fromDefined in
cause?publicunknown-Error.causenode_modules/typescript/lib/lib.es2022.error.d.ts:26
messagepublicstring-Error.messagenode_modules/typescript/lib/lib.es5.d.ts:1077
namepublicstring-Error.namenode_modules/typescript/lib/lib.es5.d.ts:1076
remainingMsreadonlynumberMilliseconds remaining before the provider is tried again.-src/utils/rateLimit.ts:31
stack?publicstring-Error.stacknode_modules/typescript/lib/lib.es5.d.ts:1078
stackTraceLimitstaticnumberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.Error.stackTraceLimitnode_modules/@types/node/globals.d.ts:68

Methods#

captureStackTrace()#

static captureStackTrace(targetObject, constructorOpt?): void

Defined in: node_modules/@types/node/globals.d.ts:52

Creates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called.

const myObject = {};
Error.captureStackTrace(myObject);
myObject.stack;  // Similar to `new Error().stack`

The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}.

The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace.

The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance:

function a() {
  b();
}

function b() {
  c();
}

function c() {
  // Create an error without stack trace to avoid calculating the stack trace twice.
  const { stackTraceLimit } = Error;
  Error.stackTraceLimit = 0;
  const error = new Error();
  Error.stackTraceLimit = stackTraceLimit;

  // Capture the stack trace above function b
  Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace
  throw error;
}

a();
Parameters#
ParameterType
targetObjectobject
constructorOpt?Function
Returns#

void

Inherited from#

Error.captureStackTrace

prepareStackTrace()#

static prepareStackTrace(err, stackTraces): any

Defined in: node_modules/@types/node/globals.d.ts:56

Parameters#
ParameterType
errError
stackTracesCallSite[]
Returns#

any

See#

https://v8.dev/docs/stack-trace-api#customizing-stack-traces

Inherited from#

Error.prepareStackTrace


RateLimiter#

Defined in: src/utils/rateLimit.ts:54

Serializes work for one provider, spacing it and honouring a cooldown.

Scheduling is a chain rather than a timestamp check: concurrent callers each await the previous one, so N parallel requests are spread across N intervals instead of all seeing the same “last call” time and going at once.

Constructors#

Constructor#

new RateLimiter(name, intervalMs?): RateLimiter

Defined in: src/utils/rateLimit.ts:67

Parameters#
ParameterTypeDefault value
namestringundefined
intervalMsnumberDEFAULT_INTERVAL_MS
Returns#

RateLimiter

Properties#

PropertyModifierTypeDescriptionDefined in
namereadonlystringProvider name, used in error messages.src/utils/rateLimit.ts:56

Methods#

clearCooldown()#

clearCooldown(): void

Defined in: src/utils/rateLimit.ts:102

Clear the cooldown, used when a request succeeds again.

Returns#

void

cooldownRemaining()#

cooldownRemaining(now?): number

Defined in: src/utils/rateLimit.ts:83

Milliseconds until the cooldown lifts, or 0 if it is not active.

Parameters#
ParameterType
nownumber
Returns#

number

isCoolingDown()#

isCoolingDown(now?): boolean

Defined in: src/utils/rateLimit.ts:78

Whether this provider is currently refusing work.

Parameters#
ParameterType
nownumber
Returns#

boolean

schedule()#

schedule<T>(fn): Promise<T>

Defined in: src/utils/rateLimit.ts:112

Run fn once the provider is due another request.

Throws CoolingDownError without running fn if the provider is in a cooldown, so a blocked provider costs nothing rather than a timeout.

Type Parameters#
Type Parameter
T
Parameters#
ParameterType
fn() => Promise<T>
Returns#

Promise<T>

setInterval()#

setInterval(intervalMs): void

Defined in: src/utils/rateLimit.ts:73

Change the spacing. Used by configuration and by tests.

Parameters#
ParameterType
intervalMsnumber
Returns#

void

startCooldown()#

startCooldown(ms?, now?): void

Defined in: src/utils/rateLimit.ts:94

Stop sending requests for a while.

Parameters#
ParameterTypeDefault valueDescription
msnumberDEFAULT_COOLDOWN_MSHow long to wait. Callers pass a Retry-After value when the provider supplied one, so an explicit instruction is preferred over the default guess.
nownumber...-
Returns#

void

Variables#

DEFAULT_COOLDOWN_MS#

const DEFAULT_COOLDOWN_MS: 60000 = 60_000

Defined in: src/utils/rateLimit.ts:26

How long a provider is skipped after refusing, in milliseconds.


DEFAULT_INTERVAL_MS#

const DEFAULT_INTERVAL_MS: 1000 = 1_000

Defined in: src/utils/rateLimit.ts:23

Default spacing between requests to one provider, in milliseconds.

Functions#

configuredIntervalMs()#

configuredIntervalMs(env?): number

Defined in: src/utils/rateLimit.ts:153

Read the configured spacing.

BETTER_WEB_SEARCH_RATE_LIMIT_MS=0 disables pacing, which is what the test suite does so unit tests are not slowed by real waits.

Parameters#

ParameterTypeDefault value
envProcessEnvprocess.env

Returns#

number


limiterFor()#

limiterFor(name, intervalMs?): RateLimiter

Defined in: src/utils/rateLimit.ts:165

The limiter for a provider, created on first use.

Parameters#

ParameterType
namestring
intervalMs?number

Returns#

RateLimiter


resetRateLimiters()#

resetRateLimiters(): void

Defined in: src/utils/rateLimit.ts:177

Drop every limiter. Tests use this to avoid state leaking between cases.

Returns#

void