首页 > 解决方案 > 两个云函数在一小时内使用超过 10,000 次读取?

问题描述

目前,我正在运行两个云功能。一个向帖子添加信息,而另一个删除旧帖子,例如:

exports.reveal = functions.database.ref('/reveals/{postIDthatWasRevealed}/revealed').onUpdate((change, context) => {
const revealedValue = change.after.val()

if (revealedValue === true) {
   var updates = {}
   const postID = context.params.postIDthatWasRevealed
    console.log(postID)
   return admin.firestore().collection('posters').doc(postID).get().then(snapshot => {
        const value = snapshot.data()
        console.log(value)
       // console.log(value)
        const posterID = value.posterID
        const posterName = value.posterName
        const profileImage = value.profileImage
        const postKey = value.key
        return admin.database().ref('/convoID/' + postID).once('value', (snapshot) => {
        if (snapshot.exists()) {
           const convoIDCollection = snapshot.val()
           for (var child in convoIDCollection) {
               const convoID = child

               updates["/conversations/"+convoID+"/information/reciever/Name"] = posterName
               updates["/conversations/"+convoID+"/information/reciever/profileImage"] = profileImage
               updates["/conversations/"+convoID+"/key"] = postKey
           }
        }
           const currentTime = Date.now()
           const addedTime = currentTime + 172800000

           const batch = admin.firestore().batch()
           const postFireStoreRef = admin.firestore().collection('posts').doc(postID)

           batch.update(postFireStoreRef,{"revealedDate": currentTime})
           batch.update(postFireStoreRef,{"timeOfDeletion": addedTime})
           batch.update(postFireStoreRef,{"information": {"posterID":posterID,"posterName":posterName,"profileImage":profileImage} })
           batch.update(postFireStoreRef,{"key":postKey})

           return batch.commit(), admin.database().ref().update(updates)
        })
   })
}
else {
    return null
}
})

^当获得足够的赞时,该帖子会在帖子中添加信息。帖子存储在 firestore 中,当与帖子关联的 firebase 节点获得足够的点赞数时,将下载一些附加到 firestore 帖子实体的信息。理论上,它甚至不应该读取一次,因为来自 firestore 实体的数据永远不会被下载,只会被修改。运行的第二个函数如下:

exports.hourly_job = functions.pubsub.topic('hourly-tick').onPublish((change,context) => {
    const currentTime = Date.now()
    const getPostsForDate = admin.firestore().collection('posts').where('timeOfDeletion', '<', currentTime)
    return getPostsForDate.get().then(snapshot => {
        const updates = {}
        const batch = admin.firestore().batch()
        snapshot.forEach((doc) => {
            var key = doc.id
            console.log(key)
            const convos =  database().ref('/convoID/' + key).once('value', (snapshot) => {
                 if (snapshot.exists){
                    for (var child in snapshot) {
                        const convoID = child
                        console.log(child+"shit")
                        updates["conversations/" + value] = null
                    }
                 }
             })
             updates["/convoID/"+ key] = null
             updates["/reveals/" + key] = null
             updates["/postDetails/" + key] = null
             const postFireStoreRef = admin.firestore().collection('posts').doc(key)
             const posterRef = admin.firestore().collection('posters').doc(key)
             batch.delete(postFireStoreRef)
             batch.delete(posterRef)
        })
        return admin.database().ref().update(updates), batch.commit()
})
})

每分钟,这都会查询旧帖子的火库。最多可能会返回两到三个帖子,从而导致阅读。然而,在对这些功能进行了一个小时的测试后,Google App Engine Quota 显示一万次读取,而我预计接近 20 到 50 次。此外,一整天只部署了这些功能,因此它们只运行了 87 次。这些功能没有优化吗?有没有办法监控读取操作的来源?

编辑:似乎每次我触发删除功能(实际上更改帖子的时间戳,以便在每分钟调用删除功能时将其删除)我的读取增加了几百...

标签: firebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


推荐阅读