Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

node.js - 速率限制-TCP服务器(net.Server)(Rate-Limit - TCP Server (net.Server))

I am using net.Server as my TCP server. (我正在使用net.Server作为我的TCP服务器。)

How can I enforce message rate limit? (如何执行邮件速率限制?)

I found similar solution for Express ( express-rate-limit ) and Websocket ( websocket-rate-limit ) but not for TCP. (我为Express( express-rate-limit )和Websocket( websocket-rate-limit )找到了类似的解决方案,但没有为TCP找到类似的解决方案。)

  ask by Maoritzio translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

rate-limiter-flexible package can be used for that. (可以使用rate-limiter-flexible软件包。)

It supports many stores like Redis, MongoDB, etc. Here is the most simple example with in memory storage: (它支持Redis,MongoDB等许多存储。这是内存存储中最简单的示例:)

const opts = {
  points: 10, // 10 points
  duration: 1, // Per second
};

const rateLimiter = new RateLimiterMemory(opts);

rateLimiter.consume(remoteAddress, 1) // consume 1 point
    .then((rateLimiterRes) => {
      // 1 point consumed
      // Some app logic here
    })
    .catch((rateLimiterRes) => {
      // Not enough points to consume
    });

More examples on Wiki (Wiki上的更多示例)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...