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: consume part of StreamingResponseIterator to support failure while under a retry context #10206

Merged
merged 7 commits into from
Jan 30, 2020
Merged
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion api_core/google/api_core/grpc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ def error_remapped_callable(*args, **kwargs):
class _StreamingResponseIterator(grpc.Call):
def __init__(self, wrapped):
self._wrapped = wrapped
self._stored_first_result = False

# 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._first_result = six.next(self._wrapped)
self._stored_first_result = True
except StopIteration:

Choose a reason for hiding this comment

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

Will this work for the WatchStream? For Watch, the client has to send the first request. We will not receive a response until after this request has been processed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Watch uses ResumableBidiRpc and has its own way of managing recovery. This will have an effect on things like query though.

# ignore stop iteration at this time. This should be handled outside of retry.
pass


def __iter__(self):
"""This iterator is also an iterable that returns itself."""
Expand All @@ -76,8 +88,12 @@ def next(self):
protobuf.Message: A single response from the stream.
"""
try:
return six.next(self._wrapped)
if self._stored_first_result:
self._stored_first_result = False
return self._first_result
return six.next(self._wrapped)
except grpc.RpcError as exc:
# If the stream has already returned data, we cannot recover here.
six.raise_from(exceptions.from_grpc_error(exc), exc)

# Alias needed for Python 2/3 support.
Expand Down