热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

javascript–运行一堆Firebase查询后60秒内Firebase超时的云功能

我正在使用Firebase作为群组协作应用程序(如Whatsapp),我正在使用云功能来确定哪些手机通讯录也在使用我的应用程序(再次类似于Whatsapp).云函数运行正常,直到我

我正在使用Firebase作为群组协作应用程序(如Whatsapp),我正在使用云功能来确定哪些手机通讯录也在使用我的应用程序(再次类似于Whatsapp).云函数运行正常,直到我开始在函数日志中看到以下日志以进行某些调用.

函数执行耗时60023 ms,状态为:’timeout’

我做了一些调试,发现对于这个特定的用户,他在手机的通讯录上有很多联系人,所以很明显,要弄清楚哪些联系人正在使用该应用程序所需的工作也增加到了一个点,它花了超过60秒以下是Cloud Function的代码

// contactsData is an array of contacts on the user's phone
// Each contact can contain one more phone numbers which are
// present in the phoneNumbers array. So, essentially, we need
// to query over all the phone numbers in the user's contact book
contactsData.forEach((contact) => {
contact.phoneNumbers.forEach((phoneNumber) => {
// Find if user with this phoneNumber is using the app
// Check against mobileNumber and mobileNumberWithCC
promises.push(ref.child('users').orderByChild("mobileNumber").
equalTo(phoneNumber.number).once("value").then(usersSnapshot => {
// usersSnapshot should contain just one entry assuming
// that the phoneNumber will be unique to the user
if(!usersSnapshot.exists()) {
return null
}
var user = null
usersSnapshot.forEach(userSnapshot => {
user = userSnapshot.val()
})
return {
name: contact.name,
mobileNumber: phoneNumber.number,
id: user.id
}
}))
promises.push(ref.child('users').orderByChild("mobileNumberWithCC").
equalTo(phoneNumber.number).once("value").then(usersSnapshot => {
// usersSnapshot should contain just one entry assuming
// that the phoneNumber will be unique to the user
if(!usersSnapshot.exists()) {
return null
}
var user = null
usersSnapshot.forEach(userSnapshot => {
user = userSnapshot.val()
})
return {
name: contact.name,
mobileNumber: phoneNumber.number,
id: user.id
}
}))
});
});
return Promise.all(promises)
}).then(allCOntacts=> {
// allContacts is an array of nulls and contacts using the app
// Get rid of null and any duplicate entries in the returned array
currentCOntacts= arrayCompact(allContacts)
// Create contactsObj which will the user's contacts that are using the app
currentContacts.forEach(cOntact=> {
contactsObj[contact.id] = contact
})
// Return the currently present contacts
return ref.child('userInfos').child(uid).child('contacts').once('value')
}).then((contactsSnapshot) => {
if(contactsSnapshot.exists()) {
contactsSnapshot.forEach((contactSnapshot) => {
previousContacts.push(contactSnapshot.val())
})
}
// Update the contacts on firease asap after reading the previous contacts
ref.child('userInfos').child(uid).child('contacts').set(contactsObj)
// Figure out the new, deleted and renamed contacts
newCOntacts= arrayDifferenceWith(currentContacts, previousContacts,
(obj1, obj2) => (obj1.id === obj2.id))
deletedCOntacts= arrayDifferenceWith(previousContacts, currentContacts,
(obj1, obj2) => (obj1.id === obj2.id))
renamedCOntacts= arrayIntersectionWith(currentContacts, previousContacts,
(obj1, obj2) => (obj1.id === obj2.id && obj1.name !== obj2.name))
// Create the deletedContactsObj to store on firebase
deletedContacts.forEach((deletedContact) => {
deletedContactsObj[deletedContact.id] = deletedContact
})
// Get the deleted contacts
return ref.child('userInfos').child(uid).child('deletedContacts').once('value')
}).then((deletedContactsSnapshot) => {
if(deletedContactsSnapshot.exists()) {
deletedContactsSnapshot.forEach((deletedContactSnapshot) => {
previouslyDeletedContacts.push(deletedContactSnapshot.val())
})
}
// Contacts that were previously deleted but now added again
restoredCOntacts= arrayIntersectionWith(newContacts, previouslyDeletedContacts,
(obj1, obj2) => (obj1.id === obj2.id))
// Removed the restored contacts from the deletedContacts
restoredContacts.forEach((restoredContact) => {
deletedContactsObj[restoredContact.id] = null
})
// Update groups using any of the deleted, new or renamed contacts
return ContactsHelper.processContactsData(uid, deletedContacts, newContacts, renamedContacts)
}).then(() => {
// Set after retrieving the previously deletedContacts
return ref.child('userInfos').child(uid).child('deletedContacts').update(deletedContactsObj)
})

以下是一些示例数据

// This is a sample contactsData
[
{
"phoneNumbers": [
{
"number": "12324312321",
"label": "home"
},
{
"number": "2322412132",
"label": "work"
}
],
"givenName": "blah5",
"familyName": "",
"middleName": ""
},
{
"phoneNumbers": [
{
"number": "1231221221",
"label": "mobile"
}
],
"givenName": "blah3",
"familyName": "blah4",
"middleName": ""
},
{
"phoneNumbers": [
{
"number": "1234567890",
"label": "mobile"
}
],
"givenName": "blah1",
"familyName": "blah2",
"middleName": ""
}
]
// This is how users are stored on Firebase. This could a lot of users
"users": {
"id1" : {
"countryCode" : "91",
"id" : "id1",
"mobileNumber" : "1231211232",
"mobileNumberWithCC" : "911231211232",
"name" : "Varun"
},
"id2" : {
"countryCode" : "1",
"id" : "id2",
"mobileNumber" : "2342112133",
"mobileNumberWithCC" : "12342112133",
"name" : "Ashish"
},
"id3" : {
"countryCode" : "1",
"id" : "id3",
"mobileNumber" : "123213421",
"mobileNumberWithCC" : "1123213421",
"name" : "Pradeep Singh"
}
}

在这种特殊情况下,contactsData包含1046个条目,对于其中一些条目,有两个phoneNumbers.所以,我们假设我需要检查总共1500个电话号码.我正在创建查询以与数据库中的用户的mobileNumber和mobileNumberWithCC进行比较.因此,在承诺完成之前,函数将总共有3000个查询,我猜测它需要超过60秒来完成所有这些查询,因此云函数超时.

我的几个问题是:

>所有这些查询预计需要超过60秒吗?考虑到它在Firebase基础架构中运行,我期待它完成得更快.
>有没有办法增加函数的超时限制?我目前正在使用Blaze计划.

我还要感谢上述功能的任何替代实施建议,以减轻问题.谢谢!

解决方法:

如果无法避免查询这么多数据,可以使用左侧的“函数”产品更改项目的Cloud Console中函数的超时.目前,您必须在每次新部署时重置超时.


推荐阅读
  • 本题探讨如何通过最大流算法解决农场排水系统的设计问题。题目要求计算从水源点到汇合点的最大水流速率,使用经典的EK(Edmonds-Karp)和Dinic算法进行求解。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 毕业设计:基于机器学习与深度学习的垃圾邮件(短信)分类算法实现
    本文详细介绍了如何使用机器学习和深度学习技术对垃圾邮件和短信进行分类。内容涵盖从数据集介绍、预处理、特征提取到模型训练与评估的完整流程,并提供了具体的代码示例和实验结果。 ... [详细]
  • 本题要求将由小写字母组成的字符串划分为多个片段,确保每个字母只出现在一个片段中。目标是生成尽可能多的片段,并返回每个片段的长度列表。本文将详细解释问题描述、解题思路及代码实现。 ... [详细]
  • 数据结构入门:栈的基本概念与操作
    本文详细介绍了栈这一重要的数据结构,包括其基本概念、顺序存储结构、栈的基本操作(如入栈、出栈、清空栈和销毁栈),以及如何利用栈实现二进制到十进制的转换。通过具体代码示例,帮助读者更好地理解和应用栈的相关知识。 ... [详细]
  • 本文介绍了 Winter-1-C A + B II 问题的详细解题思路和测试数据。该问题要求计算两个大整数的和,并输出结果。我们将深入探讨如何处理大整数运算,确保在给定的时间和内存限制下正确求解。 ... [详细]
  • 本题探讨了在一个有向图中,如何根据特定规则将城市划分为若干个区域,使得每个区域内的城市之间能够相互到达,并且划分的区域数量最少。题目提供了时间限制和内存限制,要求在给定的城市和道路信息下,计算出最少需要划分的区域数量。 ... [详细]
  • QUIC协议:快速UDP互联网连接
    QUIC(Quick UDP Internet Connections)是谷歌开发的一种旨在提高网络性能和安全性的传输层协议。它基于UDP,并结合了TLS级别的安全性,提供了更高效、更可靠的互联网通信方式。 ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • 本文详细介绍了中央电视台电影频道的节目预告,并通过专业工具分析了其加载方式,确保用户能够获取最准确的电视节目信息。 ... [详细]
  • Codeforces Round #566 (Div. 2) A~F个人题解
    Dashboard-CodeforcesRound#566(Div.2)-CodeforcesA.FillingShapes题意:给你一个的表格,你 ... [详细]
  • 本文探讨了在Java中实现系统托盘最小化的两种方法:使用SWT库和JDK6自带的功能。通过这两种方式,开发者可以创建跨平台的应用程序,使窗口能够最小化到系统托盘,并提供丰富的交互功能。 ... [详细]
  • 本题旨在通过给定的评级信息,利用拓扑排序和并查集算法来确定全球 Tetris 高手排行榜。题目要求判断是否可以根据提供的信息生成一个明确的排名表,或者是否存在冲突或信息不足的情况。 ... [详细]
  • 本文深入探讨了POJ2762问题,旨在通过强连通分量缩点和单向连通性的判断方法,解决有向图中任意两点之间的可达性问题。文章详细介绍了算法原理、实现步骤,并附带完整的代码示例。 ... [详细]
author-avatar
mobiledu2502875213
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有