Skip to content
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

docs(samples): Add code sample for optimistic subscribe #1182

Merged
merged 3 commits into from
Jun 18, 2024

Conversation

mukund-ananthu
Copy link
Contributor

@mukund-ananthu mukund-ananthu commented Jun 4, 2024

@mukund-ananthu mukund-ananthu requested review from a team as code owners June 4, 2024 20:25
@mukund-ananthu mukund-ananthu requested a review from gericdong June 4, 2024 20:25
Copy link

snippet-bot bot commented Jun 4, 2024

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: pubsub Issues related to the googleapis/python-pubsub API. samples Issues that are directly related to samples. labels Jun 4, 2024
@mukund-ananthu mukund-ananthu force-pushed the optimisticSub branch 2 times, most recently from 0bd7cd1 to d4bafff Compare June 4, 2024 20:35
@mukund-ananthu mukund-ananthu self-assigned this Jun 4, 2024
@mukund-ananthu mukund-ananthu marked this pull request as draft June 4, 2024 20:39
@mukund-ananthu mukund-ananthu force-pushed the optimisticSub branch 12 times, most recently from cc540b6 to d16bf30 Compare June 5, 2024 23:38
@mukund-ananthu mukund-ananthu marked this pull request as ready for review June 6, 2024 12:36
@mukund-ananthu mukund-ananthu added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 7, 2024
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 7, 2024
@mukund-ananthu mukund-ananthu added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 14, 2024
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Jun 14, 2024
@mukund-ananthu
Copy link
Contributor Author

@kamalaboulhosn Gentle bump. PTAL when free.

)
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
print("Subscription already exists. Timed out.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we instead say "Successfully subscribed until the timeout passed."? As it is, it sounds like this is an error state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've replaced the statement with the suggested statement.

request={"name": subscription_path, "topic": topic_path}
)

if subscription:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do something if this test fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the call to create_subscription() fails, then it would throw an exception and the control would not arrive at the if subscription check.

If the subscription already exists, returns ``ALREADY_EXISTS``.
If the corresponding topic doesn't exist, returns ``NOT_FOUND``.

The check should always evaluate to true and was added as a precautionary null check. I've now additionally included an else clause that raises a ValueError in case this check fails.

I've now also enclosed the create subscription code in a try / except clause to handle any exceptions thrown by the create_subscription() call. Reasons for not doing this earlier is to keep the sample code simple, similar to the pubsub_create_pull_subscription() sample code in the file:

# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(
request={"name": subscription_path, "topic": topic_path}
)
print(f"Subscription created: {subscription}")
# [END pubsub_create_pull_subscription]

@mukund-ananthu mukund-ananthu force-pushed the optimisticSub branch 4 times, most recently from 76e5004 to acd5472 Compare June 17, 2024 18:52
@mukund-ananthu
Copy link
Contributor Author

@kamalaboulhosn Addressed comments. PTAL when free.

@mukund-ananthu mukund-ananthu merged commit d8e8aa5 into main Jun 18, 2024
28 checks passed
@mukund-ananthu mukund-ananthu deleted the optimisticSub branch June 18, 2024 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: pubsub Issues related to the googleapis/python-pubsub API. samples Issues that are directly related to samples. size: m Pull request size is medium.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add code samples for optimistic subcribe
3 participants