Cloud Functions for Firebase Performance

I use Cloud features for Firebase for:

  • Get parameters from api.ai
  • Make a third-party API call and
  • Reply to api.ai.

My third-party API call uses the Node.js module request and is wrapped inside the function ( getInfoFromApi() ) in index.js.

The problem I am facing is that it takes 15 to 20 seconds to execute a call to a secondary function in sequence. Note. The cloud function itself completes its execution sequentially in the range 400 & middot; ms

By writing simple comments to the console, I can see when a function starts, when a secondary function is called, and when it receives a response from a third party, so I think I can see what happens.

Roughly, the timings look like this:

  • 0: cloud function initialization
  • 400 ms: cloud function complete
  • 16 s function: getInfoFromApi() is called (!)
  • 17 sec: third-party API returns results

My questions:

  • Is there an obvious reason for the delay in calling a secondary function? This does not seem to be caused by the cold start issue , as the cloud function quickly comes to life and the delay persists even after repeated calls.
  • Is the node request module used causing the problem? Is there a better module for creating / managing HTTP requests from cloud functions?

You can see the simplified Gist index.js here: https://gist.github.com/anonymous/7e00420cf2623b33b80d88880be04f65

Here's the capture of the Firebase console, showing an example of time. Note: the output is slightly different from the code above, as I simplified the code above to help understand. enter image description here

+5
source share
2 answers

The getInfoFrom3rdParty() call is an asynchronous event. However, you did not return a promise from your function, so the functions do not wait for the completion of the asynchronous event.

It seems to me that since you are returning undefined, the function also assumes that it failed and retries. At some point in the repetition process, the async event probably ends before the function exits (i.e., Unintended success due to race conditions). I have seen similar results in other cases where users do not return a promise or value in their functions.

I canโ€™t tell from the essence of what you are trying to do - it does not seem to do anything with third-party results and is probably not a realistic mcve of your use case. But something like this, probably you want:

 exports.getInfo = functions.https.onRequest((request, response) => { // .... // NOTE THE RETURN; MOST IMPORTANT PART OF THIS SAMPLE return getInfoFromThirdParty(...).then(() => { response.writeHead(200, {"Content-Type": "application/json"}); response.end(JSON.stringify(payload)); }).catch(e => /* write error to response */); }); function getInfoFrom3rdParty(food) { reqObj.body = '{"query": "'+food+'"}'; return new Promise((resolve, reject) => { mainRequest(reqObj, function (error, response, body) { // .... if( error ) reject(error); else resolve(...); // .... }); }); } 
+1
source

From my experience with cloud functions, as soon as the completion flag completes, it will cause a delay (from 3 to 15 seconds). This blocks the remaining execution and increases the overall response time.

To overcome this, you can try placing Promise on your third-party call as soon as it completes, then execute "response.send ()", which terminates the function.

0
source

Source: https://habr.com/ru/post/1265749/


All Articles