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

koa-rate-limits

v1.0.1

Published

koa rate limit middleware base on @aftership/rate-limiter

Downloads

6

Readme

限流中间件

koa-rate-limit是koa的限流中间件。核心是基于Redis实现,所以在使用的时候确保已经安装Redis。
提供的能力:

  1. 基本的限流实现,基于固定窗口的限流算法
  2. 支持API级的限流,遵循路由匹配原则的限流规则匹配

开发中的能力:

  1. 令牌桶限流算法的实现
  2. 基于用户IP的限流
  3. 限流配置的动态配置

使用说明

安装

# npm
npm i koa-rate-limit

# yarn 
yarn add koa-rate-limit

使用示例

import * as Koa from 'koa';
import * as Redis from 'ioredis';
import * as Router from 'koa-router';
import { WindowRateLimiter IConfig } from 'koa-rate-limit';

const app = new Koa();

const limitConfig: IConfig = {
    default: {
        limit: 10,
        duration: 10,
    },
    _hello: {
        limit: 3,
        duration: 10,
    },
};
WindowRateLimiter.init(new Redis(), limiterConfig, 'prefix');

app.use(WindowRateLimiter.limiter);

const router = new Router();
router.get('/hello', (ctx, next) => {
  ctx.body = 'hello world';
});

app.use(router.routes());

app.use((ctx, next) => {
  ctx.body = ctx.path;
});

app.listen(3000);

使用示例说明

关于Redis和ioredis的使用,请参考下面的文档:
Redis使用指南
ioredis使用指南

关于koa中间件的使用,请参考下面的文档:
koa中间件机制详解

关于配置文件的说明 中间件使用的配置文件通常是下面的格式:

const limitConfig: IConfig = {
    default: {
        limit: 10,
        duration: 10,
    },
    _hello: {
        limit: 3,
        duration: 10,
    },
};

本中间件是对接口进行限流,使用接口的path作为限流的key,若提供了prefix,则key为:${prefix}${key}:

this.key = prefix ? `${prefix}${key}` : key;

这里的key的生成方式如下:

// 替换path中的'/'为'_'
const key = path.replace(/\//g, '_');

限流的规则如下:

  1. 根据接口的path计算出key
  2. 查找与key最匹配的限流配置,遵循最佳匹配原则
  3. 若没有与key最匹配的限流配置,则检查默认配置

支持功能

功能:

  1. 支持接口级别的限流
  2. 支持固定窗口限流算法和令牌桶限流算法

版本记录

1.0.0

  1. 基于固定窗口限流算法实现KOA中间件
  2. 支持接口级别的限流