简体   繁体   English

Firebase 功能冷启动时间慢

[英]Firebase functions slow cold start time

I read here endpoint spin-up is supposed to be transparent, which I assume means cold start times should not differ from regular execution times.在这里读到端点启动应该是透明的,我认为这意味着冷启动时间不应与常规执行时间不同。 Is this still the case?现在还是这样吗? We are getting extremely slow and unusable cold start times - around 16 seconds - across all endpoints.我们在所有端点上的冷启动时间都非常缓慢且无法使用 - 大约 16 秒。

Cold start: Function execution took 16172 ms, finished with status code: 200 After: Function execution took 1002 ms, finished with status code: 304冷启动: Function execution took 16172 ms, finished with status code: 200之后: Function execution took 1002 ms, finished with status code: 304

Is this expected behaviour and what could be causing it?这是预期的行为吗?可能是什么原因造成的?

UPDATE: The cold start times seem to no longer be an issue with node 8, at least for me.更新:冷启动时间似乎不再是节点 8 的问题,至少对我而言。 I'll leave my answer below for any individuals curious about keeping their functions warm with a cron task via App Engine.对于任何想通过 App Engine 使用 cron 任务保持其功能温暖的人,我将在下面留下我的答案。 However, there is also a new cron method available that may keep them warm more easily.但是,还有一种新的 cron 方法可用,可以更轻松地使它们保持温暖。 See the firebase blog for more details about cron and Firebase . 有关 cron 和 Firebase 的更多详细信息,请参阅 firebase 博客


My cold start times have been ridiculous, to the point where the browser will timeout waiting for a request.我的冷启动时间很荒谬,以至于浏览器在等待请求时会超时。 (like if it's waiting for a Firestore API to complete). (就像它正在等待 Firestore API 完成一样)。

Example A function that creates a new user account ( auth.user().onCreate trigger ), then sets up a user profile in firestore.示例创建新用户帐户 ( auth.user().onCreate trigger ) 的函数,然后在 firestore 中设置用户配置文件。

  • First Start After Deploy : consistently between 30 and 60 seconds, frequently gives me a "connection error" on the first try when cold (this is after waiting several seconds once Firebase CLI says "Deploy Complete!"部署后首次启动:始终在 30 到 60 秒之间,在冷的第一次尝试时经常给我一个“连接错误”(这是在 Firebase CLI 说“部署完成!”后等待几秒钟后)
  • Cold Start : 10 - 20 seconds冷启动:10 - 20 秒
  • When Warm : All of this completes in approximately 400ms.热时:所有这些都在大约 400 毫秒内完成。

As you can imagine, not many users will sit around waiting more than a few seconds for an account to be setup.可以想象,没有多少用户会等待超过几秒钟来设置帐户。 I can't just let this happen in the background either, because it's part of an application process that needs a profile setup to store input data.我也不能让它在后台发生,因为它是需要配置文件设置来存储输入数据的应用程序进程的一部分。

My solution was to add "ping" function to all of my API's, and create a cron-like scheduler task to ping each of my functions every minute, using app engine.我的解决方案是将“ping”函数添加到我的所有 API 中,并创建一个类似 cron 的调度程序任务,使用 app 引擎每分钟 ping 我的每个函数。

Ensure the ping function does something, like access a firestore document, or setup a new user account, and not just respond to the http request.确保 ping 功能执行某些操作,例如访问 firestore 文档或设置新用户帐户,而不仅仅是响应 http 请求。

See this tutorial for app engine scheduling: https://cloud.google.com/appengine/docs/flexible/nodejs/scheduling-jobs-with-cron-yaml有关应用引擎调度,请参阅本教程: https : //cloud.google.com/appengine/docs/flexible/nodejs/scheduling-jobs-with-cron-yaml

Well it is about resource usage of Cloud Functions I guess, I was there too.嗯,我猜这与 Cloud Functions 的资源使用有关,我也在那里。 While your functions are idle, Cloud Functions also releases its resources, at first call it reassignes those resources and at second call you are fine.当您的函数空闲时,Cloud Functions 也会释放其资源,第一次调用时它会重新分配这些资源,第二次调用时您就没事了。 I cannot say it is good or not, but that is the case.我不能说它好不好,但就是这样。

if you try to return a value from an async function there won't be any variables in the main function definition (functions.https.onCall) and GCP will think that the function has finished and try to remove resources from it.如果您尝试从异步 function 返回一个值,则 function 主定义 (functions.https.onCall) 中不会有任何变量,GCP 会认为 function 已完成并尝试从中删除资源。

Previous breaking code (taking 16 + seconds):之前的破解代码(耗时 16 秒以上):

functions.https.onCall((data, context) => {
  return asyncFunction()
});

After returning a promise in the function definition the function times are a lot faster (milliseconds) as the function waits for the promise to resolve before removing resources.在 function 定义中返回 promise 后,function 时间快很多(毫秒),因为 function 在删除资源之前等待 promise 解析。

functions.https.onCall((data, context) => {
  return new Promise((resolve) => {
    asyncFunction()
        .then((message) => {
          resolve(message);
        }).catch((error) => {
          resolve(error);
        });
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM