Skip to content

AsyncIO Integration [Part 1] #26

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

Merged
merged 1 commit into from
May 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/retry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ Retry
.. automodule:: google.api_core.retry
:members:
:show-inheritance:

Retry in AsyncIO
----------------

.. automodule:: google.api_core.retry_async
:members:
:show-inheritance:
8 changes: 7 additions & 1 deletion google/api_core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,10 @@ def from_grpc_status(status_code, message, **kwargs):
return error


def _is_informative_grpc_error(rpc_exc):
return hasattr(rpc_exc, "code") and hasattr(rpc_exc, "details")


def from_grpc_error(rpc_exc):
"""Create a :class:`GoogleAPICallError` from a :class:`grpc.RpcError`.

Expand All @@ -454,7 +458,9 @@ def from_grpc_error(rpc_exc):
GoogleAPICallError: An instance of the appropriate subclass of
:class:`GoogleAPICallError`.
"""
if isinstance(rpc_exc, grpc.Call):
# NOTE(lidiz) All gRPC error shares the parent class grpc.RpcError.
# However, check for grpc.RpcError breaks backward compatibility.
if isinstance(rpc_exc, grpc.Call) or _is_informative_grpc_error(rpc_exc):
return from_grpc_status(
rpc_exc.code(), rpc_exc.details(), errors=(rpc_exc,), response=rpc_exc
)
Expand Down
6 changes: 6 additions & 0 deletions google/api_core/gapic_v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

from google.api_core.gapic_v1 import client_info
from google.api_core.gapic_v1 import config
from google.api_core.gapic_v1 import method
from google.api_core.gapic_v1 import routing_header

__all__ = ["client_info", "config", "method", "routing_header"]

if sys.version_info >= (3, 6):
from google.api_core.gapic_v1 import config_async # noqa: F401
__all__.append("config_async")
10 changes: 6 additions & 4 deletions google/api_core/gapic_v1/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _exception_class_for_grpc_status_name(name):
return exceptions.exception_class_for_grpc_status(getattr(grpc.StatusCode, name))


def _retry_from_retry_config(retry_params, retry_codes):
def _retry_from_retry_config(retry_params, retry_codes, retry_impl=retry.Retry):
"""Creates a Retry object given a gapic retry configuration.

Args:
Expand All @@ -70,7 +70,7 @@ def _retry_from_retry_config(retry_params, retry_codes):
exception_classes = [
_exception_class_for_grpc_status_name(code) for code in retry_codes
]
return retry.Retry(
return retry_impl(
retry.if_exception_type(*exception_classes),
initial=(retry_params["initial_retry_delay_millis"] / _MILLIS_PER_SECOND),
maximum=(retry_params["max_retry_delay_millis"] / _MILLIS_PER_SECOND),
Expand Down Expand Up @@ -110,7 +110,7 @@ def _timeout_from_retry_config(retry_params):
MethodConfig = collections.namedtuple("MethodConfig", ["retry", "timeout"])


def parse_method_configs(interface_config):
def parse_method_configs(interface_config, retry_impl=retry.Retry):
"""Creates default retry and timeout objects for each method in a gapic
interface config.

Expand All @@ -120,6 +120,8 @@ def parse_method_configs(interface_config):
an interface named ``google.example.v1.ExampleService`` you would
pass in just that interface's configuration, for example
``gapic_config['interfaces']['google.example.v1.ExampleService']``.
retry_impl (Callable): The constructor that creates a retry decorator
that will be applied to the method based on method configs.

Returns:
Mapping[str, MethodConfig]: A mapping of RPC method names to their
Expand Down Expand Up @@ -151,7 +153,7 @@ def parse_method_configs(interface_config):
if retry_params_name is not None:
retry_params = retry_params_map[retry_params_name]
retry_ = _retry_from_retry_config(
retry_params, retry_codes_map[method_params["retry_codes_name"]]
retry_params, retry_codes_map[method_params["retry_codes_name"]], retry_impl
)
timeout_ = _timeout_from_retry_config(retry_params)

Expand Down
42 changes: 42 additions & 0 deletions google/api_core/gapic_v1/config_async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright 2020 Google LLC
#
# 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.
"""AsyncIO helpers for loading gapic configuration data.

The Google API generator creates supplementary configuration for each RPC
method to tell the client library how to deal with retries and timeouts.
"""

from google.api_core import retry_async
from google.api_core.gapic_v1 import config
from google.api_core.gapic_v1.config import MethodConfig # noqa: F401


def parse_method_configs(interface_config):
"""Creates default retry and timeout objects for each method in a gapic
interface config with AsyncIO semantics.

Args:
interface_config (Mapping): The interface config section of the full
gapic library config. For example, If the full configuration has
an interface named ``google.example.v1.ExampleService`` you would
pass in just that interface's configuration, for example
``gapic_config['interfaces']['google.example.v1.ExampleService']``.

Returns:
Mapping[str, MethodConfig]: A mapping of RPC method names to their
configuration.
"""
return config.parse_method_configs(
interface_config,
retry_impl=retry_async.AsyncRetry)
Loading