forked from googleapis/python-bigtable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_helpers.py
More file actions
111 lines (94 loc) · 3.83 KB
/
_helpers.py
File metadata and controls
111 lines (94 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations
from typing import Callable, Any
from inspect import iscoroutinefunction
import time
from google.api_core import exceptions as core_exceptions
from google.cloud.bigtable.exceptions import RetryExceptionGroup
"""
Helper functions used in various places in the library.
"""
def _make_metadata(
table_name: str, app_profile_id: str | None
) -> list[tuple[str, str]]:
"""
Create properly formatted gRPC metadata for requests.
"""
params = []
params.append(f"table_name={table_name}")
if app_profile_id is not None:
params.append(f"app_profile_id={app_profile_id}")
params_str = "&".join(params)
return [("x-goog-request-params", params_str)]
def _attempt_timeout_generator(
per_request_timeout: float | None, operation_timeout: float
):
"""
Generator that yields the timeout value for each attempt of a retry loop.
Will return per_request_timeout until the operation_timeout is approached,
at which point it will return the remaining time in the operation_timeout.
Args:
- per_request_timeout: The timeout value to use for each request, in seconds.
If None, the operation_timeout will be used for each request.
- operation_timeout: The timeout value to use for the entire operationm in seconds.
Yields:
- The timeout value to use for the next request, in seonds
"""
per_request_timeout = (
per_request_timeout if per_request_timeout is not None else operation_timeout
)
deadline = operation_timeout + time.monotonic()
while True:
yield max(0, min(per_request_timeout, deadline - time.monotonic()))
def _convert_retry_deadline(
func: Callable[..., Any],
timeout_value: float | None = None,
retry_errors: list[Exception] | None = None,
):
"""
Decorator to convert RetryErrors raised by api_core.retry into
DeadlineExceeded exceptions, indicating that the underlying retries have
exhaused the timeout value.
Optionally attaches a RetryExceptionGroup to the DeadlineExceeded.__cause__,
detailing the failed exceptions associated with each retry.
Supports both sync and async function wrapping.
Args:
- func: The function to decorate
- timeout_value: The timeout value to display in the DeadlineExceeded error message
- retry_errors: An optional list of exceptions to attach as a RetryExceptionGroup to the DeadlineExceeded.__cause__
"""
timeout_str = f" of {timeout_value:.1f}s" if timeout_value is not None else ""
error_str = f"operation_timeout{timeout_str} exceeded"
def handle_error():
new_exc = core_exceptions.DeadlineExceeded(
error_str,
)
source_exc = None
if retry_errors:
source_exc = RetryExceptionGroup(retry_errors)
new_exc.__cause__ = source_exc
raise new_exc from source_exc
# separate wrappers for async and sync functions
async def wrapper_async(*args, **kwargs):
try:
return await func(*args, **kwargs)
except core_exceptions.RetryError:
handle_error()
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except core_exceptions.RetryError:
handle_error()
return wrapper_async if iscoroutinefunction(func) else wrapper