|
3 | 3 | import socket |
4 | 4 | import ssl |
5 | 5 | from contextlib import contextmanager |
| 6 | +from typing import Any, Sequence, Tuple |
6 | 7 | from urllib.parse import urlparse |
7 | 8 |
|
8 | 9 | import grpc |
@@ -34,19 +35,31 @@ def ssl_channel_credentials(target: str, tls_config): |
34 | 35 | return grpc.ssl_channel_credentials() |
35 | 36 |
|
36 | 37 |
|
37 | | -def aio_secure_channel(target: str, credentials: grpc.ChannelCredentials): |
| 38 | +def aio_secure_channel(target: str, |
| 39 | + credentials: grpc.ChannelCredentials, |
| 40 | + grpc_options: dict[str, Any] | None): |
38 | 41 | return grpc.aio.secure_channel( |
39 | 42 | target, |
40 | 43 | credentials, |
41 | | - options=( |
42 | | - ("grpc.lb_policy_name", "round_robin"), |
43 | | - ("grpc.keepalive_time_ms", 350000), |
44 | | - ("grpc.keepalive_timeout_ms", 5000), |
45 | | - ("grpc.http2.max_pings_without_data", 5), |
46 | | - ("grpc.keepalive_permit_without_calls", 1), |
47 | | - ), |
| 44 | + options=_override_default_grpc_options(grpc_options), |
48 | 45 | ) |
49 | 46 |
|
| 47 | +def _override_default_grpc_options(grpc_options: dict[str, Any] | None) -> Sequence[Tuple[str, Any]]: |
| 48 | + defaults = ( |
| 49 | + ("grpc.lb_policy_name", "round_robin"), |
| 50 | + # we keep a low keepalive time to avoid idle timeouts on cloud load balancers |
| 51 | + ("grpc.keepalive_time_ms", 20000), |
| 52 | + ("grpc.keepalive_timeout_ms", 5000), |
| 53 | + ("grpc.http2.max_pings_without_data", 5), |
| 54 | + ("grpc.keepalive_permit_without_calls", 1), |
| 55 | + ) |
| 56 | + options = dict(defaults, **(grpc_options or {})) |
| 57 | + raise ValueError(options) |
| 58 | + print("grpc_options", grpc_options) |
| 59 | + print(options) |
| 60 | + return tuple(options.items()) |
| 61 | + |
| 62 | + |
50 | 63 |
|
51 | 64 | def configure_grpc_env(): |
52 | 65 | # disable informative logs by default, i.e.: |
|
0 commit comments