I'm triggering an HTTP Cloud Function, which then publishes a message to my PubSub topic. When publishing every few seconds, this works quite quickly, usually with <1s delay. However, if I am NOT publishing anything for a while (say 5 minutes), the publishing happens with a long delay (~20s).
How can I make this consistently quick? Ideas:
- Somehow configure Pub/Sub publisher to do so immediatelly. Couldn't find such argument in API documentation, though.
- Change the publisher function.
Publisher function:
"use strict";
const PubSub = require("@google-cloud/pubsub");
const pubsub = PubSub({
projectId: "pubsub-forwarders"
});
exports.testFunction = function testFunction(req, res) {
publishMessage("testtopic", "testmessage");
res.status(200).send();
}
function publishMessage(topicName, data) {
const topic = pubsub.topic(topicName);
const publisher = topic.publisher();
const dataBuffer = Buffer.from(data);
return publisher.publish(dataBuffer)
.then((results) => {
console.log("Message", JSON.stringify(results), "published.");
return JSON.stringify(results);
});
}
Cloud functions logs:
16:57:05.938 Function execution started
16:57:05.948 Function execution took 11 ms, finished with status code: 200
16:57:22.987 Message "179492329652563" published.
Thanks a lot!