firebase functions cold start use Express.js node.js get problem
System on firebase functions always have problems that code start.
https://medium.com/@siriwatknp/cold-start-workaround-in-firebase-cloud-functions-8e9db1426bd3
So
google office
https://firebase.google.com/docs/functions/networking
const http = require('http');
const functions = require('firebase-functions');
// Setting the `keepAlive` option to `true` keeps
// connections open between function invocations
const agent = new http.Agent({keepAlive: true});
exports.function = functions.https.onRequest((request, response) => {
req = http.request({
host: '',
port: 80,
path: '',
method: 'GET',
agent: agent, // Holds the connection open after the first invocation
}, res => {
let rawData = '';
res.setEncoding('utf8');
res.on('data', chunk => { rawData += chunk; });
...
Two line let me confused. Not only me.
const agent = new http.Agent({keepAlive: true});
agent: agent, // Holds the connection open after the first invocation
Some guy same me.
Get same problem.
Success??
Thanks Max. However I found the answer to this problem from the same link I posted above (i.e. firebase.google.com/docs/functions/networking). Just adding two lines of code solved the problem. Now response is quite prompt. I added following lines: 1. const agent = new http.Agent({keepAlive: true}); 2. agent: agent, – Vipul Sisodia Apr 2 ‘19 at 19:04
See Express.js
https://nodejs.org/dist/latest-v10.x/docs/api/http.html#http_new_agent_options
Not thing can Try.
Other way
https://medium.com/@onufrienkos/keep-alive-connection-on-inter-service-http-requests-3f2de73ffa1
index.js```
require(‘https’).globalAgent.keepAlive = true;
const functions = require(‘firebase-functions’);
const express = require(‘express’);
const app = express();
…
exports.api = functions.runWith(runtimeOpts).https.onRequest(app);
require('https').globalAgent.keepAlive = true;
```
Try by yourself.