首页 > 解决方案 > Nodejs只有4个远程http请求同时执行

问题描述

我正在尝试使用下面的 NodeJS 代码执行多个同时请求,但是从输出中我可以看到下面的请求以 4 组的形式执行。
为什么会这样?

此外,我将默认 libuv 线程池大小更改为 7。

const http = require("http");
const rq = require("request");

http.createServer(function(req, res) {
  res.write("Server started");
  var start = process.hrtime();
  rq("https://stream101.herokuapp.com/5.php", {
    pool: {
      maxSockets: 20
    }
  }, (err, res, body) => {
    if (res) {
      var end = process.hrtime(start);
      console.log(body);
      console.log("in time : %ds", end[0] + end[1] / 1e9);
    }
  });
  res.end();
}).listen(8081);

这里stream101.herokuapp.com只是在睡眠 5 秒后回显内容。

这就是我的节点应用程序的启动方式:

set UV_THREADPOOL_SIZE=7 && node index.js      

节点控制台输出:

Streaming after 5 seconds
in time : 6.075168388s
Streaming after 5 seconds
in time : 6.079874785s
Streaming after 5 seconds
in time : 6.077171028s
Streaming after 5 seconds
in time : 6.420050492s

Streaming after 5 seconds
in time : 10.966390463s
Streaming after 5 seconds
in time : 10.967194215s
Streaming after 5 seconds
in time : 10.998825902s
Streaming after 5 seconds
in time : 11.42817129s

Streaming after 5 seconds
in time : 16.353070153s
Streaming after 5 seconds
in time : 16.440429564s
Streaming after 5 seconds
in time : 16.643850903s
Streaming after 5 seconds
in time : 16.624362216s

上述测试用例的 Apache 基准测试命令:

ab -n12 -c5 http://localhost:8081/

是否存在与 libuv 线程池相关的问题,因为它可以用于 heruko dns 查询?

标签: node.jsconcurrencythreadpoollibuv

解决方案


推荐阅读