Skip to content

配额配置

1) DSL 字符串

直接向 Throttled 传入可读的配额字符串:

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

const throttle = new Throttled({
  key: '/api/ping',
  quota: '100/s',
  // quota: '100/s burst 200',
  // quota: '100 per second',
  // quota: '100 per second burst 200',
});

console.log(throttle.limit());

支持的模式:

  • n / unit
  • n / unit burst <burst>
  • n per unit
  • n per unit burst <burst>

burst 表示突发容量,用于 Token Bucket、Leaky Bucket、GCRA。省略时默认等于 n

支持的时间单位:

规范单位短格式兼容格式示例
secondss, sec, secs, second, seconds100/s
minutemm, min, mins, minute, minutes60/m
hourhh, hr, hrs, hour, hours10/h
daydd, day, days5/d
weekww, wk, wks, week, weeks1/w

2) 编程方式

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

perSec(60);       // 60 req/sec
perMin(60);       // 60 req/min
perHour(60);      // 60 req/hour
perDay(60);       // 60 req/day
perWeek(60);      // 60 req/week

// 带 burst
perMin(60, 120);  // 60 req/min, burst 120

// 自定义周期
perDuration(120, 120, 150); // 120 req 每 120s, burst 150

MIT License — Port of throttled-py