Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(pubsub): add stop method#9365
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
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dad43aa
feat(pubsub): add stop method
ec7677c
feat(pubsub): add unit-test for stop method
93b5ec8
Add comment for _futures. Change wait() conditions.
a0d82e6
Move attribute comment
210abf0
Make stop() method non-blocking.
99bfc26
Add one more assert for stop() method.
3216d84
Fix comment.
94d3023
Comment fix.
3f53333
Add stopping lock.
b63c176
Spelling mistake fix.
3ef412c
Preventing race conditions while stopping publisher.
b1d515d
Fix test.
2534467
Lock method stop() completely.
45a6bf8
Small refactor.
ba01ec1
Update pubsub/google/cloud/pubsub_v1/publisher/client.py
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -74,6 +74,9 @@ def __init__(self, client, topic, settings, autocommit=True): | ||
| self._state_lock = threading.Lock() | ||
| # These members are all communicated between threads; ensure that | ||
| # any writes to them use the "state lock" to remain atomic. | ||
| # _futures list should remain unchanged after batch | ||
IlyaFaer marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # status changed from ACCEPTING_MESSAGES to any other | ||
| # in order to avoid race conditions | ||
| self._futures = [] | ||
| self._messages = [] | ||
| self._size = 0 | ||
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 |
|---|---|---|
| @@ -134,6 +134,7 @@ def __init__(self, batch_settings=(), **kwargs): | ||
| # messages. One batch exists for each topic. | ||
| self._batch_lock = self._batch_class.make_lock() | ||
| self._batches = {} | ||
| self._is_stopped = False | ||
| @classmethod | ||
| def from_service_account_file(cls, filename, batch_settings=(), **kwargs): | ||
| @@ -187,20 +188,19 @@ def _batch(self, topic, create=False, autocommit=True): | ||
| """ | ||
| # If there is no matching batch yet, then potentially create one | ||
| # and place it on the batches dictionary. | ||
| with self._batch_lock: | ||
| if not create: | ||
| batch = self._batches.get(topic) | ||
| if batch is None: | ||
| create = True | ||
| if create: | ||
| batch = self._batch_class( | ||
| autocommit=autocommit, | ||
| client=self, | ||
| settings=self.batch_settings, | ||
| topic=topic, | ||
| ) | ||
| self._batches[topic] = batch | ||
| if not create: | ||
| batch = self._batches.get(topic) | ||
| if batch is None: | ||
| create = True | ||
| if create: | ||
| batch = self._batch_class( | ||
| autocommit=autocommit, | ||
| client=self, | ||
| settings=self.batch_settings, | ||
| topic=topic, | ||
| ) | ||
| self._batches[topic] = batch | ||
| return batch | ||
| @@ -242,12 +242,17 @@ def publish(self, topic, data, **attrs): | ||
| instance that conforms to Python Standard library's | ||
| :class:`~concurrent.futures.Future` interface (but not an | ||
| instance of that class). | ||
| Raises: | ||
| RuntimeError: | ||
| If called after publisher has been stopped | ||
| by a `stop()` method call. | ||
plamut marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| # Sanity check: Is the data being sent as a bytestring? | ||
| # If it is literally anything else, complain loudly about it. | ||
| if not isinstance(data, six.binary_type): | ||
| raise TypeError( | ||
| "Data being published to Pub/Sub must be sent " "as a bytestring." | ||
| "Data being published to Pub/Sub must be sent as a bytestring." | ||
| ) | ||
| # Coerce all attributes to text strings. | ||
| @@ -266,11 +271,38 @@ def publish(self, topic, data, **attrs): | ||
| message = types.PubsubMessage(data=data, attributes=attrs) | ||
| # Delegate the publishing to the batch. | ||
| batch = self._batch(topic) | ||
| future = None | ||
| while future is None: | ||
| future = batch.publish(message) | ||
| if future is None: | ||
| batch = self._batch(topic, create=True) | ||
| with self._batch_lock: | ||
| if self._is_stopped: | ||
| raise RuntimeError("Cannot publish on a stopped publisher.") | ||
| batch = self._batch(topic) | ||
| future = None | ||
| while future is None: | ||
| future = batch.publish(message) | ||
| if future is None: | ||
| batch = self._batch(topic, create=True) | ||
pradn marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| return future | ||
| def stop(self): | ||
| """Immediately publish all outstanding messages. | ||
| Asynchronously sends all outstanding messages and | ||
| prevents future calls to `publish()`. Method should | ||
| be invoked prior to deleting this `Client()` object | ||
| in order to ensure that no pending messages are lost. | ||
| .. note:: | ||
| This method is non-blocking. Use `Future()` objects | ||
| returned by `publish()` to make sure all publish | ||
| requests completed, either in success or error. | ||
| """ | ||
| with self._batch_lock: | ||
| if self._is_stopped: | ||
| raise RuntimeError("Cannot stop a publisher already stopped.") | ||
| self._is_stopped = True | ||
| for batch in self._batches.values(): | ||
| batch.commit() | ||
30 changes: 30 additions & 0 deletions
30 pubsub/tests/unit/pubsub_v1/publisher/test_publisher_client.py
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
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.