API: utils/rateLimit
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#
| Parameter | Type |
|---|---|
name | string |
remainingMs | number |
Returns#
Overrides#
Error.constructor
Properties#
| Property | Modifier | Type | Description | Inherited from | Defined in |
|---|---|---|---|---|---|
cause? | public | unknown | - | Error.cause | node_modules/typescript/lib/lib.es2022.error.d.ts:26 |
message | public | string | - | Error.message | node_modules/typescript/lib/lib.es5.d.ts:1077 |
name | public | string | - | Error.name | node_modules/typescript/lib/lib.es5.d.ts:1076 |
remainingMs | readonly | number | Milliseconds remaining before the provider is tried again. | - | src/utils/rateLimit.ts:31 |
stack? | public | string | - | Error.stack | node_modules/typescript/lib/lib.es5.d.ts:1078 |
stackTraceLimit | static | number | The 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.stackTraceLimit | node_modules/@types/node/globals.d.ts:68 |
Methods#
captureStackTrace()#
staticcaptureStackTrace(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#
| Parameter | Type |
|---|---|
targetObject | object |
constructorOpt? | Function |
Returns#
void
Inherited from#
Error.captureStackTrace
prepareStackTrace()#
staticprepareStackTrace(err,stackTraces):any
Defined in: node_modules/@types/node/globals.d.ts:56
Parameters#
| Parameter | Type |
|---|---|
err | Error |
stackTraces | CallSite[] |
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#
| Parameter | Type | Default value |
|---|---|---|
name | string | undefined |
intervalMs | number | DEFAULT_INTERVAL_MS |
Returns#
Properties#
| Property | Modifier | Type | Description | Defined in |
|---|---|---|---|---|
name | readonly | string | Provider 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#
| Parameter | Type |
|---|---|
now | number |
Returns#
number
isCoolingDown()#
isCoolingDown(
now?):boolean
Defined in: src/utils/rateLimit.ts:78
Whether this provider is currently refusing work.
Parameters#
| Parameter | Type |
|---|---|
now | number |
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#
| Parameter | Type |
|---|---|
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#
| Parameter | Type |
|---|---|
intervalMs | number |
Returns#
void
startCooldown()#
startCooldown(
ms?,now?):void
Defined in: src/utils/rateLimit.ts:94
Stop sending requests for a while.
Parameters#
| Parameter | Type | Default value | Description |
|---|---|---|---|
ms | number | DEFAULT_COOLDOWN_MS | How long to wait. Callers pass a Retry-After value when the provider supplied one, so an explicit instruction is preferred over the default guess. |
now | number | ... | - |
Returns#
void
Variables#
DEFAULT_COOLDOWN_MS#
constDEFAULT_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#
constDEFAULT_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#
| Parameter | Type | Default value |
|---|---|---|
env | ProcessEnv | process.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#
| Parameter | Type |
|---|---|
name | string |
intervalMs? | number |
Returns#
resetRateLimiters()#
resetRateLimiters():
void
Defined in: src/utils/rateLimit.ts:177
Drop every limiter. Tests use this to avoid state leaking between cases.
Returns#
void