Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdebd63

Browse files
acocuzzoparthea
andauthored
feat: Add grpc Compression argument to channels and methods (#451)
* (feat): Add grpc Compression argument * Add compression arg to channel creation * fix linter errors * fix linter errors * refactor with new lib * reformat * fix tests * add compression after refactor: * fix lint * fix unit tests * fix unit tests * fix operation * remove unused import * remove compression for grpc_gcp.secure_channel call * fix method.py comment * Update grpc_helpers.py Remove experimental disclaimer * Update grpc_helpers_async.py Remove experimental disclaimer * Update google/api_core/operations_v1/operations_client.py Co-authored-by: Anthonios Partheniou <[email protected]> * Update google/api_core/operations_v1/operations_client.py Co-authored-by: Anthonios Partheniou <[email protected]> * Update google/api_core/operations_v1/operations_client.py Co-authored-by: Anthonios Partheniou <[email protected]> * Update google/api_core/operations_v1/operations_async_client.py Co-authored-by: Anthonios Partheniou <[email protected]> * Update google/api_core/operations_v1/operations_async_client.py Co-authored-by: Anthonios Partheniou <[email protected]> * Update google/api_core/operations_v1/operations_async_client.py Co-authored-by: Anthonios Partheniou <[email protected]> * Update google/api_core/operations_v1/operations_async_client.py Co-authored-by: Anthonios Partheniou <[email protected]> * Update google/api_core/operations_v1/operations_client.py Co-authored-by: Anthonios Partheniou <[email protected]> --------- Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 2477ab9 commit bdebd63

16 files changed

+377
-89
lines changed

google/api_core/gapic_v1/method.py

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Helpers for wrapping low-level gRPC methods with common functionality.
1616
1717
This is used by gapic clients to provide common error mapping, retry, timeout,
18-
pagination, and long-running operations to gRPC methods.
18+
compression, pagination, and long-running operations to gRPC methods.
1919
"""
2020

2121
import enum
@@ -38,7 +38,7 @@ class _MethodDefault(enum.Enum):
3838

3939

4040
DEFAULT = _MethodDefault._DEFAULT_VALUE
41-
"""Sentinel value indicating that a retry or timeout argument was unspecified,
41+
"""Sentinel value indicating that a retry, timeout, or compression argument was unspecified,
4242
so the default should be used."""
4343

4444

@@ -72,27 +72,43 @@ class _GapicCallable(object):
7272
after its start, not to be confused with deadline). If ``None``,
7373
this callable will not specify a timeout argument to the low-level
7474
RPC method.
75+
compression (grpc.Compression): The default compression for the callable.
76+
If ``None``, this callable will not specify a compression argument
77+
to the low-level RPC method.
7578
metadata (Sequence[Tuple[str, str]]): Additional metadata that is
7679
provided to the RPC method on every invocation. This is merged with
7780
any metadata specified during invocation. If ``None``, no
7881
additional metadata will be passed to the RPC method.
7982
"""
8083

81-
def __init__(self, target, retry, timeout, metadata=None):
84+
def __init__(
85+
self,
86+
target,
87+
retry,
88+
timeout,
89+
compression,
90+
metadata=None,
91+
):
8292
self._target = target
8393
self._retry = retry
8494
self._timeout = timeout
95+
self._compression = compression
8596
self._metadata = metadata
8697

87-
def __call__(self, *args, timeout=DEFAULT, retry=DEFAULT, **kwargs):
88-
"""Invoke the low-level RPC with retry, timeout, and metadata."""
98+
def __call__(
99+
self, *args, timeout=DEFAULT, retry=DEFAULT, compression=DEFAULT, **kwargs
100+
):
101+
"""Invoke the low-level RPC with retry, timeout, compression, and metadata."""
89102

90103
if retry is DEFAULT:
91104
retry = self._retry
92105

93106
if timeout is DEFAULT:
94107
timeout = self._timeout
95108

109+
if compression is DEFAULT:
110+
compression = self._compression
111+
96112
if isinstance(timeout, (int, float)):
97113
timeout = TimeToDeadlineTimeout(timeout=timeout)
98114

@@ -109,6 +125,8 @@ def __call__(self, *args, timeout=DEFAULT, retry=DEFAULT, **kwargs):
109125
metadata = list(metadata)
110126
metadata.extend(self._metadata)
111127
kwargs["metadata"] = metadata
128+
if self._compression is not None:
129+
kwargs["compression"] = compression
112130

113131
return wrapped_func(*args, **kwargs)
114132

@@ -117,19 +135,21 @@ def wrap_method(
117135
func,
118136
default_retry=None,
119137
default_timeout=None,
138+
default_compression=None,
120139
client_info=client_info.DEFAULT_CLIENT_INFO,
121140
):
122141
"""Wrap an RPC method with common behavior.
123142
124-
This applies common error wrapping, retry, and timeout behavior a function.
125-
The wrapped function will take optional ``retry`` and ``timeout``
143+
This applies common error wrapping, retry, timeout, and compression behavior to a function.
144+
The wrapped function will take optional ``retry``, ``timeout``, and ``compression``
126145
arguments.
127146
128147
For example::
129148
130149
import google.api_core.gapic_v1.method
131150
from google.api_core import retry
132151
from google.api_core import timeout
152+
from grpc import Compression
133153
134154
# The original RPC method.
135155
def get_topic(name, timeout=None):
@@ -138,6 +158,7 @@ def get_topic(name, timeout=None):
138158
139159
default_retry = retry.Retry(deadline=60)
140160
default_timeout = timeout.Timeout(deadline=60)
161+
default_compression = Compression.NoCompression
141162
wrapped_get_topic = google.api_core.gapic_v1.method.wrap_method(
142163
get_topic, default_retry)
143164
@@ -186,6 +207,9 @@ def get_topic(name, timeout=None):
186207
default_timeout (Optional[google.api_core.Timeout]): The default
187208
timeout strategy. Can also be specified as an int or float. If
188209
``None``, the method will not have timeout specified by default.
210+
default_compression (Optional[grpc.Compression]): The default
211+
grpc.Compression. If ``None``, the method will not have
212+
compression specified by default.
189213
client_info
190214
(Optional[google.api_core.gapic_v1.client_info.ClientInfo]):
191215
Client information used to create a user-agent string that's
@@ -194,19 +218,23 @@ def get_topic(name, timeout=None):
194218
metadata will be provided to the RPC method.
195219
196220
Returns:
197-
Callable: A new callable that takes optional ``retry`` and ``timeout``
198-
arguments and applies the common error mapping, retry, timeout,
221+
Callable: A new callable that takes optional ``retry``, ``timeout``,
222+
and ``compression``
223+
arguments and applies the common error mapping, retry, timeout, compression,
199224
and metadata behavior to the low-level RPC method.
200225
"""
201226
func = grpc_helpers.wrap_errors(func)
202-
203227
if client_info is not None:
204228
user_agent_metadata = [client_info.to_grpc_metadata()]
205229
else:
206230
user_agent_metadata = None
207231

208232
return functools.wraps(func)(
209233
_GapicCallable(
210-
func, default_retry, default_timeout, metadata=user_agent_metadata
234+
func,
235+
default_retry,
236+
default_timeout,
237+
default_compression,
238+
metadata=user_agent_metadata,
211239
)
212240
)

google/api_core/gapic_v1/method_async.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""AsyncIO helpers for wrapping gRPC methods with common functionality.
1515
1616
This is used by gapic clients to provide common error mapping, retry, timeout,
17-
pagination, and long-running operations to gRPC methods.
17+
compression, pagination, and long-running operations to gRPC methods.
1818
"""
1919

2020
import functools
@@ -30,19 +30,26 @@ def wrap_method(
3030
func,
3131
default_retry=None,
3232
default_timeout=None,
33+
default_compression=None,
3334
client_info=client_info.DEFAULT_CLIENT_INFO,
3435
):
3536
"""Wrap an async RPC method with common behavior.
3637
3738
Returns:
38-
Callable: A new callable that takes optional ``retry`` and ``timeout``
39-
arguments and applies the common error mapping, retry, timeout,
40-
and metadata behavior to the low-level RPC method.
39+
Callable: A new callable that takes optional ``retry``, ``timeout``,
40+
and ``compression`` arguments and applies the common error mapping,
41+
retry, timeout, metadata, and compression behavior to the low-level RPC method.
4142
"""
4243
func = grpc_helpers_async.wrap_errors(func)
4344

4445
metadata = [client_info.to_grpc_metadata()] if client_info is not None else None
4546

4647
return functools.wraps(func)(
47-
_GapicCallable(func, default_retry, default_timeout, metadata=metadata)
48+
_GapicCallable(
49+
func,
50+
default_retry,
51+
default_timeout,
52+
default_compression,
53+
metadata=metadata,
54+
)
4855
)

google/api_core/grpc_helpers.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import collections
1818
import functools
19+
import logging
1920
import warnings
2021

2122
import grpc
@@ -51,6 +52,8 @@
5152
# The list of gRPC Callable interfaces that return iterators.
5253
_STREAM_WRAP_CLASSES = (grpc.UnaryStreamMultiCallable, grpc.StreamStreamMultiCallable)
5354

55+
_LOGGER = logging.getLogger(__name__)
56+
5457

5558
def _patch_callable_name(callable_):
5659
"""Fix-up gRPC callable attributes.
@@ -276,7 +279,8 @@ def create_channel(
276279
quota_project_id=None,
277280
default_scopes=None,
278281
default_host=None,
279-
**kwargs
282+
compression=None,
283+
**kwargs,
280284
):
281285
"""Create a secure channel with credentials.
282286
@@ -297,6 +301,8 @@ def create_channel(
297301
default_scopes (Sequence[str]): Default scopes passed by a Google client
298302
library. Use 'scopes' for user-defined scopes.
299303
default_host (str): The default endpoint. e.g., "pubsub.googleapis.com".
304+
compression (grpc.Compression): An optional value indicating the
305+
compression method to be used over the lifetime of the channel.
300306
kwargs: Additional key-word args passed to
301307
:func:`grpc_gcp.secure_channel` or :func:`grpc.secure_channel`.
302308
Note: `grpc_gcp` is only supported in environments with protobuf < 4.0.0.
@@ -319,12 +325,18 @@ def create_channel(
319325
)
320326

321327
if HAS_GRPC_GCP: # pragma: NO COVER
328+
if compression is not None and compression != grpc.Compression.NoCompression:
329+
_LOGGER.debug(
330+
"Compression argument is being ignored for grpc_gcp.secure_channel creation."
331+
)
322332
return grpc_gcp.secure_channel(target, composite_credentials, **kwargs)
323-
return grpc.secure_channel(target, composite_credentials, **kwargs)
333+
return grpc.secure_channel(
334+
target, composite_credentials, compression=compression, **kwargs
335+
)
324336

325337

326338
_MethodCall = collections.namedtuple(
327-
"_MethodCall", ("request", "timeout", "metadata", "credentials")
339+
"_MethodCall", ("request", "timeout", "metadata", "credentials", "compression")
328340
)
329341

330342
_ChannelRequest = collections.namedtuple("_ChannelRequest", ("method", "request"))
@@ -351,11 +363,15 @@ def __init__(self, method, channel):
351363
"""List[protobuf.Message]: All requests sent to this callable."""
352364
self.calls = []
353365
"""List[Tuple]: All invocations of this callable. Each tuple is the
354-
request, timeout, metadata, and credentials."""
366+
request, timeout, metadata, compression, and credentials."""
355367

356-
def __call__(self, request, timeout=None, metadata=None, credentials=None):
368+
def __call__(
369+
self, request, timeout=None, metadata=None, credentials=None, compression=None
370+
):
357371
self._channel.requests.append(_ChannelRequest(self._method, request))
358-
self.calls.append(_MethodCall(request, timeout, metadata, credentials))
372+
self.calls.append(
373+
_MethodCall(request, timeout, metadata, credentials, compression)
374+
)
359375
self.requests.append(request)
360376

361377
response = self.response

google/api_core/grpc_helpers_async.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def create_channel(
212212
quota_project_id=None,
213213
default_scopes=None,
214214
default_host=None,
215+
compression=None,
215216
**kwargs
216217
):
217218
"""Create an AsyncIO secure channel with credentials.
@@ -233,6 +234,8 @@ def create_channel(
233234
default_scopes (Sequence[str]): Default scopes passed by a Google client
234235
library. Use 'scopes' for user-defined scopes.
235236
default_host (str): The default endpoint. e.g., "pubsub.googleapis.com".
237+
compression (grpc.Compression): An optional value indicating the
238+
compression method to be used over the lifetime of the channel.
236239
kwargs: Additional key-word args passed to :func:`aio.secure_channel`.
237240
238241
Returns:
@@ -252,7 +255,9 @@ def create_channel(
252255
default_host=default_host,
253256
)
254257

255-
return aio.secure_channel(target, composite_credentials, **kwargs)
258+
return aio.secure_channel(
259+
target, composite_credentials, compression=compression, **kwargs
260+
)
256261

257262

258263
class FakeUnaryUnaryCall(_WrappedUnaryUnaryCall):

google/api_core/operation.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,16 @@ def from_grpc(operation, operations_stub, result_type, grpc_metadata=None, **kwa
315315
operation.
316316
"""
317317
refresh = functools.partial(
318-
_refresh_grpc, operations_stub, operation.name, metadata=grpc_metadata
318+
_refresh_grpc,
319+
operations_stub,
320+
operation.name,
321+
metadata=grpc_metadata,
319322
)
320323
cancel = functools.partial(
321-
_cancel_grpc, operations_stub, operation.name, metadata=grpc_metadata
324+
_cancel_grpc,
325+
operations_stub,
326+
operation.name,
327+
metadata=grpc_metadata,
322328
)
323329
return Operation(operation, refresh, cancel, result_type, **kwargs)
324330

@@ -347,9 +353,13 @@ def from_gapic(operation, operations_client, result_type, grpc_metadata=None, **
347353
operation.
348354
"""
349355
refresh = functools.partial(
350-
operations_client.get_operation, operation.name, metadata=grpc_metadata
356+
operations_client.get_operation,
357+
operation.name,
358+
metadata=grpc_metadata,
351359
)
352360
cancel = functools.partial(
353-
operations_client.cancel_operation, operation.name, metadata=grpc_metadata
361+
operations_client.cancel_operation,
362+
operation.name,
363+
metadata=grpc_metadata,
354364
)
355365
return Operation(operation, refresh, cancel, result_type, **kwargs)

google/api_core/operation_async.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,13 @@ def from_gapic(operation, operations_client, result_type, grpc_metadata=None, **
213213
operation.
214214
"""
215215
refresh = functools.partial(
216-
operations_client.get_operation, operation.name, metadata=grpc_metadata
216+
operations_client.get_operation,
217+
operation.name,
218+
metadata=grpc_metadata,
217219
)
218220
cancel = functools.partial(
219-
operations_client.cancel_operation, operation.name, metadata=grpc_metadata
221+
operations_client.cancel_operation,
222+
operation.name,
223+
metadata=grpc_metadata,
220224
)
221225
return AsyncOperation(operation, refresh, cancel, result_type, **kwargs)

0 commit comments

Comments
 (0)