gpt4 book ai didi

Node.js 传出 Http 请求连接限制(不能建立超过五个的连接)

转载 作者:可可西里 更新时间:2023-11-01 17:02:47 27 4
gpt4 key购买 nike

我正在使用 node.js 构建数据传输代理服务器。它使用 http(s) REST API 将客户端请求通过管道传输到快速对象存储服务器。

It works fine for the individual request but when the outgoing ESTABLISHED tcp connection for the same destination and port(443) reaches five, it cannot create any new connection.

这似乎不是 O/S 的问题,因为我尝试使用 java servlet 创建超过 10 个连接并且它工作正常。

我试过像下面这样为 globalAgent 设置最大套接字,但它没有改变任何东西。

http.globalAgent.maxSockets = 500;
https.globalAgent.maxSockets = 500;

这是我的源代码的一部分。

app.post('/download*', function(req, res){

/***********************************************************
* Some codes here to create new request option
***********************************************************/

var client = https.request(reqOptions, function(swiftRes) {
var buffers = [];
res.header('Content-Length', swiftRes.headers['content-length']);
res.header('Content-Type', swiftRes.headers['content-type']);
res.header('range', swiftRes.headers['range']);
res.header('connection', swiftRes.headers['connection']);

swiftRes.pipe(res);

swiftRes.on('end', function(err){
res.end();
});
});

client.on('error', function(err) {
callback && callback(err);
client.end(err);
clog.error('######### Swift Client Error event occurred. Process EXIT ');
});

client.end();
});

我希望我能得到这个问题的解决方案。

提前致谢。

最佳答案

通常,更改maxSockets 应该可以解决您的问题,请尝试将其值调低一点。

https.globalAgent.maxSockets=20;

如果这不能解决您的问题,请尝试关闭连接池。将值为 false 的键 agent 添加到请求的选项中。请记住,Node.js 使用池来使用保持连接。

//Your option code
reqOptions.agent=false;

关于Node.js 传出 Http 请求连接限制(不能建立超过五个的连接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16849107/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com