Skip to content

指定算法

通过 using 参数选择算法。

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

const throttle = new Throttled({
  using: RateLimiterType.FIXED_WINDOW,
  // using: RateLimiterType.SLIDING_WINDOW,
  // using: RateLimiterType.TOKEN_BUCKET,
  // using: RateLimiterType.LEAKING_BUCKET,
  // using: RateLimiterType.GCRA,
  quota: '1/m',
});

console.log(throttle.limit('key', 2).limited); // true

算法列表

算法RateLimiterType说明
Fixed WindowFIXED_WINDOW固定窗口计数器,窗口边界重置
Sliding WindowSLIDING_WINDOW基于滑动窗口日志
Token BucketTOKEN_BUCKET恒定速率补充令牌
Leaky BucketLEAKING_BUCKET恒定速率泄漏请求
GCRAGCRA通用信元速率算法

Async

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

const throttle = new AsyncThrottled({
  using: RateLimiterType.FIXED_WINDOW,
  quota: '1/m',
});

async function main() {
  console.log((await throttle.limit('key', 2)).limited); // true
}

main();

MIT License — Port of throttled-py