【发布时间】: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