-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
Hi,
I tried your suggestion but it does not work as-is:
// [START functions_tips_connection_pooling]
const http = require('http');
const agent = new http.Agent({keepAlive: true});
/**
* HTTP Cloud Function that caches an HTTP agent to pool HTTP connections.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.connectionPooling = (req, res) => {
req = http.request(
{
host: '',
port: 80,
path: '',
method: 'GET',
agent: agent,
},
resInner => {
let rawData = '';
resInner.setEncoding('utf8');
resInner.on('data', chunk => {
rawData += chunk;
});
resInner.on('end', () => {
res.status(200).send(`Data: ${rawData}`);
});
}
);
req.on('error', e => {
res.status(500).send(`Error: ${e.message}`);
});
req.end();
};
// [END functions_tips_connection_pooling]
(1) http is not allowed.
Likely solution: the port error disappears when using https instead i.e.
const https = require('https');
...
(2) I tried different settings of options to https.request(options, ...) but I could not get any to work.
For example:
const options =
{ "method": "POST",
"protocol": "https:",
"host": "https://us-central1-[my_project_id].cloudfunctions.net",
"hostname": "https://us-central1-[my_project_id].cloudfunctions.net",
"agent": agent,
"path": "",
"port": "",
"headers": { "Content-Type": "application/json" }
}
resulted in error message:
Error: getaddrinfo ENOTFOUND https://us-central1-[my_project_id].cloudfunctions.net https://us-central1-[my_project_id].cloudfunctions.net:443
(3) I searched for a solution and found this: https://github.com/expressjs/express/issues/3556 i.e. not to use your require("https") approach but to simply utilize the expressjs functionality by adding e.g.:
exports.connectionPooling = (req, res) => {
res.set("Access-Control-Allow-Origin", "*");
req.socket.setKeepAlive({"enable": true})
...
(4)
I tried to run artillery with a POST json request that but could not see any gains with and without req.socket.setKeepAlive({"enable": true})
Here is the artillery .yml I used:
config:
target: "https://us-central1-[my_project_id].cloudfunctions.net/connectionPooling"
phases:
- duration: 10
arrivalRate: 30
scenarios:
- flow:
- post:
url: "/"
json:
request: '{"request": [1,"yes", true]}'
Please advice, thanks :)