I have a strange problem with an HTTPS Cloud Function that based on specific rules send push notifications to client. In particular, when the Cloud Function starts for the first time, notifications are sent with a delay. On the contrary, after Cold Start, everything works as expected, the push notification is sent straight after.
My code looks like the following (very simplified):
exports.myCloudFunction = functions.https.onRequest(async (request, response) => {
try {
const numberOfImportedItems = await importData();
if (numberOfImportedItems > 0) {
await sendPushNotification();
}
response.status(200).send('OK');
} catch (error) {
response.status(404).send('KO');
}
});
where sendPushNotification is defined the following:
async function sendPushNotification() {
// https://firebase.google.com/docs/functions/tips?hl=en#use_dependencies_wisely
const module = await import('push-notification-module');
// compose the notification here
module.send(notification);
}
It's worth to note that import('push-notification-module') it's not a top level import but a local one since the in certain cases notifications are not sent (based on numberOfImportedItems).
Do you have any suggestion why this happens? Do you have any suggestion on how to mitigate this delay behavior?
Cloud Functions are stateless execution environments which are initialized from scratch, which leads to cold starts. Also as @Frank stated in the Stackoverflow Link,
Also you may review the Tips & Tricks For Cloud Functions documentation to avoid issues when using Cloud Functions..
From the description, it seems like the intended behavior when a function is invoked in some time or a cold start issue. You may keep your function always warm or check the minimum instances configured for your function.
You can refer to this Documentation on how to set a minimum instance limit for existing functions.