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.

https://stackoverflow.com/questions/56912118/how-can-i-maintain-persist-a-cloud-functions-connection-using-expressjs


Success??

https://stackoverflow.com/questions/55425015/how-to-keep-alive-dialogflow-firebase-function-to-avoid-new-connection-time-wast

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

https://stackoverflow.com/questions/60667847/unable-to-verify-leaf-signature-from-request-with-firebase-functions-node-js-wit

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.