GeezerVison is showing github.com — feat: support customizable retry and timeout settings on the publishe… · googleapis/python-pubsub@7597604
This repository was archived by the owner on Oct 13, 2026. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings tests/unit/pubsub_v1/publisher
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: UPGRADING.md +42-1Lines changed: 42 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -100,7 +100,7 @@ specified by the API producer. | 100 | 100 | *, | 101 | 101 | project: str = None, | 102 | 102 | retry: retries.Retry = gapic_v1.method.DEFAULT, | 103 | | - timeout: float = None, | | 103 | + timeout: google.pubsub_v1.types.TimeoutType = gapic_v1.method.DEFAULT, | 104 | 104 | metadata: Sequence[Tuple[str, str]] = (), | 105 | 105 | ) -> pagers.ListTopicsPager: | 106 | 106 | ``` | @@ -161,3 +161,44 @@ The publisher and subscriber clients cannot be constructed with `client_config` | 161 | 161 | argument anymore. If you want to customize retry and timeout settings for a particular | 162 | 162 | method, you need to do it upon method invocation by passing the custom `timeout` and | 163 | 163 | `retry` arguments, respectively. | | 164 | + | | 165 | + | | 166 | +## Custom Retry and Timeout settings for Publisher Client | | 167 | + | | 168 | +The ``publisher_options`` parameter to the Publisher Client, as well as all of the | | 169 | +client's methods, now accept custom retry and timeout settings: | | 170 | + | | 171 | +```py | | 172 | +custom_retry = api_core.retry.Retry( | | 173 | + initial=0.250, # seconds (default: 0.1) | | 174 | + maximum=90.0, # seconds (default: 60.0) | | 175 | + multiplier=1.45, # default: 1.3 | | 176 | + deadline=300.0, # seconds (default: 60.0) | | 177 | + predicate=api_core.retry.if_exception_type( | | 178 | + api_core.exceptions.Aborted, | | 179 | + api_core.exceptions.DeadlineExceeded, | | 180 | + api_core.exceptions.InternalServerError, | | 181 | + api_core.exceptions.ResourceExhausted, | | 182 | + api_core.exceptions.ServiceUnavailable, | | 183 | + api_core.exceptions.Unknown, | | 184 | + api_core.exceptions.Cancelled, | | 185 | + ), | | 186 | +) | | 187 | + | | 188 | +custom_timeout=api_core.timeout.ExponentialTimeout( | | 189 | + initial=1.0, | | 190 | + maximum=10.0, | | 191 | + multiplier=1.0, | | 192 | + deadline=300.0, | | 193 | +) | | 194 | + | | 195 | +publisher = pubsub_v1.PublisherClient( | | 196 | + publisher_options = pubsub_v1.types.PublisherOptions( | | 197 | + retry=custom_retry, | | 198 | + timeout=custom_timeout, | | 199 | + ), | | 200 | +) | | 201 | +``` | | 202 | + | | 203 | +The timeout can be either an instance of `google.api_core.timeout.ConstantTimeout`, | | 204 | +or an instance of `google.api_core.timeout.ExponentialTimeout`, as in the example. |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/pubsub_v1/publisher/_batch/thread.py +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,6 +73,9 @@ class Batch(base.Batch): | 73 | 73 | commit_retry (Optional[google.api_core.retry.Retry]): Designation of what | 74 | 74 | errors, if any, should be retried when commiting the batch. If not | 75 | 75 | provided, a default retry is used. | | 76 | + commit_timeout (:class:`~.pubsub_v1.types.TimeoutType`): | | 77 | + The timeout to apply when commiting the batch. If not provided, a | | 78 | + default timeout is used. | 76 | 79 | """ | 77 | 80 |
| 78 | 81 | def __init__( | @@ -83,6 +86,7 @@ def __init__( | 83 | 86 | batch_done_callback=None, | 84 | 87 | commit_when_full=True, | 85 | 88 | commit_retry=gapic_v1.method.DEFAULT, | | 89 | + commit_timeout: gapic_types.TimeoutType = gapic_v1.method.DEFAULT, | 86 | 90 | ): | 87 | 91 | self._client = client | 88 | 92 | self._topic = topic | @@ -106,6 +110,7 @@ def __init__( | 106 | 110 | self._size = self._base_request_size | 107 | 111 |
| 108 | 112 | self._commit_retry = commit_retry | | 113 | + self._commit_timeout = commit_timeout | 109 | 114 |
| 110 | 115 | @staticmethod | 111 | 116 | def make_lock(): | @@ -261,7 +266,10 @@ def _commit(self): | 261 | 266 | try: | 262 | 267 | # Performs retries for errors defined by the retry configuration. | 263 | 268 | response = self._client.api.publish( | 264 | | - topic=self._topic, messages=self._messages, retry=self._commit_retry | | 269 | + topic=self._topic, | | 270 | + messages=self._messages, | | 271 | + retry=self._commit_retry, | | 272 | + timeout=self._commit_timeout, | 265 | 273 | ) | 266 | 274 | except google.api_core.exceptions.GoogleAPIError as exc: | 267 | 275 | # We failed to publish, even after retries, so set the exception on | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/pubsub_v1/publisher/_sequencer/base.py +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 |
| 17 | 17 | import abc | 18 | 18 |
| | 19 | +from google.api_core import gapic_v1 | | 20 | +from google.pubsub_v1 import types as gapic_types | | 21 | + | 19 | 22 |
| 20 | 23 | class Sequencer(metaclass=abc.ABCMeta): | 21 | 24 | """The base class for sequencers for Pub/Sub publishing. A sequencer | @@ -45,14 +48,21 @@ def unpause(self, message): # pragma: NO COVER | 45 | 48 |
| 46 | 49 | @staticmethod | 47 | 50 | @abc.abstractmethod | 48 | | - def publish(self, message, retry=None): # pragma: NO COVER | | 51 | + def publish( | | 52 | + self, | | 53 | + message, | | 54 | + retry=None, | | 55 | + timeout: gapic_types.TimeoutType = gapic_v1.method.DEFAULT, | | 56 | + ): # pragma: NO COVER | 49 | 57 | """ Publish message for this ordering key. | 50 | 58 |
| 51 | 59 | Args: | 52 | 60 | message (~.pubsub_v1.types.PubsubMessage): | 53 | 61 | The Pub/Sub message. | 54 | 62 | retry (Optional[google.api_core.retry.Retry]): | 55 | 63 | The retry settings to apply when publishing the message. | | 64 | + timeout (:class:`~.pubsub_v1.types.TimeoutType`): | | 65 | + The timeout to apply when publishing the message. | 56 | 66 |
| 57 | 67 | Returns: | 58 | 68 | A class instance that conforms to Python Standard library's | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/pubsub_v1/publisher/_sequencer/ordered_sequencer.py +21-4Lines changed: 21 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 21 | 21 | from google.cloud.pubsub_v1.publisher import exceptions | 22 | 22 | from google.cloud.pubsub_v1.publisher._sequencer import base as sequencer_base | 23 | 23 | from google.cloud.pubsub_v1.publisher._batch import base as batch_base | | 24 | +from google.pubsub_v1 import types as gapic_types | 24 | 25 |
| 25 | 26 |
| 26 | 27 | class _OrderedSequencerStatus(str, enum.Enum): | @@ -226,13 +227,19 @@ def unpause(self): | 226 | 227 | raise RuntimeError("Ordering key is not paused.") | 227 | 228 | self._state = _OrderedSequencerStatus.ACCEPTING_MESSAGES | 228 | 229 |
| 229 | | - def _create_batch(self, commit_retry=gapic_v1.method.DEFAULT): | | 230 | + def _create_batch( | | 231 | + self, | | 232 | + commit_retry=gapic_v1.method.DEFAULT, | | 233 | + commit_timeout: gapic_types.TimeoutType = gapic_v1.method.DEFAULT, | | 234 | + ): | 230 | 235 | """ Create a new batch using the client's batch class and other stored | 231 | 236 | settings. | 232 | 237 |
| 233 | 238 | Args: | 234 | 239 | commit_retry (Optional[google.api_core.retry.Retry]): | 235 | 240 | The retry settings to apply when publishing the batch. | | 241 | + commit_timeout (:class:`~.pubsub_v1.types.TimeoutType`): | | 242 | + The timeout to apply when publishing the batch. | 236 | 243 | """ | 237 | 244 | return self._client._batch_class( | 238 | 245 | client=self._client, | @@ -241,16 +248,24 @@ def _create_batch(self, commit_retry=gapic_v1.method.DEFAULT): | 241 | 248 | batch_done_callback=self._batch_done_callback, | 242 | 249 | commit_when_full=False, | 243 | 250 | commit_retry=commit_retry, | | 251 | + commit_timeout=commit_timeout, | 244 | 252 | ) | 245 | 253 |
| 246 | | - def publish(self, message, retry=gapic_v1.method.DEFAULT): | | 254 | + def publish( | | 255 | + self, | | 256 | + message, | | 257 | + retry=gapic_v1.method.DEFAULT, | | 258 | + timeout: gapic_types.TimeoutType = gapic_v1.method.DEFAULT, | | 259 | + ): | 247 | 260 | """ Publish message for this ordering key. | 248 | 261 |
| 249 | 262 | Args: | 250 | 263 | message (~.pubsub_v1.types.PubsubMessage): | 251 | 264 | The Pub/Sub message. | 252 | 265 | retry (Optional[google.api_core.retry.Retry]): | 253 | 266 | The retry settings to apply when publishing the message. | | 267 | + timeout (:class:`~.pubsub_v1.types.TimeoutType`): | | 268 | + The timeout to apply when publishing the message. | 254 | 269 |
| 255 | 270 | Returns: | 256 | 271 | A class instance that conforms to Python Standard library's | @@ -287,13 +302,15 @@ def publish(self, message, retry=gapic_v1.method.DEFAULT): | 287 | 302 | ), "Publish is only allowed in accepting-messages state." | 288 | 303 |
| 289 | 304 | if not self._ordered_batches: | 290 | | - new_batch = self._create_batch(commit_retry=retry) | | 305 | + new_batch = self._create_batch( | | 306 | + commit_retry=retry, commit_timeout=timeout | | 307 | + ) | 291 | 308 | self._ordered_batches.append(new_batch) | 292 | 309 |
| 293 | 310 | batch = self._ordered_batches[-1] | 294 | 311 | future = batch.publish(message) | 295 | 312 | while future is None: | 296 | | - batch = self._create_batch(commit_retry=retry) | | 313 | + batch = self._create_batch(commit_retry=retry, commit_timeout=timeout) | 297 | 314 | self._ordered_batches.append(batch) | 298 | 315 | future = batch.publish(message) | 299 | 316 |
| |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/pubsub_v1/publisher/_sequencer/unordered_sequencer.py +19-4Lines changed: 19 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 15 | 15 | from google.api_core import gapic_v1 | 16 | 16 |
| 17 | 17 | from google.cloud.pubsub_v1.publisher._sequencer import base | | 18 | +from google.pubsub_v1 import types as gapic_types | 18 | 19 |
| 19 | 20 |
| 20 | 21 | class UnorderedSequencer(base.Sequencer): | @@ -77,13 +78,19 @@ def unpause(self): | 77 | 78 | """ Not relevant for this class. """ | 78 | 79 | raise NotImplementedError | 79 | 80 |
| 80 | | - def _create_batch(self, commit_retry=gapic_v1.method.DEFAULT): | | 81 | + def _create_batch( | | 82 | + self, | | 83 | + commit_retry=gapic_v1.method.DEFAULT, | | 84 | + commit_timeout: gapic_types.TimeoutType = gapic_v1.method.DEFAULT, | | 85 | + ): | 81 | 86 | """ Create a new batch using the client's batch class and other stored | 82 | 87 | settings. | 83 | 88 |
| 84 | 89 | Args: | 85 | 90 | commit_retry (Optional[google.api_core.retry.Retry]): | 86 | 91 | The retry settings to apply when publishing the batch. | | 92 | + commit_timeout (:class:`~.pubsub_v1.types.TimeoutType`): | | 93 | + The timeout to apply when publishing the batch. | 87 | 94 | """ | 88 | 95 | return self._client._batch_class( | 89 | 96 | client=self._client, | @@ -92,16 +99,24 @@ def _create_batch(self, commit_retry=gapic_v1.method.DEFAULT): | 92 | 99 | batch_done_callback=None, | 93 | 100 | commit_when_full=True, | 94 | 101 | commit_retry=commit_retry, | | 102 | + commit_timeout=commit_timeout, | 95 | 103 | ) | 96 | 104 |
| 97 | | - def publish(self, message, retry=gapic_v1.method.DEFAULT): | | 105 | + def publish( | | 106 | + self, | | 107 | + message, | | 108 | + retry=gapic_v1.method.DEFAULT, | | 109 | + timeout: gapic_types.TimeoutType = gapic_v1.method.DEFAULT, | | 110 | + ): | 98 | 111 | """ Batch message into existing or new batch. | 99 | 112 |
| 100 | 113 | Args: | 101 | 114 | message (~.pubsub_v1.types.PubsubMessage): | 102 | 115 | The Pub/Sub message. | 103 | 116 | retry (Optional[google.api_core.retry.Retry]): | 104 | 117 | The retry settings to apply when publishing the message. | | 118 | + timeout (:class:`~.pubsub_v1.types.TimeoutType`): | | 119 | + The timeout to apply when publishing the message. | 105 | 120 |
| 106 | 121 | Returns: | 107 | 122 | ~google.api_core.future.Future: An object conforming to | @@ -119,7 +134,7 @@ def publish(self, message, retry=gapic_v1.method.DEFAULT): | 119 | 134 | raise RuntimeError("Unordered sequencer already stopped.") | 120 | 135 |
| 121 | 136 | if not self._current_batch: | 122 | | - newbatch = self._create_batch(commit_retry=retry) | | 137 | + newbatch = self._create_batch(commit_retry=retry, commit_timeout=timeout) | 123 | 138 | self._current_batch = newbatch | 124 | 139 |
| 125 | 140 | batch = self._current_batch | @@ -129,7 +144,7 @@ def publish(self, message, retry=gapic_v1.method.DEFAULT): | 129 | 144 | future = batch.publish(message) | 130 | 145 | # batch is full, triggering commit_when_full | 131 | 146 | if future is None: | 132 | | - batch = self._create_batch(commit_retry=retry) | | 147 | + batch = self._create_batch(commit_retry=retry, commit_timeout=timeout) | 133 | 148 | # At this point, we lose track of the old batch, but we don't | 134 | 149 | # care since it's already committed (because it was full.) | 135 | 150 | self._current_batch = batch | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/pubsub_v1/publisher/client.py +20-2Lines changed: 20 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -230,7 +230,13 @@ def resume_publish(self, topic, ordering_key): | 230 | 230 | sequencer.unpause() | 231 | 231 |
| 232 | 232 | def publish( | 233 | | - self, topic, data, ordering_key="", retry=gapic_v1.method.DEFAULT, **attrs | | 233 | + self, | | 234 | + topic, | | 235 | + data, | | 236 | + ordering_key="", | | 237 | + retry=gapic_v1.method.DEFAULT, | | 238 | + timeout: gapic_types.TimeoutType = gapic_v1.method.DEFAULT, | | 239 | + **attrs | 234 | 240 | ): | 235 | 241 | """Publish a single message. | 236 | 242 |
| @@ -269,6 +275,12 @@ def publish( | 269 | 275 | retry (Optional[google.api_core.retry.Retry]): Designation of what | 270 | 276 | errors, if any, should be retried. If `ordering_key` is specified, | 271 | 277 | the total retry deadline will be changed to "infinity". | | 278 | + If given, it overides any retry passed into the client through | | 279 | + the ``publisher_options`` argument. | | 280 | + timeout (:class:`~.pubsub_v1.types.TimeoutType`): | | 281 | + The timeout for the RPC request. Can be used to override any timeout | | 282 | + passed in through ``publisher_options`` when instantiating the client. | | 283 | +
| 272 | 284 | attrs (Mapping[str, str]): A dictionary of attributes to be | 273 | 285 | sent as metadata. (These may be text strings or byte strings.) | 274 | 286 |
| @@ -331,6 +343,12 @@ def publish( | 331 | 343 | def on_publish_done(future): | 332 | 344 | self._flow_controller.release(message) | 333 | 345 |
| | 346 | + if retry is gapic_v1.method.DEFAULT: # if custom retry not passed in | | 347 | + retry = self.publisher_options.retry | | 348 | + | | 349 | + if timeout is gapic_v1.method.DEFAULT: # if custom timeout not passed in | | 350 | + timeout = self.publisher_options.timeout | | 351 | + | 334 | 352 | with self._batch_lock: | 335 | 353 | if self._is_stopped: | 336 | 354 | raise RuntimeError("Cannot publish on a stopped publisher.") | @@ -347,7 +365,7 @@ def on_publish_done(future): | 347 | 365 |
| 348 | 366 | # Delegate the publishing to the sequencer. | 349 | 367 | sequencer = self._get_or_create_sequencer(topic, ordering_key) | 350 | | - future = sequencer.publish(message, retry=retry) | | 368 | + future = sequencer.publish(message, retry=retry, timeout=timeout) | 351 | 369 | future.add_done_callback(on_publish_done) | 352 | 370 |
| 353 | 371 | # Create a timer thread if necessary to enforce the batching | |
Collapse file Copy file name to clipboardExpand all lines: google/cloud/pubsub_v1/types.py +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 22 | 22 | import proto | 23 | 23 |
| 24 | 24 | from google.api import http_pb2 | | 25 | +from google.api_core import gapic_v1 | 25 | 26 | from google.iam.v1 import iam_policy_pb2 | 26 | 27 | from google.iam.v1 import policy_pb2 | 27 | 28 | from google.iam.v1.logging import audit_data_pb2 | @@ -98,11 +99,13 @@ class LimitExceededBehavior(str, enum.Enum): | 98 | 99 | # This class is used when creating a publisher client to pass in options | 99 | 100 | # to enable/disable features. | 100 | 101 | PublisherOptions = collections.namedtuple( | 101 | | - "PublisherConfig", ["enable_message_ordering", "flow_control"] | | 102 | + "PublisherOptions", ["enable_message_ordering", "flow_control", "retry", "timeout"] | 102 | 103 | ) | 103 | 104 | PublisherOptions.__new__.__defaults__ = ( | 104 | 105 | False, # enable_message_ordering: False | 105 | 106 | PublishFlowControl(), # default flow control settings | | 107 | + gapic_v1.method.DEFAULT, # use default api_core value for retry | | 108 | + gapic_v1.method.DEFAULT, # use default api_core value for timeout | 106 | 109 | ) | 107 | 110 | PublisherOptions.__doc__ = "The options for the publisher client." | 108 | 111 | PublisherOptions.enable_message_ordering.__doc__ = ( | @@ -112,6 +115,14 @@ class LimitExceededBehavior(str, enum.Enum): | 112 | 115 | "Flow control settings for message publishing by the client. By default " | 113 | 116 | "the publisher client does not do any throttling." | 114 | 117 | ) | | 118 | +PublisherOptions.retry.__doc__ = ( | | 119 | + "Retry settings for message publishing by the client. This should be " | | 120 | + "an instance of :class:`google.api_core.retry.Retry`." | | 121 | +) | | 122 | +PublisherOptions.timeout.__doc__ = ( | | 123 | + "Timeout settings for message publishing by the client. It should be compatible " | | 124 | + "with :class:`~.pubsub_v1.types.TimeoutType`." | | 125 | +) | 115 | 126 |
| 116 | 127 | # Define the type class and default values for flow control settings. | 117 | 128 | # | |
|