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

fix: Stop using api_core default timeouts in publish since they are broken #1326

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion google/cloud/pubsub_v1/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from google.protobuf import timestamp_pb2

from google.api_core.protobuf_helpers import get_messages
from google.api_core.timeout import ExponentialTimeout

from google.pubsub_v1.types import pubsub as pubsub_gapic_types

Expand Down Expand Up @@ -191,7 +192,12 @@ class PublisherOptions(NamedTuple):
"an instance of :class:`google.api_core.retry.Retry`."
)

timeout: "OptionalTimeout" = gapic_v1.method.DEFAULT # use api_core default
# Use ExponentialTimeout instead of api_core default because the default
# value results in retries with zero deadline.
# Refer https://github.com/googleapis/python-api-core/issues/654
timeout: "OptionalTimeout" = ExponentialTimeout(
initial=5, maximum=60, multiplier=1.3, deadline=600
)
(
"Timeout settings for message publishing by the client. It should be "
"compatible with :class:`~.pubsub_v1.types.TimeoutType`."
Expand Down
13 changes: 12 additions & 1 deletion tests/unit/pubsub_v1/publisher/test_publisher_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import sys

import grpc
import math

# special case python < 3.8
if sys.version_info.major == 3 and sys.version_info.minor < 8:
Expand All @@ -35,6 +36,7 @@
from google.api_core import gapic_v1
from google.api_core import retry as retries
from google.api_core.gapic_v1.client_info import METRICS_METADATA_KEY
from google.api_core.timeout import ExponentialTimeout

from google.cloud.pubsub_v1 import publisher
from google.cloud.pubsub_v1 import types
Expand Down Expand Up @@ -652,6 +654,8 @@ def test_publish_new_batch_needed(creds):
future = client.publish(topic, b"foo", bar=b"baz")
assert future is mock.sentinel.future

call_args = batch_class.call_args

# Check the mocks.
batch_class.assert_called_once_with(
client=mock.ANY,
Expand All @@ -660,8 +664,15 @@ def test_publish_new_batch_needed(creds):
batch_done_callback=None,
commit_when_full=True,
commit_retry=gapic_v1.method.DEFAULT,
commit_timeout=gapic_v1.method.DEFAULT,
commit_timeout=mock.ANY,
)
commit_timeout_arg = call_args[1]["commit_timeout"]
assert isinstance(commit_timeout_arg, ExponentialTimeout)
assert math.isclose(commit_timeout_arg._initial, 5) is True
assert math.isclose(commit_timeout_arg._maximum, 60) is True
assert math.isclose(commit_timeout_arg._multiplier, 1.3) is True
assert math.isclose(commit_timeout_arg._deadline, 600) is True

message_pb = gapic_types.PubsubMessage(data=b"foo", attributes={"bar": "baz"})
wrapper = PublishMessageWrapper(message=message_pb)
batch1.publish.assert_called_once_with(wrapper)
Expand Down
Loading