-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix: consume part of StreamingResponseIterator to support failure while under a retry context #10206
Changes from all commits
acd79db
2186441
1c36fde
d2271dd
49fc431
135d9c0
0f77cb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,6 +65,19 @@ class _StreamingResponseIterator(grpc.Call): | |
def __init__(self, wrapped): | ||
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) | ||
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. | ||
pass | ||
except StopIteration: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Watch uses |
||
# 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.""" | ||
return self | ||
|
@@ -76,8 +89,13 @@ def next(self): | |
protobuf.Message: A single response from the stream. | ||
""" | ||
try: | ||
if hasattr(self, "_stored_first_result"): | ||
result = self._stored_first_result | ||
del self._stored_first_result | ||
return 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. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tseaver Under test we have a non-iterable. While it is odd that you would call this without it does seem possible? So added a guard here