Firebase云端function非常慢

我们正在研究使用新的Firebase云function的应用程序。 目前发生的事情是一个事务放入队列节点。 然后该函数删除该节点并将其放入正确的节点中。 由于能够脱机工作,因此已经实施。

我们目前的问题是function的速度。 函数本身大约需要400ms,所以没关系。 但有时这些函数需要很长时间(大约8秒),而条目已经被添加到队列中了。

我们怀疑服务器需要时间来启动,因为当我们在第一次之后再次执行操作时。 这需要更less的时间。

有没有办法解决这个问题? 在这里我添加了我们的function的代码。 我们怀疑它没有什么问题,但为了以防万一,我们添加了它。

const functions = require('firebase-functions'); const admin = require('firebase-admin'); const database = admin.database(); exports.insertTransaction = functions.database .ref('/userPlacePromotionTransactionsQueue/{userKey}/{placeKey}/{promotionKey}/{transactionKey}') .onWrite(event => { if (event.data.val() == null) return null; // get keys const userKey = event.params.userKey; const placeKey = event.params.placeKey; const promotionKey = event.params.promotionKey; const transactionKey = event.params.transactionKey; // init update object const data = {}; // get the transaction const transaction = event.data.val(); // transfer transaction saveTransaction(data, transaction, userKey, placeKey, promotionKey, transactionKey); // remove from queue data[`/userPlacePromotionTransactionsQueue/${userKey}/${placeKey}/${promotionKey}/${transactionKey}`] = null; // fetch promotion database.ref(`promotions/${promotionKey}`).once('value', (snapshot) => { // Check if the promotion exists. if (!snapshot.exists()) { return null; } const promotion = snapshot.val(); // fetch the current stamp count database.ref(`userPromotionStampCount/${userKey}/${promotionKey}`).once('value', (snapshot) => { let currentStampCount = 0; if (snapshot.exists()) currentStampCount = parseInt(snapshot.val()); data[`userPromotionStampCount/${userKey}/${promotionKey}`] = currentStampCount + transaction.amount; // determines if there are new full cards const currentFullcards = Math.floor(currentStampCount > 0 ? currentStampCount / promotion.stamps : 0); const newStamps = currentStampCount + transaction.amount; const newFullcards = Math.floor(newStamps / promotion.stamps); if (newFullcards > currentFullcards) { for (let i = 0; i < (newFullcards - currentFullcards); i++) { const cardTransaction = { action: "pending", promotion_id: promotionKey, user_id: userKey, amount: 0, type: "stamp", date: transaction.date, is_reversed: false }; saveTransaction(data, cardTransaction, userKey, placeKey, promotionKey); const completedPromotion = { promotion_id: promotionKey, user_id: userKey, has_used: false, date: admin.database.ServerValue.TIMESTAMP }; const promotionPushKey = database .ref() .child(`userPlaceCompletedPromotions/${userKey}/${placeKey}`) .push() .key; data[`userPlaceCompletedPromotions/${userKey}/${placeKey}/${promotionPushKey}`] = completedPromotion; data[`userCompletedPromotions/${userKey}/${promotionPushKey}`] = completedPromotion; } } return database.ref().update(data); }, (error) => { // Log to the console if an error happened. console.log('The read failed: ' + error.code); return null; }); }, (error) => { // Log to the console if an error happened. console.log('The read failed: ' + error.code); return null; }); }); function saveTransaction(data, transaction, userKey, placeKey, promotionKey, transactionKey) { if (!transactionKey) { transactionKey = database.ref('transactions').push().key; } data[`transactions/${transactionKey}`] = transaction; data[`placeTransactions/${placeKey}/${transactionKey}`] = transaction; data[`userPlacePromotionTransactions/${userKey}/${placeKey}/${promotionKey}/${transactionKey}`] = transaction; } 

firebaser在这里

这听起来像是你正在经历一个所谓的冷启动function。

当你的函数在一段时间内没有被执行的时候,云端函数会把它放在一个使用较less资源的模式中。 然后当你再次点击该function时,它将从这个模式恢复环境。 恢复所需的时间包括固定成本(如恢复容器)和部分variables成本(例如,如果使用大量节点模块,则可能需要更长的时间)。

我们不断监视这些操作的性能,以确保开发者体验和资源使用之间的最佳组合。 所以期待这些时间随着时间的推移而改善

好消息是你只能在开发过程中经历这个。 一旦你的function在生产中经常被触发,很有可能他们几乎不会再次受到冷遇。