|
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,20 +35,28 @@ 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, credentials: grpc.ChannelCredentials, grpc_options: dict[str, Any] | None): |
38 | 39 | return grpc.aio.secure_channel( |
39 | 40 | target, |
40 | 41 | 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 | | - ), |
| 42 | + options=_override_default_grpc_options(grpc_options), |
48 | 43 | ) |
49 | 44 |
|
50 | 45 |
|
| 46 | +def _override_default_grpc_options(grpc_options: dict[str, str | int] | None) -> Sequence[Tuple[str, Any]]: |
| 47 | + defaults = ( |
| 48 | + ("grpc.lb_policy_name", "round_robin"), |
| 49 | + # we keep a low keepalive time to avoid idle timeouts on cloud load balancers |
| 50 | + ("grpc.keepalive_time_ms", 20000), |
| 51 | + ("grpc.keepalive_timeout_ms", 5000), |
| 52 | + ("grpc.http2.max_pings_without_data", 5), |
| 53 | + ("grpc.keepalive_permit_without_calls", 1), |
| 54 | + ) |
| 55 | + options = dict(defaults) |
| 56 | + options.update(grpc_options or {}) |
| 57 | + return tuple(options.items()) |
| 58 | + |
| 59 | + |
51 | 60 | def configure_grpc_env(): |
52 | 61 | # disable informative logs by default, i.e.: |
53 | 62 | # WARNING: All log messages before absl::InitializeLog() is called are written to STDERR |
|
0 commit comments