2
votes

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!

1
Same slowness here, but on every call. Have you resolved this?Spiff

1 Answers

1
votes

Reason is the Google Cloud Function is downing the instance after response sent.

Just change asynchronous publishing to synchronous and send response after success publish. It shortens publication timing to tens of milliseconds.

"use strict";
const PubSub = require("@google-cloud/pubsub");
const pubsub = PubSub({
    projectId: "pubsub-forwarders"
});

exports.testFunction = async function testFunction(req, res) {
    const messageId = await publishMessage("testtopic", "testmessage");
    console.log("Message ", messageId, " published.");
    res.status(200).send();
}

async function publishMessage(topicName, data) {
    const topic = pubsub.topic(topicName);
    const publisher = topic.publisher();
    const dataBuffer = Buffer.from(data);
    return await publisher.publish(dataBuffer);
}

See the reference: https://cloud.google.com/functions/docs/bestpractices/tips#do_not_start_background_activities