【问题标题】:Firestore Cloud Function Array Loop - Writes Long DelayFirestore 云函数数组循环 - 写入长延迟
【发布时间】:2019-04-19 14:50:35
【问题描述】:

我有一个云函数,它循环遍历一组 uid,然后为每个 uid 创建一个写入。正常写入几乎立即发生,但在写入出现之前有很长的延迟。

我尝试过不同大小的数组。只有一个或两个 uid 的数组更快,但仍有大约 5-6 秒的延迟。

exports.addPostings = functions.firestore
    .document('posts/{postID}')
    .onCreate((snap, context) => {
    const newValue = snap.data();
    var uid = newValue.author.uid;
    let followers = [];
    var feedRef = db.collection("feedItems");
    var authorRef = db.collection("users").doc(newValue.author.uid);
    authorRef.get().then((doc) => {
        let data = doc.data();
        let post_count = data.postCount;
        authorRef.update({
            postCount: Number(post_count) + 1
        }).then(() => {
            authorRef.collection('followers').doc('content').get().then((doc) => {
                let data = doc.data();
                if (typeof data.uids != 'undefined') {
                    followers = data.uids;
                }
            }).then(() => {
                followers.forEach((fol) => {
                  feedRef.add({
                      createdAt: admin.firestore.FieldValue.serverTimestamp(), uid: fol, creatorUid: uid,
                      postId: context.params.postID, isResharedPost: false, wasViewed: false,
                      wasReshared: false, wasLiked: false, wasDirectlyShared: false
                  });
                });
            });
        });
    });
});

【问题讨论】:

  • 您是否考虑在云函数中命名为cold start 的东西?当您的函数在一段时间内未执行时,Cloud Functions 会将其置于使用较少资源的模式。所以部署后的第一次调用会很慢。
  • 冷启动能持续多久?
  • 有时长达 10 秒。这取决于各种因素。但是在几次调用之后,您的函数应该会及时响应。
  • 好的,那么我认为这不是问题所在。我会尝试使用批处理,看看是否有帮助

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


【解决方案1】:

您应该在代码中修改几点:

  1. 您没有返回异步方法返回的承诺,这是编写 Cloud Function 代码时的关键,正如 Firebase 官方视频系列中关于“JavaScript Promises”的 3 个视频中所述:@ 987654321@
  2. 您应该正确链接承诺
  3. 您应该按照 CuriousGeorge 的指示使用批量写入

以下修改应该可以解决问题(但未经测试!):

exports.addPostings = functions.firestore
  .document('posts/{postID}')
  .onCreate((snap, context) => {
    const newValue = snap.data();
    var uid = newValue.author.uid;
    let followers = [];
    var feedRef = db.collection('feedItems');
    var authorRef = db.collection('users').doc(newValue.author.uid);

    return authorRef
      .get()
      .then(doc => {
        let data = doc.data();
        let post_count = data.postCount;
        return authorRef.update({
          postCount: Number(post_count) + 1
        });
      })
      .then(() => {
        return authorRef
          .collection('followers')
          .doc('content')
          .get();
      })
      .then(doc => {
        let data = doc.data();
        if (typeof data.uids != 'undefined') {
          followers = data.uids;

          let batch = db.batch();

          followers.forEach(fol => {
            const ref = feedRef.doc();
            batch.set(ref, {
              createdAt: admin.firestore.FieldValue.serverTimestamp(),
              uid: fol,
              creatorUid: uid,
              postId: context.params.postID,
              isResharedPost: false,
              wasViewed: false,
              wasReshared: false,
              wasLiked: false,
              wasDirectlyShared: false
            });
          });

          // Commit the batch
          return batch.commit();
        } else {
          return null;
        }
      });
  });

【讨论】:

  • 谢谢,我试试看
  • 立即生效!只是想知道如果batch要写超过500,会发生什么?
  • 很高兴能帮到你!如果您认为我的回答“有用且经过充分研究”,您也可以投票赞成,请参阅stackoverflow.com/help/someone-answers。谢谢!如果我没记错的话,如果你在批量写入中添加超过 500 个“元素”,你会得到一个异常。您可能需要制定策略来处理这种情况。这个答案可能会对您有所帮助:stackoverflow.com/questions/54453776/…
  • 嘿,我正在尝试在类似的功能中解决相同的问题,但在那里遇到了问题。你认为你可以检查这个问题stackoverflow.com/questions/55833485/…
【解决方案2】:

此外,您应该考虑在此处使用 transactions.batched 写入。这允许您定义一系列读/写,然后同时执行它们。这种方法可能很慢的部分原因是您正在执行多次读/写(如果在处理过程中出现故障,这也很糟糕)。 https://firebase.google.com/docs/firestore/manage-data/transactions#transactions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 2022-01-20
    • 2018-07-25
    相关资源
    最近更新 更多