【发布时间】:2019-05-31 18:50:54
【问题描述】:
我有以下 Firebase 函数代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification = functions.https.onRequest((request, response) => {
const payLoad = {
data: {
execute_noise_measure: "true"
}
};
const options = {
priority: "high"
};
return admin.messaging().sendToTopic("all", payLoad, options).then(function(response) {
return console.log("Successfully sent message:", response);
})
.catch(function(error) {
return console.log("Error sending message:", error);
});
});
函数正确执行,在控制台中给出“成功发送消息:”响应。不幸的是,每个函数调用都会消耗大量的“函数调用中的 CPU 分配”。几次通话后,我的每日限额超出了:“错误:超出配额(配额组“CPUMilliSecondsDailyNonbillable”超出配额”
【问题讨论】:
-
我不认为
sendToTopic应该占用大量本地 CPU 时间,因为从主题到设备 ID 的实际扇出发生在 Google 的服务器上。你能计时/找出每个单独的电话需要多长时间吗? -
@FrankvanPuffelen 我刚刚添加了带有日志输出的屏幕截图。检查我的编辑。该函数似乎以超时结束。我怎样才能避免这种情况?
-
这听起来像是您返回的承诺永远不会解决,这肯定可以解释为什么您会在几次调用中耗尽您的免费 CPU 配额。
return console.log(...)在我看来很可疑,因为console.log()返回undefined。我会分离出console.log(...); return true;。 -
这似乎是问题的一部分。我根据您的建议将这些陈述分开,但这还不够。我还需要添加
response.status(200).send("ok");。现在一切正常,CPU 分配使用率下降了大约 400 次。 -
啊,我没有注意到你的函数是HTTP触发的。在这种情况下,您确实需要向客户端发送响应以终止该功能。
标签: javascript google-cloud-platform firebase-cloud-messaging google-cloud-functions firebase-admin