1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Fix connection keep-alive in http request node

This commit is contained in:
Nick O'Leary 2023-06-21 15:45:23 +01:00
parent 2571949e90
commit 610cb170e7
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9

View File

@ -23,6 +23,8 @@ module.exports = async function(RED) {
const { v4: uuid } = require('uuid'); const { v4: uuid } = require('uuid');
const crypto = require('crypto'); const crypto = require('crypto');
const URL = require("url").URL const URL = require("url").URL
const http = require("http")
const https = require("https")
var mustache = require("mustache"); var mustache = require("mustache");
var querystring = require("querystring"); var querystring = require("querystring");
var cookie = require("cookie"); var cookie = require("cookie");
@ -65,16 +67,27 @@ in your Node-RED user directory (${RED.settings.userDir}).
function HTTPRequest(n) { function HTTPRequest(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
checkNodeAgentPatch(); checkNodeAgentPatch();
var node = this; const node = this;
var nodeUrl = n.url; const nodeUrl = n.url;
var isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1; const isTemplatedUrl = (nodeUrl||"").indexOf("{{") != -1;
var nodeMethod = n.method || "GET"; const nodeMethod = n.method || "GET";
var paytoqs = false; let paytoqs = false;
var paytobody = false; let paytobody = false;
var redirectList = []; let redirectList = [];
var sendErrorsToCatch = n.senderr; const sendErrorsToCatch = n.senderr;
node.headers = n.headers || []; node.headers = n.headers || [];
var nodeHTTPPersistent = n["persist"]; const useKeepAlive = n["persist"];
let agents = null
if (useKeepAlive) {
agents = {
http: new http.Agent({ keepAlive: true }),
https: new https.Agent({ keepAlive: true })
}
node.on('close', function () {
agents.http.destroy()
agents.https.destroy()
})
}
if (n.tls) { if (n.tls) {
var tlsNode = RED.nodes.getNode(n.tls); var tlsNode = RED.nodes.getNode(n.tls);
} }
@ -560,12 +573,14 @@ in your Node-RED user directory (${RED.settings.userDir}).
opts.agent = { opts.agent = {
http: new HttpProxyAgent(proxyOptions), http: new HttpProxyAgent(proxyOptions),
https: new HttpsProxyAgent(proxyOptions) https: new HttpsProxyAgent(proxyOptions)
}; }
} else { } else {
node.warn("Bad proxy url: "+ prox); node.warn("Bad proxy url: "+ prox);
} }
} }
if (useKeepAlive && !opts.agent) {
opts.agent = agents
}
if (tlsNode) { if (tlsNode) {
opts.https = {}; opts.https = {};
tlsNode.addTLSOptions(opts.https); tlsNode.addTLSOptions(opts.https);