Firebase云功能和多个请求

时间:2019-11-29 15:33:10

标签: google-cloud-firestore google-cloud-functions

我正在使用下面的代码运行一个函数来创建一组文档,当对该云函数有单个请求时,它会正常工作。

当我快速连续对该功能发出多个请求时,该功能将失败。

有人有什么建议吗?

export const importCaseCreateCaseStatus = functions.firestore.
 document('caseStatus/{case}').onCreate((snap, context) => {

 const caseReference = snap.id;
 console.log(caseReference)

 var reg = "[^£]*£[^£]*";
 const uid = caseReference.match(reg);
 if (uid) {

 let ref = db.collection('caseStatus').doc(caseReference).collection("0").doc("0")
 let ref1 = db.collection('caseStatus').doc(caseReference).collection("0").doc("1")
 let ref2 = db.collection('caseStatus').doc(caseReference).collection("0").doc("2")
 let ref3 = db.collection('caseStatus').doc(caseReference).collection("0").doc("3")
 let ref4 = db.collection('caseStatus').doc(caseReference).collection("0").doc("4")
 let ref5 = db.collection('caseStatus').doc(caseReference).collection("0").doc("5")
 let ref6 = db.collection('caseStatus').doc(caseReference).collection("0").doc("6")
 let ref7 = db.collection('caseStatus').doc(caseReference).collection("0").doc("7")
 let ref8 = db.collection('caseStatus').doc(caseReference).collection("0").doc("8")
 let ref9 = db.collection('caseStatus').doc(caseReference).collection("0").doc("9")

 batch.set(ref, arrayMilestones[0]);
 batch.set(ref1, arrayMilestones[1]);
 batch.set(ref2, arrayMilestones[2]);
 batch.set(ref3, arrayMilestones[3]);
 batch.set(ref4, arrayMilestones[4]);
 batch.set(ref5, arrayMilestones[5]);
 batch.set(ref6, arrayMilestones[6]);
 batch.set(ref7, arrayMilestones[7]);
 batch.set(ref8, arrayMilestones[8]);
 batch.set(ref9, arrayMilestones[9]);

 return batch.commit().then(function () {
 console.log("imported milestones")
 return null

 });
 }

 });

1 个答案:

答案 0 :(得分:0)

我认为这与您的代码无关,似乎与如何管理Cloud Functions服务有关。

当确定需要更多容量时,云功能将自动扩展。当不再需要这些实例时,它们将按比例缩小。

我建议您看一下本文档,在其中可以找到有关如何在Cloud Functions上使用Pub / Sub触发器的有用信息,Pub / Sub是一种全局分布的消息总线,可以根据需要自动缩放[1]。

我也建议您查看这些视频,其中解释了函数缩放[2]的工作方式以及如何处理并行执行[3]。

[1] https://cloud.google.com/functions/docs/calling/pubsub

[2] https://www.youtube.com/watch?v=rCpKxpIMg6o

[3] https://www.youtube.com/watch?v=MzTS6mFDGjU