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

@midwayjs-plus/throttler

v3.9.3

Published

midway throttler base on traffic-throttler

Downloads

2

Readme

midway-throttler

midway request throttler component, based on traffic-throttler。 It's core design base on @nestjs/throttler, and API will same as @nestjs/throttler.

这是基于 midway 3.0 的限流器组件模块,基于 traffic-throttler 封装。核心逻辑来自 @nestjs/throttler, API 用法也尽量与其保持一致

Install (安装)

npm i midway-throttler --save

Usage(用法)

1. 引入组件

首先在 src/configuration.ts 中引入组件,并且配置全局守卫

import * as throttler from 'midway-throttler';
import { ThrottlerGuard } from 'midway-throttler';
import * as koa from '@midwayjs/koa';

@Configuration({
  imports: [koa, throttler], //import throttler 组件
  importConfigs: [join(__dirname, './config')],
})
export class ContainerLifeCycle {
  @App()
  app: koa.Application;

  async onReady() {
    //全局守卫配置
    this.app.useGuard(ThrottlerGuard);
  }
}

2. 配置相关参数

在 config.default.ts 配置文件中,配置如下

throttler: {
  ttl?: number,    //时间窗口,在该时间段内限制 limit 个数的请求, 超出限流
  limit?: number,   //请求个数限制
  storage?: IThrottlerStorageOption,    //内部存储配置,支持 memory | redis 两种存储方式
  errorMsg?: string,  //超出限流后的报错信息
}

针对某些 Controller 或者某些 Method, 支持

  1. 想覆盖全局配置,可以通过 Throttle 装饰器进行配置修改;
  2. 想要不限流,可以通过 SkipThrottle 装饰器放开限流
import { Controller, Get } from '@midwayjs/decorator';
import { SkipThrottle, Throttle } from 'midway-throttler';

@Throttle(2, 20) //重新配置
@Controller('/')
export class HomeController {
  @Throttle(1, 10) //重新配置
  @Get('/')
  async home(): Promise<string> {
    return 'Hello Midwayjs!';
  }

  @SkipThrottle(true) //该请求不限流
  @Get('/skip')
  async doSkip(): Promise<string> {
    return 'I am skip, no throttle';
  }
}

Example(例子)

  1. cd example
  2. npm i
  3. npm run dev