In my Node server (running on Mac), I'm requesting data from an external API using websockets and sending the result I get to my cloud API.
The websocket is sending a lot of data (1000+ obj/s). When I try to POST this to my cloud server, I get error on client side (my node server). This only happens during high load, I get no errors when load is lower (say 300/s). I'm not POSTing to my local server so I don't know what this error means.
One thing I noticed is that the ports are not reused. When I have high load, I go up to 15000+ used ports on my local machine and then I start getting these errors.
I have tried setting "agent: false" in the HTTP header without success. I have also tried "connection": "keep-alive" without success (found these two suggestions on SO).
This is the code that is POSTing the data I get from websocket:
function PostCode(codestring, apipathstring) {
return new Promise((resolve,reject)=>{
// An object of options to indicate where to post to
var post_options = {
host: 'hostname', //
port: 'port',
path: apipathstring,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data),
}
};
// Set up the request
var post_req = https.request(post_options,function(res){
let data = '';
res.setEncoding('utf8');
// A chunk of data has been recieved.
res.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
res.on('end', () => {
resolve(data);
});
// Error event received
}).on("error", (err) => {
reject("POSTING Error: " + err);
});
post_req.write(post_data);
post_req.end();
})
}
Optimally, I would stream even high traffic data without getting these connection errors.
Errors below:
Error: connect EADDRNOTAVAIL [my_cloud_IP] - Local (0.0.0.0:0)
Error: getaddrinfo ENOTFOUND [my_cloud_IP] [my_cloud_IP]:[my_cloud_port]
You can create your own "keep alive" agent, and use that one specific agent to send all the HTTP requests:
const http = require('http');
const keepAliveAgent = new http.Agent({ keepAlive: true });
options.agent = keepAliveAgent;
http.request(options, onResponseCallback);
According to the document :
keepAlive Keep sockets around even when there are no outstanding requests, so they can be used for future requests without having to reestablish a TCP connection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.