同一触发器的多个云功能会引起问题吗? #AskFirebase

时间:2018-11-02 20:11:25

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

应该对每个触发器整合云功能,还是为同一触发器编写多个功能?特别是,如果我正在阅读里面的文档...对性能或计费有重大影响吗?

// Example: multiple onWrite functions triggered by the same Firestore doc path
functions.firestore.document('myCollection/{itemId}').onWrite((change, context) => {
   // do one complex thing, potentially reading/writing data
}

functions.firestore.document('myCollection/{itemId}').onWrite((change, context) => {
   // do another complex thing, potentially reading/writing the same or different data
}

或...

// Example: one trigger and a monolithic function handling everything...
functions.firestore.document('myCollection/{itemId}').onWrite(async (change, context) => {
   const otherDataSnapshot = await admin.firestore().ref('myPath').once('value').then();
   this.doOneComplexThing(change, context, otherDataSnapshot);
   this.doAnotherComplexThing(change, context, otherDataSnapshot);
}

const doOneComplexThing = (change, context, otherDataSnapshot) => {
   // do one complex thing
}

const doAnotherComplexThing = (change, context, otherDataSnapshot) => {
   // do that other complex thing
}

AskFirebase

1 个答案:

答案 0 :(得分:1)

如果您有两个函数触发器而不是一个,则将为每个onWrite两次调用而不是一次调用。它将永远更加昂贵。进行拆分可能有充分的理由,这使其值得进行拆分,因此您必须针对具体情况决定这一点。