Cur*_*rse 3 firebase firebase-realtime-database google-cloud-functions
我看到了几个关于这个问题的主题,但有时由于很少的错误,它是公正的。
这对我来说仍然是一个真正的问题,我的经验很少:
const addRoom = functions.region('europe-west1').https.onCall((data, context) => {
console.log("a")
return Promise.resolve();
});
Run Code Online (Sandbox Code Playgroud)
没有 Promise.resolve() 的结果相同:
const addRoom = functions.region('europe-west1').https.onCall((data, context) => {
console.log("a")
});
Run Code Online (Sandbox Code Playgroud)
(当然,我在客户端只调用了一次该函数)
我创建了一个 Web 应用程序并进行了一些测试:来自云函数的 538 次调用,而不应超过 100 次......我简直不敢相信这个统计数据,这是不可能的。这是一个严重的问题。
该怎么办 ?
我处理这个问题的方法是在 Firestore 中创建一个集合,该集合记录eventIds然后在每次我使用我想成为幂等的云函数时进行检查。
//Check if exists in event log
async function isIdempotenceOk(eventId) {
console.log(eventId);
let eventDoc = await
admin.firestore().collection('events').doc(eventId).get();
if (eventDoc.exists) {
console.log('Event already processed');
return false;
} else {
await admin.firestore().collection('events').doc(eventId).set({ eventId: eventId });
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以在你的其他函数中调用它,如下所示:
const addRoom = functions.region('europe-west1').https.onCall((data, context) => {
console.log("a")
if(!await isIdempotenceOk(data.eventId)){return null;}
console.log('ok to continue')
return Promise.resolve();
});
Run Code Online (Sandbox Code Playgroud)
这确实意味着您在 Firestore 中有一个集合,它为您执行此检查的每个函数调用,并且可能有另一种方法来存储eventIds要检查的对象。
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |