Google cloud platform 处理同时出现的3条发布子消息,将所有3条消息合并并存储在fire store中
我有3个Pub-Sub触发的云函数,它们接收3条不同的消息。这些文件同时出版Google cloud platform 处理同时出现的3条发布子消息,将所有3条消息合并并存储在fire store中,google-cloud-platform,google-cloud-firestore,google-cloud-functions,Google Cloud Platform,Google Cloud Firestore,Google Cloud Functions,我有3个Pub-Sub触发的云函数,它们接收3条不同的消息。这些文件同时出版 主要数据 子数据1 次级数据2 必须根据某些逻辑将这些消息写入firestore 预期目标: 子数据1和子数据2必须合并,结果应插入主数据文档中。组合子数据1和2后,它知道需要连接自身的主数据路径(fire store文档路径) 问题: 在连接到主数据之前,云函数必须将各自的消息数据存储到firestore中。此外,主数据必须在合并子数据1和2之前插入,以便合并的子数据1和2可以作为额外数据附加到主数据中 我所尝试的:
Sub data 1 comes->looks for sub data 2 in its orphaned path , Combines with it ->
knows the main data path-> attach into it, if main data document is not available yet, get stored into the orphaned collection
or
Sub data 1 comes->looks for sub data 2, data 2 not available. Stored into the Orphaned collection.
If sub data 2 comes,
Sub data 2 comes->looks for sub data 1 , combines with it ,knows the main data path
->attach into it, if main data document is not available yet.stored
into the orphaned collection
当主数据插入firestore时,它将在孤立集合中查找此子数据,如果可用,则将这些数据附加到其中
由于这些消息同时出现,间隔正好是毫秒,因此此逻辑无法按预期工作。这里有两种方法值得考虑。如果您知道需要等待多长时间,您可以使用“睡眠”功能启动需要等待的函数,然后让它们踩水一段时间(这有点老套,在规模上可能会很昂贵,但它是有效的): 一个更好的解决方案可能是放弃后续函数的pub/sub消息,而是将它们切换到http函数,让第一个函数通过一个简单的fetch请求调用它们 从要首先调用的函数:
// ... do some stuff, then fetch when ready
let res = await fetch(
"https://us-central1-projectName.cloudfunctions.net/secondFunction",
{
method: "post",
body: JSON.stringify(body),
headers: {
Authorization: `bearer ${token}`,
"Content-Type": "application/json",
},
}
);
您可以使用承载令牌保护这些功能,因为。消息来自第三方rabbit mq服务器。我们的酒吧订阅了这些主题。如果有2条以上的消息,将这些子数据消息放入pub-sub-pull订阅并从主数据获取,将添加更多逻辑。我使用pub-sub重试和指数后退时间修复了这个问题。子数据1推送订阅将始终写入孤立集合。主要数据将写入其各自的集合。Sub-data 2推送订阅在执行时将始终查找Sub-data 1和主数据。如果其中一个在执行时不可用,它将NACK pub sub消息,因此将在10-15秒后重试。最大重试次数为5次。发布-订阅重试流有点事与愿违。每当推送订阅拒绝消息时,pub-sub就会延迟新消息。考虑到一分钟内有100条消息,由于这个延迟,发布子队列开始填满。
// ... do some stuff, then fetch when ready
let res = await fetch(
"https://us-central1-projectName.cloudfunctions.net/secondFunction",
{
method: "post",
body: JSON.stringify(body),
headers: {
Authorization: `bearer ${token}`,
"Content-Type": "application/json",
},
}
);