Daniel Wyatt
Daniel Wyatt

Reputation: 1151

When do GCP cloud functions acknowledge pub/sub messages?

I have a cloud function that gets triggered from a pub/sub message. This function never explicitly acknowledges the message in the source code.

So when does this function acknowledge the pub/sub message if the acknowledgement never happens in the source code?

Update: when a function crashes, I understand that a message acknowledgement shouldn't occur and yet a new function invocation for that message never appears in the logs

Reproducible Example

Upvotes: 5

Views: 4550

Answers (2)

guillaume blaquiere
guillaume blaquiere

Reputation: 75990

If the function finish gracefully, the message is acknowledge. If the function exits in error, the message is NACK.


EDIT 1

I have tested with a Go background function. You need to deploy your cloud function with the parameter --retry to allow the messages in error to be retried. Else, the messages aren't retried.

In Go, here the cases where retried are performed:

  • Return an Error (equivalent to exception in Java or Python), status "error" in the logs
  • Perform a log.Fatal() (exit the function (function crash) with a specific log) status "connection error" in the logs
  • Perform an explicit exit, status "connection error" in the logs

Here the code (if interested)

type PubSubMessage struct {
    Data []byte `json:"data"`

}

func PubsubError(ctx context.Context, m PubSubMessage) error {

    switch string(m.Data) {
    case "ok":
        return nil
    case "error":
        return errors.New("it's an error")
    case "fatal":
        log.Fatal("crash")
    case "exit":
        os.Exit(1)
    }
    return nil
}

And how i deployed my Cloud Functions

gcloud beta functions deploy --runtime=go113 --trigger-topic=test-topic \
  --source=function --entry-point=PubsubError --region=us-central1 \
  --retry pubsuberror

Upvotes: 4

al-dann
al-dann

Reputation: 2725

Based on this description: Google Cloud Pub/Sub Triggers

Cloud Functions acks the Pub/Sub message internally upon successful function invocation.

I do understand that documentation quotation as the acknowledgement happens only after the execution of code is finished without any (uncatched) errors.

At the same time, while the execution might still be 'in progress', the Pub/Sub service may make a decision to trigger another cloud function (instance) from the same Pub/Sub message.

Some additional details are in this Issue Tracker dicussion: Cloud Function explicit acknowledgement of a pubsub message

From my point of view, independently from 'successful' or 'not successful' the invocation happened, the cloud function is to be developed in an idempopent way, taking into account 'at least once delivery' paradigm of the Pub/Sub service. In other words the cloud function is to be developed in a such a way, that multiple invocations from one message are handled correctly.

Upvotes: 1

Related Questions