-
Notifications
You must be signed in to change notification settings - Fork 233
feat: add timeout option and graceful shutdown to Subscription.close() #2068
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
24c37bb
feat: Add timeout option to Subscription.close()
google-labs-jules[bot] 687212d
docs: revert README change so release-please can do it
feywind da726d0
feat: jules' vibin' is too lo-fi, fix some bad assumptions
feywind 629aa43
samples: typeless a JS sample for close with timeout
feywind a0ba234
feat: add awaitWithTimeout and test
feywind 13e58f2
tests: also test error results without timeout
feywind 5db179e
feat: update for the current spec, test updates coming
feywind 62afe56
tests: misc fixes before further additions
feywind b904884
feat: update Temporal shims to match latest standards
feywind 2cf219f
chore: linter fix
feywind 6e41e4d
feat: update to latest spec doc info, finish unit tests
feywind 49003e9
Merge branch 'main' into feat-close-timeout
feywind ca50c3c
feat: also move the options from close() parameters to subscriber opt…
feywind ce5e917
chore: fix linter errors
feywind f2898a3
samples: update to latest API changes
feywind 073c6d7
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] bcad5e7
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] f28be03
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 9aabe08
Merge branch 'feat-close-timeout' of https://github.com/googleapis/no…
gcf-owl-bot[bot] 8866c98
fix: no need to check isEmpty on remove
feywind 82e0c81
chore: merge remote-tracking branch 'refs/remotes/origin/feat-close-t…
feywind f08ae6d
chore: merge remote-tracking branch 'remotes/origin/main' into feat-c…
feywind 791a7f0
chore: remove unneeded promise skip code
feywind 36e5361
fix: substantially clarify the awaitWithTimeout interface
feywind d3a3489
chore: hoist timeout logic into its own method
feywind a285935
fix: tests were leaking EventEmitter handlers
feywind abeeb6c
chore: change constant to CONSTANT_CASE
feywind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // This is a generated sample, using the typeless sample bot. Please | ||
| // look for the source TypeScript sample (.ts) for modifications. | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * This sample demonstrates how to use the `timeout` option when closing a Pub/Sub | ||
| * subscription using the Node.js client library. The timeout allows for graceful | ||
| * shutdown, attempting to nack any buffered messages before closing. | ||
| * | ||
| * For more information, see the README.md under /pubsub and the documentation | ||
| * at https://cloud.google.com/pubsub/docs. | ||
| */ | ||
|
|
||
| // sample-metadata: | ||
| // title: Close Subscription with Timeout | ||
| // description: Demonstrates closing a subscription with a specified timeout for graceful shutdown. | ||
| // usage: node closeSubscriptionWithTimeout.js <topic-name> <subscription-name> | ||
|
|
||
| // This sample is currently speculative. | ||
| // -START pubsub_close_subscription_with_timeout] | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const { | ||
| PubSub, | ||
| Duration, | ||
| SubscriptionCloseBehaviors, | ||
| } = require('@google-cloud/pubsub'); | ||
|
|
||
| // Creates a client; cache this for further use | ||
| const pubsub = new PubSub(); | ||
|
|
||
| async function closeSubscriptionWithTimeout( | ||
| topicNameOrId, | ||
| subscriptionNameOrId, | ||
| ) { | ||
| const topic = pubsub.topic(topicNameOrId); | ||
|
|
||
| // Closes the subscription immediately, not waiting for anything. | ||
| let subscription = topic.subscription(subscriptionNameOrId, { | ||
| closeOptions: { | ||
| timeout: Duration.from({seconds: 0}), | ||
| }, | ||
| }); | ||
| await subscription.close(); | ||
|
|
||
| // Shuts down the gRPC connection, and waits for just before the timeout | ||
| // to send nacks for buffered messages. If `timeout` were missing, this | ||
| // would wait for the maximum leasing timeout. | ||
| subscription = topic.subscription(subscriptionNameOrId, { | ||
| closeOptions: { | ||
| behavior: SubscriptionCloseBehaviors.WaitForProcessing, | ||
| timeout: Duration.from({seconds: 10}), | ||
| }, | ||
| }); | ||
| await subscription.close(); | ||
|
|
||
| // Shuts down the gRPC connection, sends nacks for buffered messages, and waits | ||
| // through the timeout for nacks to send. | ||
| subscription = topic.subscription(subscriptionNameOrId, { | ||
| closeOptions: { | ||
| behavior: SubscriptionCloseBehaviors.NackImmediately, | ||
| timeout: Duration.from({seconds: 10}), | ||
| }, | ||
| }); | ||
| await subscription.close(); | ||
| } | ||
| // -END pubsub_close_subscription_with_timeout] | ||
|
|
||
| // Presumes topic and subscription have been created prior to running the sample. | ||
| // If you uncomment the cleanup code above, the sample will delete them afterwards. | ||
| function main( | ||
| topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID', | ||
| subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID', | ||
| ) { | ||
| closeSubscriptionWithTimeout(topicNameOrId, subscriptionNameOrId).catch( | ||
| err => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| /** | ||
| * This sample demonstrates how to use the `timeout` option when closing a Pub/Sub | ||
| * subscription using the Node.js client library. The timeout allows for graceful | ||
| * shutdown, attempting to nack any buffered messages before closing. | ||
| * | ||
| * For more information, see the README.md under /pubsub and the documentation | ||
| * at https://cloud.google.com/pubsub/docs. | ||
| */ | ||
|
|
||
| // sample-metadata: | ||
| // title: Close Subscription with Timeout | ||
| // description: Demonstrates closing a subscription with a specified timeout for graceful shutdown. | ||
| // usage: node closeSubscriptionWithTimeout.js <topic-name> <subscription-name> | ||
|
|
||
| // This sample is currently speculative. | ||
| // -START pubsub_close_subscription_with_timeout] | ||
|
|
||
| // Imports the Google Cloud client library | ||
| import { | ||
| PubSub, | ||
| Duration, | ||
| SubscriptionCloseBehaviors, | ||
| } from '@google-cloud/pubsub'; | ||
|
|
||
| // Creates a client; cache this for further use | ||
| const pubsub = new PubSub(); | ||
|
|
||
| async function closeSubscriptionWithTimeout( | ||
| topicNameOrId: string, | ||
| subscriptionNameOrId: string, | ||
| ) { | ||
| const topic = pubsub.topic(topicNameOrId); | ||
|
|
||
| // Closes the subscription immediately, not waiting for anything. | ||
| let subscription = topic.subscription(subscriptionNameOrId, { | ||
| closeOptions: { | ||
| timeout: Duration.from({seconds: 0}), | ||
| }, | ||
| }); | ||
| await subscription.close(); | ||
|
|
||
| // Shuts down the gRPC connection, and waits for just before the timeout | ||
| // to send nacks for buffered messages. If `timeout` were missing, this | ||
| // would wait for the maximum leasing timeout. | ||
| subscription = topic.subscription(subscriptionNameOrId, { | ||
| closeOptions: { | ||
| behavior: SubscriptionCloseBehaviors.WaitForProcessing, | ||
| timeout: Duration.from({seconds: 10}), | ||
| }, | ||
| }); | ||
| await subscription.close(); | ||
|
|
||
| // Shuts down the gRPC connection, sends nacks for buffered messages, and waits | ||
| // through the timeout for nacks to send. | ||
| subscription = topic.subscription(subscriptionNameOrId, { | ||
| closeOptions: { | ||
| behavior: SubscriptionCloseBehaviors.NackImmediately, | ||
| timeout: Duration.from({seconds: 10}), | ||
| }, | ||
| }); | ||
| await subscription.close(); | ||
| } | ||
| // -END pubsub_close_subscription_with_timeout] | ||
|
|
||
| // Presumes topic and subscription have been created prior to running the sample. | ||
| // If you uncomment the cleanup code above, the sample will delete them afterwards. | ||
| function main( | ||
| topicNameOrId = 'YOUR_TOPIC_NAME_OR_ID', | ||
| subscriptionNameOrId = 'YOUR_SUBSCRIPTION_NAME_OR_ID', | ||
| ) { | ||
| closeSubscriptionWithTimeout(topicNameOrId, subscriptionNameOrId).catch( | ||
| err => { | ||
| console.error(err.message); | ||
| process.exitCode = 1; | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.