Skip to content

API 参考

主接口

Throttled(同步)

typescript
import { Throttled } from 'throttled-nodejs';

构造函数 new Throttled(options?)

参数类型默认值说明
keystring | nullnull限流标识
timeoutnumber | null-1(非阻塞)最大等待时间(秒)
usingRateLimiterTypeT | nullTOKEN_BUCKET算法类型
quotaQuota | string | null60/m配额
storeStoreP | null全局 MemoryStore存储后端
costnumber1每次请求消耗
hooksHook[] | null[]Hook 中间件链

方法

  • limit(key?, cost?, timeout?)RateLimitResult — 执行限流检查
  • peek(key)RateLimitState — 查询状态(不消耗)
  • enter()RateLimitResult — 上下文管理器(拒绝时抛 LimitedError
  • exit() — 退出上下文

静态方法

  • Throttled.decorate(options)MethodDecorator — 创建装饰器

AsyncThrottled(异步)

typescript
import { AsyncThrottled } from 'throttled-nodejs';

参数与 Throttled 相同,所有方法返回 Promise

  • limit(key?, cost?, timeout?)Promise<RateLimitResult>
  • peek(key)Promise<RateLimitState>
  • enter()Promise<RateLimitResult>

RateLimitResult

属性类型说明
limitedbooleantrue 表示请求被拒绝
stateRateLimitState惰性计算的状态快照

RateLimitState

属性类型说明
limitnumber初始状态最大请求数
remainingnumber当前剩余请求数
resetAfternumber距重置到初始状态的秒数
retryAfternumber建议重试等待秒数(0 = 已允许)

Rate / Quota

typescript
import { Rate, Quota, perSec, perMin, perHour, perDay, perWeek, perDuration } from 'throttled-nodejs';

Rate

属性类型说明
periodnumber时间周期(秒)
limitnumber周期内最大请求数

Quota

属性类型说明
rateRate基础速率
burstnumber突发容量
periodSecnumber周期秒数(向下取整)
emissionIntervalnumber请求间隔(秒)
fillRatenumber每秒补充速率

工厂函数

函数说明
perSec(limit, burst?)每秒请求数
perMin(limit, burst?)每分钟请求数
perHour(limit, burst?)每小时请求数
perDay(limit, burst?)每天请求数
perWeek(limit, burst?)每周请求数
perDuration(sec, limit, burst?)自定义周期(秒)

RateLimiterType

typescript
import { RateLimiterType } from 'throttled-nodejs';
常量
FIXED_WINDOW"fixed_window"
SLIDING_WINDOW"sliding_window"
TOKEN_BUCKET"token_bucket"
LEAKING_BUCKET"leaking_bucket"
GCRA"gcra"

Hooks

typescript
import { Hook, HookContext, buildHookChain } from 'throttled-nodejs';
import { AsyncHook } from 'throttled-nodejs';

Hook(同步)

typescript
abstract class Hook {
  abstract onLimit(
    callNext: () => RateLimitResult,
    context: HookContext,
  ): RateLimitResult;
}

AsyncHook(异步)

typescript
abstract class AsyncHook {
  abstract onLimit(
    callNext: () => Promise<RateLimitResult>,
    context: HookContext,
  ): Promise<RateLimitResult>;
}

HookContext

属性类型说明
keystring限流标识
costnumber请求消耗
algorithmstring算法名
storeTypestring存储后端类型

Store

typescript
import { MemoryStore, RedisStore } from 'throttled-nodejs';

MemoryStore

构造函数:new MemoryStore(options?)

参数类型默认值说明
MAX_SIZEnumber1024最大条目数,超出后 LRU 淘汰

RedisStore

构造函数:new RedisStore(server?, options?)

参数类型默认值说明
serverstring"redis://localhost:6379/0"Redis 连接 URL
optionsobject{}连接选项

支持 URL scheme:redis://rediss://redis+sentinel://redis+cluster://


异常

typescript
import { BaseThrottledError, SetUpError, DataError, StoreUnavailableError, LimitedError } from 'throttled-nodejs';
异常说明
BaseThrottledError所有限流异常的基类
SetUpError配置/初始化错误
DataError参数无效错误
StoreUnavailableError存储后端不可用
LimitedError超出限流(含 rateLimitResult 属性)

工具类

typescript
import { Timer, Benchmark, nowSec, nowMonoF, nowMs } from 'throttled-nodejs';

Timer

方法说明
enter()开始计时
exit()停止计时
decorate(fn)包装函数计时

Benchmark

方法说明
serial(task, batch, ...args)串行执行
concurrent(task, batch, workers?, ...args)并发执行
asyncSerial(task, batch, ...args)异步串行
asyncConcurrent(task, batch, workers?, ...args)异步并发
stats()打印统计
clear()重置数据

常量

typescript
import { StoreType } from 'throttled-nodejs';
常量
MEMORY"memory"
REDIS"redis"

OpenTelemetry Hooks

typescript
import { OTelHook, AsyncOTelHook } from 'throttled-nodejs';

详情见 OpenTelemetry 文档

MIT License — Port of throttled-py