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

rate-valve

v0.2.16

Published

Node.js http rate limiting plugin

Downloads

15

Readme

Valve

Node.js HTTP rate limiting plugin. Based on native http module, Frameless !!!

Installation

npm install rate-valve [--save]

Usage

  • [x] 接口限流
  • [x] 服务限流
  • [ ] ip 隔离
  • [x] 手动触发
  • [x] 拦截器 - filter

Configuration

  • enable - Optional, type:Boolean, 是否开启限流,默认 true
  • rule - Optional, type:Object, 限流规则, 默认无上限
  • rule.api - Optional, type:Array<Object>, API 限流规则
  • rule.api.path - Required, type:String, API 路径
  • rule.api.method - Required, type:'GET' | 'POST' | 'DELETE' | 'PUT' | 'PATCH' | 'HEAD' | 'OPTIONS', 请求方式
  • rule.api.rule - Required, type:Object, 限流规则
  • rule.api.rule.limit - Required, type:Number, 窗口时间内请求上限
  • rule.server - Optional, type:Object, 服务限流规则
  • rule.server.limit - Required, type:Number, 窗口时间内服务请求上限
  • rule.ip - Optional, type:Object, IP 限流规则
  • rule.ip.enable - Optional, type:Boolean, 是否开启 IP 限流,默认 false
  • rule.ip.limit - Required, type:Number, 窗口时间内 IP 请求上限
  • rule.ip.storage - Optional, type: String | RedisOptions, IP 限流存储方式,默认内存存储 'memory', if use redis,please see ioredis configuration
  • rule.ip.whiteList - Optional, type:Array<String>, IP 白名单, 优先级高于黑名单,白名单内的 IP 不经过限流
  • rule.ip.blackList - Optional, type:Array<String>, IP 黑名单, 黑名单内的 IP 强制限流
  • interval - Optional, type:Number, 窗口时间,单位秒,默认 60 秒
  • message - Optional, type:String, 限流时响应内容,不配置则响应默认内容
  • statusCode - Optional, type:Number, 限流时响应的 HTTP 状态码,默认 429
  • isSendRetry - Optional, type:Boolean, 限流时是否响应 Retry-After 响应头,默认 false
  • requestPropertyName - Optional, type:String, 限流信息绑定在 request 对象上的属性名,默认 valve
  • filters - Optional, type:Array<Filter>, 限流拦截器,通过则对当前请求不限流,只要通过一个拦截器即可
  • logger-Optional, type:Any, 自定义 Logger,默认 Console
  • performance-Optional,type:Object, 性能配置,开启自适应限流的时候需要配置
  • performance.enable-Optional, type:Boolean, 是否开启自适应限流,默认 false
  • performance.limitThreshold-Optional, type:Number, 超过性能限制次数,当超过该次数,将开启限流,默认 2
  • performance.limit-Optional, type:Object, 自适应限流的上限配置
  • performance.limit.cpu-Optional, type:Number, cpu 上限,取值 0-1, 默认 1,
  • performance.limit.memory-Optional, type:String, 内存上限,支持单位:B, KB, MB, GB,默认 '1GB',
  • performance.recoveryThreshold-Optional, type:Number, 低于性能限制次数,当超过该次数,将关闭限流,默认 1
  • performance.recovery-Optional, type:Object, 自适应限流的下限配置
  • performance.recovery.cpu-Optional, type:Number, cpu 下限,取值 0-1, 默认 0,
  • performance.recovery.memory-Optional, type:String, 内存下限,支持单位:B, KB, MB, GB,默认 '0GB',
  • debug-Optional, type:Boolean, 是否输出调试日志,默认 false
type Filter = (request: http.IncomingMessage, response: http.ServerResponse) => boolean;

Example:

const options = {
    enable: true,
    rule:{
        server: {
            limit: 300,
        },
        api: [
            {
                method: 'GET',
                path: '/test1',
                rule: { limit: 100 }
            },
            {
                method: 'GET',
                path: '/test2/:id',
                rule: { limit: 150 }
            }
        ],
    },
    interval: 10,
    message: 'Request sent too fast, please fill it out later',
    statusCode: 429,
    isSendRetry: false,
    filters: [
        (request, response) => {
            return request.headers['x-user-id'] == 1;
        }
    ],
    performance: {
        enable: true,
        limitThreshold: 2,
        limit: {
            cpu: 0.8,
            memory: '1GB',
        },
        recoveryThreshold: 1,
        recovery: {
            cpu: 0.2,
            memory: '0GB',
        },
    },
    debug: false,
}

limit in request

通常情况下不需要人为操作,如果想要在代码中手动去触发限流,可参考:

import { Valve } from 'rate-valve';
import * as http from 'http';

// Initialization configuration needs to be done before the http server starts
new Valve(ValveOptions);

http.createServer((request, response) => {
    console.log(request.valve.toJSON());        // { count: { total: 100, api: 10 } }

    if(request.headers['x-user-id'] === 1)
        return request.valve.limit();           // 在请求中手动执行限流

    res.write('hello world /' + req.url);
    res.end();
}).listen(2233);