Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection pooling problem #1544

Closed
SinanGabel opened this issue Nov 18, 2019 · 3 comments
Closed

connection pooling problem #1544

SinanGabel opened this issue Nov 18, 2019 · 3 comments
Assignees
Labels
type: question Request for information or clarification. Not an issue.

Comments

@SinanGabel
Copy link

SinanGabel commented Nov 18, 2019

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 :)

@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Nov 19, 2019
@fhinkel
Copy link
Contributor

fhinkel commented Nov 19, 2019

@SinanGabel Thanks for reporting the problem. @ace-n Could you have a look at this please. Thanks!

@fhinkel fhinkel added type: question Request for information or clarification. Not an issue. and removed triage me I really want to be triaged. labels Nov 19, 2019
@fhinkel
Copy link
Contributor

fhinkel commented Dec 3, 2019

ping @ace-n

@fhinkel fhinkel closed this as completed Feb 11, 2020
@SinanGabel
Copy link
Author

SinanGabel commented Feb 11, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

4 participants