【问题标题】:how to use/implement a custom nodejs Circuit Breaker based on the number of requests?如何根据请求数量使用/实现自定义nodejs断路器?
【发布时间】:2021-04-09 18:18:47
【问题描述】:

我正在尝试根据 Typescript/express 应用程序中提供的请求数量而不是失败百分比来确定实现断路器的最佳等待时间。

由于该应用程序旨在由大量用户在高负载下执行,因此我正在尝试自定义响应代码以使用 k8s/istio 触发水平缩放事件。

如果有一些异步工作正在进行,我首先要获取的是 nodejs eventloop 事件中的请求数,因为我的请求的很大一部分是使用 async/await 异步执行的。

顺便说一句:
我看过这些库

为了让这成为可能,我有什么好的想法/路径可以开始吗?

【问题讨论】:

  • 您究竟希望发生什么,称为断路器?你能提供你想要的迷你规格吗?添加,您查看的库有什么问题?还有,"trigger a Horizo​​ntal scaling event with k8s/istio"是什么意思?

标签: node.js typescript express asynchronous


【解决方案1】:

我无法从您的问题中确定,但如果您尝试做的只是跟踪正在进行的请求数量,然后在该数量超过特定值时特别执行某些操作,那么你可以使用这个中间件:

function requestCntr() {
    let inProgress = 0;
    const events = ['finish', 'end', 'error', 'close'];

    return function(req, res, next) {

      function done() {
          // request finished, so decrement the inProgress counter
          --inProgress;
          // unhook all our event handlers so we don't count it more than one
          events.forEach(event => res.off(event, done));
      }

      // increment counter for requests in progress
      ++inProgress;
      const maxRequests = 10;
      if (inProgress > maxRequests) {
          console.log('more than 10 requests in flight at the same time');
          // do whatever you want to here
      }

      events.forEach(event => res.on(event, done));
      next();
    }
}

app.use(requestCntr());

【讨论】:

  • 这正是我正在寻找的,一个针对当前正在进行的请求的计数器。我一直在寻找限速器而不是断路器。一旦达到 maxRequests,我将返回 429 HTTP 代码。我将实现与本文中的架构类似的东西。 github.com/stefanprodan/istio-hpa,非常感谢
猜你喜欢
  • 2020-05-06
  • 2018-09-06
  • 1970-01-01
  • 2015-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多