首页 > 解决方案 > Firebase 函数 - 间隔内的间隔,以减少调用

问题描述

调用量越大,使用 Firebase 云函数的成本也越高。

https://i.gyazo.com/a99fe3cd8d9fac6f98060441263ef211.png

我想知道这是否意味着当我在一个函数中调用/运行一个函数 x 次时我可以降低成本。我已经对此进行了测试,结果发现调用减少了。这就是我的意思:

exports.functionName = functions.region("europe-west2").pubsub.schedule('every 1 minutes') 
  .onRun((context) => { //counts as invocation
    console.log("Running...")
    var timesRun = 0;
    var interval = setInterval(() => {
      timesRun += 1;
      if(timesRun === 6){
        console.log("Stopping interval...")
        clearInterval(interval);
      }
      console.log("Executing...")
      
      //code... for example fetching json
      
    }, 10000); //doesn't count as invocation
});

有了这个,我可以每分钟运行我的代码 5 次,而官方调用等于 1。这真的更有效,还是我错过了什么?

标签: javascriptgoogle-cloud-functions

解决方案


有了这个,我可以每分钟运行我的代码 5 次,而官方调用等于 1。这真的更有效,还是我错过了什么?

使用 Cloud Functions,您需要为调用次数和CPU/内存使用持续时间付费。因此,虽然您的方法减少了调用次数,但它增加了您使用 CPU/内存的时间。

哪一个更便宜应该是把数据输入定价计算器的问题。

请注意,虽然计算器仅显示整秒,但实际上您需要为每 100 毫秒的计算时间付费:

计算时间以 100 毫秒为增量,四舍五入到最接近的增量。例如,执行 260 毫秒的函数将按 300 毫秒计费。


推荐阅读