npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

traffic-throttler

v1.0.11

Published

A Node.js throttler, Commonly used to request throttling

Downloads

59

Readme

Introduce

A Node.js RateLimiter, it's core design based @nestjs/throttler Commonly used to request throttling.

It is sufficiently extensible, so you can implement IThrottler interface to redefine you strategy (now support a simple RawThrottler) and you can implement IStorage interface to redefine your storage (now support memory and redis).

介绍

这是一个 Node.js 实现的限流器 ,它的核心设计基于@nestjs/throttler, 一般用于 web 请求中限流。 它拥有足够的扩展性,你能够实现 IThrottler 接口来重新定义策略, 也能够实现 IStorage 接口重新定义存储方式(目前存储方式支持内存和redis)

Relative Package (关联包)

midway-throttler Midway.js 限流器

Install (安装)

npm i traffic-throttler --save

API

1. throttle constructor

const throttler = new RawThrottler({
  limit: number, //The amount of requests that are allowed within the ttl's time window.
  ttl: number,  //The amount of seconds of how many requests are allowed within this time. (seconds)
  storage?: IThrottlerStorageOption;  //The storage class to use where all the record will be stored in.
});

2. tryAcquire

tryAcquire: (option: IAcquireOption) => Promise<boolean>; //acquire token; if reach limit, return false; else if not reach limit, return true;

3. destory

destory: () => Promise<void>; //destory throttler instance

Example

Memory Storage Demo


async function sleep(time) {
  return new Promise((resolve: any) => {
    setTimeout(() => {
      resolve();
    }, time);
  });
}

//main function
async function main() {

  //create a throttler, storage in memory
  const throttler = new RawThrottler({
    limit: 3,
    ttl: 5,
    storage: {
      type: StorageTypeEnum.memory,
    },
  });

  const option: IAcquireOption = { key: 'test-key' };
  const ret: boolean[] = [];
  for (let i = 0, ilen = 5; i < ilen; i++) {
    //user tryAcquire to getToken
    ret.push(await throttler.tryAcquire(option));
  }
  // ret should be [true, true, true, false, false]

  await sleep(5000);

  const nextRet: boolean[] = [];
  for (let i = 0, ilen = 2; i < ilen; i++) {
    nextRet.push(await throttler.tryAcquire(option));
  }
  // nextRet should be [true, true]
}

Redis Storage Demo


async function sleep(time) {
  return new Promise((resolve: any) => {
    setTimeout(() => {
      resolve();
    }, time);
  });
}

//main function
async function main() {

  //create a throttler, storage in redis
  const throttler = new RawThrottler({
    limit: 3,
    ttl: 5,
    storage: {
      type: StorageTypeEnum.redis,
      options: {
        host: '127.0.0.1',
        port: 6379,
        db: 0,
        password: '',
      },
    },
  });

  const option: IAcquireOption = { key: 'test-key' };
  const ret: boolean[] = [];
  for (let i = 0, ilen = 5; i < ilen; i++) {
    //user tryAcquire to getToken
    ret.push(await throttler.tryAcquire(option));
  }
  // ret should be [true, true, true, false, false]

  await sleep(5000);

  const nextRet: boolean[] = [];
  for (let i = 0, ilen = 2; i < ilen; i++) {
    nextRet.push(await throttler.tryAcquire(option));
  }
  // nextRet should be [true, true]
}