Skip to content

Commit ace61eb

Browse files
committed
use _build_retry_error as default param
1 parent cd8323e commit ace61eb

File tree

5 files changed

+29
-49
lines changed

5 files changed

+29
-49
lines changed

google/api_core/retry/retry_base.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,10 @@ def _retry_error_helper(
176176
predicate_fn: Callable[[Exception], bool],
177177
on_error_fn: Callable[[Exception], None] | None,
178178
exc_factory_fn: Callable[
179-
[list[Exception], RetryFailureReason],
179+
[list[Exception], RetryFailureReason, float | None],
180180
tuple[Exception, Exception | None],
181181
],
182+
original_timeout: float | None,
182183
):
183184
"""
184185
Shared logic for handling an error for all retry implementations
@@ -190,17 +191,20 @@ def _retry_error_helper(
190191
Args:
191192
- exc: the exception that was raised
192193
- deadline: the deadline for the retry, calculated as a diff from time.monotonic()
193-
- next_sleep: the calculated next sleep interval
194+
- next_sleep: the next sleep interval
194195
- error_list: the list of exceptions that have been raised so far
195-
- predicate_fn: the predicate that was used to determine if the exception should be retried
196-
- on_error_fn: the callback that was called when the exception was raised
197-
- exc_factory_fn: the callback that was called to build the exception to be raised on terminal failure
196+
- predicate_fn: predicate used to determine if the exception should be retried
197+
- on_error_fn: callback to execute when a retryable error occurs
198+
- exc_factory_fn: callback used to build the exception to be raised on terminal failure
199+
- original_timeout_val: the original timeout value for the retry (in seconds),
200+
to be passed to the exception factory for building an error message
198201
"""
199202
error_list.append(exc)
200203
if not predicate_fn(exc):
201204
final_exc, source_exc = exc_factory_fn(
202205
error_list,
203206
RetryFailureReason.NON_RETRYABLE_ERROR,
207+
original_timeout,
204208
)
205209
raise final_exc from source_exc
206210
if on_error_fn is not None:
@@ -209,6 +213,7 @@ def _retry_error_helper(
209213
final_exc, source_exc = exc_factory_fn(
210214
error_list,
211215
RetryFailureReason.TIMEOUT,
216+
original_timeout,
212217
)
213218
raise final_exc from source_exc
214219
_LOGGER.debug(

google/api_core/retry/retry_streaming.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@ def retry_target_stream(
5555
sleep_generator: Iterable[float],
5656
timeout: Optional[float] = None,
5757
on_error: Optional[Callable[[Exception], None]] = None,
58-
exception_factory: Optional[
59-
Callable[
60-
[List[Exception], RetryFailureReason, Optional[float]],
61-
Tuple[Exception, Optional[Exception]],
62-
]
63-
] = None,
58+
exception_factory: Callable[
59+
[List[Exception], RetryFailureReason, Optional[float]],
60+
Tuple[Exception, Optional[Exception]],
61+
] = _build_retry_error,
6462
init_args: _P.args = (),
6563
init_kwargs: _P.kwargs = {},
6664
**kwargs,
@@ -88,9 +86,8 @@ def retry_target_stream(
8886
It takes a list of all exceptions encountered, a retry.RetryFailureReason
8987
enum indicating the failure cause, and the original timeout value
9088
as arguments. It should return a tuple of the exception to be raised,
91-
along with the cause exception if any.
92-
If not provided, a default implementation will raise a RetryError
93-
on timeout, or the last exception encountered otherwise.
89+
along with the cause exception if any. The default implementation will raise
90+
a RetryError on timeout, or the last exception encountered otherwise.
9491
init_args: Positional arguments to pass to the target function.
9592
init_kwargs: Keyword arguments to pass to the target function.
9693
@@ -110,10 +107,6 @@ def retry_target_stream(
110107
time.monotonic() + timeout if timeout is not None else None
111108
)
112109
error_list: list[Exception] = []
113-
# make a partial with timeout applied
114-
exc_factory = lambda e, t: (exception_factory or _build_retry_error)( # noqa: E731
115-
e, t, timeout
116-
)
117110

118111
for sleep in sleep_generator:
119112
# Start a new retry loop
@@ -129,7 +122,7 @@ def retry_target_stream(
129122
except Exception as exc:
130123
# defer to shared logic for handling errors
131124
_retry_error_helper(
132-
exc, deadline, sleep, error_list, predicate, on_error, exc_factory
125+
exc, deadline, sleep, error_list, predicate, on_error, exception_factory, timeout
133126
)
134127
# if exception not raised, sleep before next attempt
135128
time.sleep(sleep)

google/api_core/retry/retry_streaming_async.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ async def retry_target_stream(
6161
exception_factory: Callable[
6262
[list[Exception], RetryFailureReason, float | None],
6363
tuple[Exception, Exception | None],
64-
]
65-
| None = None,
64+
] = _build_retry_error,
6665
init_args: _P.args = (),
6766
init_kwargs: _P.kwargs = {},
6867
**kwargs,
@@ -90,9 +89,8 @@ async def retry_target_stream(
9089
It takes a list of all exceptions encountered, a retry.RetryFailureReason
9190
enum indicating the failure cause, and the original timeout value
9291
as arguments. It should return a tuple of the exception to be raised,
93-
along with the cause exception if any.
94-
If not provided, a default implementation will raise a RetryError
95-
on timeout, or the last exception encountered otherwise.
92+
along with the cause exception if any. The default implementation will raise
93+
a RetryError on timeout, or the last exception encountered otherwise.
9694
init_args: Positional arguments to pass to the target function.
9795
init_kwargs: Keyword arguments to pass to the target function.
9896
@@ -111,10 +109,6 @@ async def retry_target_stream(
111109
deadline = time.monotonic() + timeout if timeout else None
112110
# keep track of retryable exceptions we encounter to pass in to exception_factory
113111
error_list: list[Exception] = []
114-
# make a partial with timeout applied
115-
exc_factory = lambda e, t: (exception_factory or _build_retry_error)( # noqa: E731
116-
e, t, timeout
117-
)
118112
target_is_generator: bool | None = None
119113

120114
for sleep in sleep_generator:
@@ -181,7 +175,7 @@ async def retry_target_stream(
181175
except Exception as exc:
182176
# defer to shared logic for handling errors
183177
_retry_error_helper(
184-
exc, deadline, sleep, error_list, predicate, on_error, exc_factory
178+
exc, deadline, sleep, error_list, predicate, on_error, exception_factory, timeout
185179
)
186180
# if exception not raised, sleep before next attempt
187181
await asyncio.sleep(sleep)

google/api_core/retry/retry_unary.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ def retry_target(
9191
exception_factory: Callable[
9292
[list[Exception], RetryFailureReason, float | None],
9393
tuple[Exception, Exception | None],
94-
]
95-
| None = None,
94+
] = _build_retry_error,
9695
**kwargs,
9796
):
9897
"""Call a function and retry if it fails.
@@ -117,9 +116,8 @@ def retry_target(
117116
It takes a list of all exceptions encountered, a retry.RetryFailureReason
118117
enum indicating the failure cause, and the original timeout value
119118
as arguments. It should return a tuple of the exception to be raised,
120-
along with the cause exception if any.
121-
If not provided, a default implementation will raise a RetryError
122-
on timeout, or the last exception encountered otherwise.
119+
along with the cause exception if any. The default implementation will raise
120+
a RetryError on timeout, or the last exception encountered otherwise.
123121
deadline (float): DEPRECATED: use ``timeout`` instead. For backward
124122
compatibility, if specified it will override ``timeout`` parameter.
125123
@@ -138,10 +136,6 @@ def retry_target(
138136

139137
deadline = time.monotonic() + timeout if timeout is not None else None
140138
error_list: list[Exception] = []
141-
# make a partial with timeout applied
142-
exc_factory = lambda e, t: (exception_factory or _build_retry_error)( # noqa: E731
143-
e, t, timeout
144-
)
145139

146140
for sleep in sleep_generator:
147141
try:
@@ -155,7 +149,7 @@ def retry_target(
155149
except Exception as exc:
156150
# defer to shared logic for handling errors
157151
_retry_error_helper(
158-
exc, deadline, sleep, error_list, predicate, on_error, exc_factory
152+
exc, deadline, sleep, error_list, predicate, on_error, exception_factory, timeout
159153
)
160154
# if exception not raised, sleep before next attempt
161155
time.sleep(sleep)

google/api_core/retry/retry_unary_async.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ async def retry_target(
101101
exception_factory: Callable[
102102
[list[Exception], RetryFailureReason, float | None],
103103
tuple[Exception, Exception | None],
104-
]
105-
| None = None,
104+
] = _build_retry_error,
106105
**kwargs,
107106
):
108107
"""Await a coroutine and retry if it fails.
@@ -127,9 +126,8 @@ async def retry_target(
127126
It takes a list of all exceptions encountered, a retry.RetryFailureReason
128127
enum indicating the failure cause, and the original timeout value
129128
as arguments. It should return a tuple of the exception to be raised,
130-
along with the cause exception if any.
131-
If not provided, a default implementation will raise a RetryError
132-
on timeout, or the last exception encountered otherwise.
129+
along with the cause exception if any. The default implementation will raise
130+
a RetryError on timeout, or the last exception encountered otherwise.
133131
deadline (float): DEPRECATED use ``timeout`` instead. For backward
134132
compatibility, if set it will override the ``timeout`` parameter.
135133
@@ -148,10 +146,6 @@ async def retry_target(
148146

149147
deadline = time.monotonic() + timeout if timeout is not None else None
150148
error_list: list[Exception] = []
151-
# make a partial with timeout applied
152-
exc_factory = lambda e, t: (exception_factory or _build_retry_error)( # noqa: E731
153-
e, t, timeout
154-
)
155149

156150
for sleep in sleep_generator:
157151
try:
@@ -161,7 +155,7 @@ async def retry_target(
161155
except Exception as exc:
162156
# defer to shared logic for handling errors
163157
_retry_error_helper(
164-
exc, deadline, sleep, error_list, predicate, on_error, exc_factory
158+
exc, deadline, sleep, error_list, predicate, on_error, exception_factory, timeout
165159
)
166160
# if exception not raised, sleep before next attempt
167161
await asyncio.sleep(sleep)

0 commit comments

Comments
 (0)