项目作者: yourtion

项目描述 :
easy use rate limiter
高级语言: TypeScript
项目地址: git://github.com/yourtion/node-elimiter.git
创建时间: 2018-07-26T07:56:55Z
项目社区:https://github.com/yourtion/node-elimiter

开源协议:

下载


NPM version
build status
Test coverage
David deps
node version
npm download
npm license

node-elimiter

Easy rate limiter.

Install

  1. $ npm install elimiter --save

How to use

  1. import Limiter from "elimiter";
  2. const db = new Redis();
  3. // limit 5 call in 60s (60*1000ms)
  4. const limiter = new Limiter(db, { id: "ip", max: 5, duration: 60 * 1000 });
  5. const res = await limit.get(); // limit.get(req.ip), limit(userid)
  6. if (res.remaining < 1) throw new Errror("out of limit");
  7. // continue
  1. // Get with reset and resetMs
  2. const res = await limit.get({ reset: true });
  3. console.log(res);
  4. // { count: 0, remaining: 5, total: 5, reset: 1546438588, resetMs: 1546438588062 }
  5. if (res.remaining < 1) throw new Errror("out of limit");
  6. // continue

Benchmark

Platform info:

  • Linux 4.4.0-101-generic x64
  • Node.JS: 10.15.0
  • V8: 6.8.275.32-node.45
    Intel(R) Xeon(R) CPU @ 2.30GHz × 2

3 tests success:

test rps ns/op spent
limit.get({ reset: true }) 10172.0 98309.4 2.012s
limit.get() 11529.1 86736.9 2.009s