【问题标题】:Rate-Limit - TCP Server (net.Server)速率限制 - TCP 服务器 (net.Server)
【发布时间】:2020-03-25 21:00:33
【问题描述】:

我使用net.Server 作为我的 TCP 服务器。

如何强制执行消息速率限制?

我为 Express (express-rate-limit) 和 Websocket (websocket-rate-limit) 找到了类似的解决方案,但对于 TCP 却没有。

【问题讨论】:

    标签: node.js typescript tcp rate-limiting ddos


    【解决方案1】:

    rate-limiter-flexible 包可以用于此。

    它支持许多存储,如 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

    【讨论】:

    • 谢谢!我也遇到过。我很惊讶它最近发布了:它让我想知道为什么没有现有的 TCP 专用解决方案 [而且我一定遗漏了一些东西]。总而言之-我发现它很容易使用!还为找到这个问题的人提供这篇论文(我读起来很有趣)。 medium.com/@animirr/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多