我目前正在将我的firebase-queue工作人员转移到Firebase的云功能。我的一个员工通过APNS和GCM向设备发送推送通知。为了通过APNS发送推送通知,我使用的是库node-apn,其中我使用以下命令创建到APNS的持久连接
const apnConnection = new apn.Connection(connectionOptions);
然后,每当我收到要发送的任务时,我就可以继续使用apnConnection向设备发送推送通知,而无需每次都重新创建它。
apnConnection.pushNotification(pushNotification, device);
我想问这样的持久连接是否也会在多次调用Firebase函数之间保持,或者我是否需要创建此连接并在每次调用Firebase函数时将其关闭。我的Firebase函数看起来像这样
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(firebaseConfig);
const apnConnection = new apn.Connection(connectionOptions);
exports.verifyCode = functions.database.ref('/tasks/sendPushNotification/{taskId}')
.onWrite(event => {
const taskSnapshot = event.data
if(!taskSnapshot.exists()) {
return
}
const task = taskSnapshot.val()
// Create pushNotification and device from the task
return apnConnection.pushNotification(pushNotification, device);
})我认为这归结于函数是否可以在调用之间保持某种状态,或者它们更像是完全无状态的AWS lambda函数,因为我们只初始化admin SDK一次,所以我认为它在调用之间保持某种状态,但我想在移动代码之前检查一下。
发布于 2017-04-01 23:12:46
虽然大多数情况下,您的函数将在同一容器中调用,但您不能依赖这一点。容器可能会被回收,或者GCF可能会启动额外的容器,以防您的函数被调用的次数超过单个容器的处理能力
https://stackoverflow.com/questions/43155978
复制相似问题