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

feat: allow disabling response stream pre-fetch #30

Merged
merged 5 commits into from Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions google/api_core/grpc_helpers.py
Expand Up @@ -62,14 +62,15 @@ def error_remapped_callable(*args, **kwargs):


class _StreamingResponseIterator(grpc.Call):
def __init__(self, wrapped):
def __init__(self, wrapped, prefetch_first_result=True):
self._wrapped = wrapped

# This iterator is used in a retry context, and returned outside after init.
# gRPC will not throw an exception until the stream is consumed, so we need
# to retrieve the first result, in order to fail, in order to trigger a retry.
try:
self._stored_first_result = six.next(self._wrapped)
if prefetch_first_result:
self._stored_first_result = six.next(self._wrapped)
except TypeError:
# It is possible the wrapped method isn't an iterable (a grpc.Call
# for instance). If this happens don't store the first result.
Expand Down Expand Up @@ -141,7 +142,12 @@ def _wrap_stream_errors(callable_):
def error_remapped_callable(*args, **kwargs):
try:
result = callable_(*args, **kwargs)
return _StreamingResponseIterator(result)
# Auto-fetching the first result causes PubSub client's streaming pull
# to hang when re-opening the stream, thus we need examine the hacky
# hidden flag to see if pre-fetching is disabled.
# https://github.com/googleapis/python-pubsub/issues/93#issuecomment-630762257
prefetch_first = getattr(callable_, "_prefetch_first_result_", True)
return _StreamingResponseIterator(result, prefetch_first_result=prefetch_first)
except grpc.RpcError as exc:
six.raise_from(exceptions.from_grpc_error(exc), exc)

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_grpc_helpers.py
Expand Up @@ -80,6 +80,19 @@ def test_wrap_stream_okay():
assert responses == expected_responses


def test_wrap_stream_prefetch_disabled():
responses = [1, 2, 3]
iter_responses = iter(responses)
callable_ = mock.Mock(spec=["__call__"], return_value=iter_responses)
callable_._prefetch_first_result_ = False

wrapped_callable = grpc_helpers._wrap_stream_errors(callable_)
wrapped_callable(1, 2, three="four")

assert list(iter_responses) == responses # no items should have been pre-fetched
callable_.assert_called_once_with(1, 2, three="four")


def test_wrap_stream_iterable_iterface():
response_iter = mock.create_autospec(grpc.Call, instance=True)
callable_ = mock.Mock(spec=["__call__"], return_value=response_iter)
Expand Down