-
Notifications
You must be signed in to change notification settings - Fork 227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: closing the subscription stream without clearing the local inventory and flushing ACKs #725
Comments
@ide thanks for the detailed write up! I think I would be in favor of adding an alternative method to @kamalaboulhosn @bcoe any thoughts here? |
(Just shifting assignee, will leave to @feywind to determine path forward.) |
I'd like to take a further look at this after we've got 2.x out the door. |
Just bumping this as still in the queue. |
I was looking for the same problem today. Our workaround for now is this:
Helps us a bit to keep the number of locally queued messages low but it also says that pubsub might just ignore that value. So... :) |
Moving this into our internal backlog, but feel free to keep adding comments if needed here. |
Any update on this? Is there any way to gracefully drain a subscription in the library now? Either of the solutions proposed here would be great. |
I think it is great idea, indeed as user of library i would expect library that has some internal state, automatically listen and obey SIGTERM by at least preparing for shutdown and releasing any locally queued unprocessed messages cleanly, so that only message that was currently processed would end with NACK and go to incremental backoff. |
…ce - remove annotation (googleapis#725) This PR was generated using Autosynth. 🌈 Synth log will be available here: https://source.cloud.google.com/results/invocations/d5e01c0b-0d2d-4da4-b495-86674ea6021f/targets
A slightly hacky workaround to allow the inventory to drain without retrieving new Pub/Sub messages is to do the following: subscription._subscriber._stream.resume = () => {};
subscription._subscriber._stream.pause(); This works by:
Then, if needed, you can wait until the inventory is empty by checking its while (subscription._subscriber._inventory._messages.size > 0) {
await sleep(1000);
} |
Is your feature request related to a problem? Please describe.
I am looking to gracefully shutdown a service (ex: during rolling deployments) by draining remaining Pub/Sub messages without accepting new ones. Currently, this library offers an async
subscription.close()
method, which (1) closes the subscription stream, (2) clears the inventory of locally buffered messages, and (3) waits to flush all ACKs/NACKs:nodejs-pubsub/src/subscriber.ts
Lines 297 to 308 in f8203ef
The issue with
close()
is that the local inventory (in theLeaseManager
instance) is cleared, which means those messages won't get handled until their ack deadline expires and the Pub/Sub service redelivers the messages to a new subscriber.Describe the solution you'd like
I would like to have an API to help with gracefully draining the Pub/Sub subscription. Here are two proposals:
subscription.pause():
pauses the underlying pull stream so that it doesn't fetch new messages from the Pub/Sub service. The caller of this API can then pause the subscription, handle the remaining messages until the inventory is drained (perhaps expose another event to learn when the inventory is empty), and then callclose()
to flush all the ACKs.subscription.close()
drain the inventory: callingclose()
would close the underlying pull stream, wait for the existing inventory to become empty instead of clearing it, and then wait for the ACKs to be flushed.Describe alternatives you've considered
In the meantime, I have a separate leasing system that deduplicates messages across Pub/Sub subscriber instances. (This was necessary because Pub/Sub has at-least-once semantics.) I could reduce the ack deadline for messages to something short, and rely on the leasing system to guard against the higher likelihood of duplicate messages.
With the shorter ack deadline, calling
close()
will still cause messages in the orphaned inventory to wait until their ack deadlines expire, but the deadlines will be shorter, reducing the impact of this issue.The text was updated successfully, but these errors were encountered: