Lin Du
Lin Du

Reputation: 102587

Why the message doesn't to be redelivered?

The Acknowledgment Deadline is 10 seconds. When I use asynchronous-pull way to process the message, I don't call message.ack() and message.nack(), wait for the message ack deadline and expect Pub/Sub redeliver this message.

After waiting over 10 seconds, the subscriber doesn't receive the message again. Here is my code:

subscriber:

import { pubsubClient, IMessage, parseMessageData } from '../../googlePubsub';
import { logger } from '../../utils';

const topicName = 'asynchronous-pull-test';
const subName = 'asynchronous-pull-test';
const subscription = pubsubClient.topic(topicName).subscription(subName);
const onMessage = (message: IMessage) => {
  const { data, ...rest } = message;
  const jsonData = parseMessageData(data);
  logger.debug('received message', { arguments: { ...rest, data: jsonData } });

  const publishTime = new Date(message.publishTime).getTime();
  const republishTimestamp = Date.now() - 5 * 1000;

  if (publishTime < republishTimestamp) {
    logger.info('message acked');
    message.ack();
  } else {
    logger.info('push message back to MQ');
  }
};
logger.info('subscribe the MQ');
subscription.on('message', onMessage).on('error', (err: Error) => logger.error(err));

publisher:

const topicName = 'asynchronous-pull-test';

async function main() {
  const messagePayload = { email: faker.internet.email(), campaignId: '1' };
  await pub(topicName, messagePayload);
}

main();

I am using "@google-cloud/pubsub": "^0.19.0",

I expect the subscriber will receive the message again at the ack deadline 10 seconds later. Which means my subscriber receives and processes the message every 10 seconds. Am I wrong?

Upvotes: 1

Views: 832

Answers (2)

Kamal Aboul-Hosn
Kamal Aboul-Hosn

Reputation: 17261

The Google Cloud Pub/Sub client libraries automatically call modifyAckDeadline for messages that are neither acked or nacked for a configurable period of time. In node.js, this is configured via the maxExtension property:

const options = {
  flowControl: {
    maxExtension: 60, // Specified in seconds
  },
};

const subscription = pubsubClient.topic(topicName).subscription(subName, options);

In general, it is not a good practice to not ack/nack a message as a means to delay its redelivery. This will result in the message still counting against the flow control max outstanding messages, meaning it could prevent the delivery of future messages until the originally received messages are acked or nacked. At this time, Cloud Pub/Sub does not have a means by which to delay message redelivery, but it is something under consideration.

Upvotes: 1

Christopher
Christopher

Reputation: 941

You should nack the message to tell Pub/Sub to redeliver it. Documentation

Upvotes: 0

Related Questions