From c695f95f9f562545a550a8860401232d1019143c Mon Sep 17 00:00:00 2001 From: ace-n Date: Thu, 9 Apr 2020 20:47:09 -0700 Subject: [PATCH 1/2] efactor connection pooling sample for Node 8+ --- functions/tips/index.js | 42 +++++++++++++------------------------ functions/tips/package.json | 3 ++- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/functions/tips/index.js b/functions/tips/index.js index 579fe88f04..44ea1a1aeb 100644 --- a/functions/tips/index.js +++ b/functions/tips/index.js @@ -74,8 +74,13 @@ exports.lazyGlobals = (req, res) => { // [END functions_tips_lazy_globals] // [START functions_tips_connection_pooling] +const axios = require('axios'); + const http = require('http'); -const agent = new http.Agent({keepAlive: true}); +const https = require('https'); + +const httpAgent = new http.Agent({keepAlive: true}); +const httpsAgent = new https.Agent({keepAlive: true}); /** * HTTP Cloud Function that caches an HTTP agent to pool HTTP connections. @@ -83,30 +88,13 @@ const agent = new http.Agent({keepAlive: true}); * @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(); +exports.connectionPooling = async (req, res) => { + try { + const {data} = await axios.get('/', {httpAgent, httpsAgent}); + res.status(200).send(`Data: ${data}`); + } catch (err) { + res.status(500).send(`Error: ${err.message}`); + } }; // [END functions_tips_connection_pooling] @@ -144,7 +132,7 @@ exports.avoidInfiniteRetries = (event, callback) => { * @param {object} event.data Data included with the event. * @param {object} event.data.retry User-supplied parameter that tells the function whether to retry. */ -exports.retryPromise = (event) => { +exports.retryPromise = event => { const tryAgain = !!event.data.retry; if (tryAgain) { @@ -193,7 +181,7 @@ const pubsub = new PubSub(); exports.gcpApiCall = (req, res) => { const topic = pubsub.topic(req.body.topic); - topic.publish(Buffer.from('Test message'), (err) => { + topic.publish(Buffer.from('Test message'), err => { if (err) { res.status(500).send(`Error publishing the message: ${err}`); } else { diff --git a/functions/tips/package.json b/functions/tips/package.json index bbbdc76916..374395570d 100644 --- a/functions/tips/package.json +++ b/functions/tips/package.json @@ -15,7 +15,8 @@ "test": "mocha test/*.test.js --timeout=60000 --exit" }, "dependencies": { - "@google-cloud/pubsub": "^1.0.0" + "@google-cloud/pubsub": "^1.0.0", + "axios": "^0.19.2" }, "devDependencies": { "mocha": "^7.0.0", From 4a7d3291c9eb36716e5fe54987e07278d998d6bf Mon Sep 17 00:00:00 2001 From: Ace Nassri Date: Fri, 10 Apr 2020 11:27:03 -0700 Subject: [PATCH 2/2] Revert erroneous lint changes --- functions/tips/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/functions/tips/index.js b/functions/tips/index.js index 44ea1a1aeb..53dd5de2b3 100644 --- a/functions/tips/index.js +++ b/functions/tips/index.js @@ -132,7 +132,7 @@ exports.avoidInfiniteRetries = (event, callback) => { * @param {object} event.data Data included with the event. * @param {object} event.data.retry User-supplied parameter that tells the function whether to retry. */ -exports.retryPromise = event => { +exports.retryPromise = (event) => { const tryAgain = !!event.data.retry; if (tryAgain) { @@ -181,7 +181,7 @@ const pubsub = new PubSub(); exports.gcpApiCall = (req, res) => { const topic = pubsub.topic(req.body.topic); - topic.publish(Buffer.from('Test message'), err => { + topic.publish(Buffer.from('Test message'), (err) => { if (err) { res.status(500).send(`Error publishing the message: ${err}`); } else {