11from __future__ import annotations
22
33import asyncio
4+ from datetime import timedelta
45from typing import Optional
56
67import grpc
1011
1112from momento .auth import CredentialProvider
1213from momento .config import Configuration , TopicConfiguration
14+ from momento .config .transport .transport_strategy import StaticGrpcConfiguration
1315from momento .internal ._utilities import momento_version
1416from momento .internal ._utilities ._channel_credentials import (
1517 channel_credentials_from_root_certs_or_default ,
1618)
19+ from momento .internal ._utilities ._grpc_channel_options import (
20+ grpc_control_channel_options_from_grpc_config ,
21+ grpc_data_channel_options_from_grpc_config ,
22+ )
1723from momento .retry import RetryStrategy
1824
1925from ... import logs
@@ -35,6 +41,9 @@ def __init__(self, configuration: Configuration, credential_provider: Credential
3541 target = credential_provider .control_endpoint ,
3642 credentials = channel_credentials_from_root_certs_or_default (configuration ),
3743 interceptors = _interceptors (credential_provider .auth_token , configuration .get_retry_strategy ()),
44+ options = grpc_control_channel_options_from_grpc_config (
45+ grpc_config = configuration .get_transport_strategy ().get_grpc_configuration (),
46+ ),
3847 )
3948
4049 async def close (self ) -> None :
@@ -64,11 +73,14 @@ def __init__(self, configuration: Configuration, credential_provider: Credential
6473 # https://grpc.github.io/grpc/python/grpc.html
6574 # https://grpc.github.io/grpc/python/glossary.html#term-channel_arguments
6675 # https://github.com/grpc/grpc/blob/v1.46.x/include/grpc/impl/codegen/grpc_types.h#L140
67- options = [
68- # ('grpc.max_concurrent_streams', 1000),
69- # ('grpc.use_local_subchannel_pool', 1),
70- # (experimental.ChannelOptions.SingleThreadedUnaryStream, 1)
71- ],
76+ # options=[
77+ # ('grpc.max_concurrent_streams', 1000),
78+ # ('grpc.use_local_subchannel_pool', 1),
79+ # (experimental.ChannelOptions.SingleThreadedUnaryStream, 1)
80+ # ],
81+ options = grpc_data_channel_options_from_grpc_config (
82+ configuration .get_transport_strategy ().get_grpc_configuration ()
83+ ),
7284 )
7385
7486 async def eagerly_connect (self , timeout_seconds : float ) -> None :
@@ -79,6 +91,9 @@ async def eagerly_connect(self, timeout_seconds: float) -> None:
7991 await asyncio .wait_for (self .wait_for_ready (), timeout_seconds )
8092 except Exception as error :
8193 self ._logger .debug (f"Failed to connect to the server within the given timeout. { error } " )
94+ raise RuntimeError (
95+ f"Failed to connect to Momento's server within given eager connection timeout { error } "
96+ ) from error
8297
8398 async def wait_for_ready (self ) -> None :
8499 latest_state = self ._secure_channel .get_state (True ) # try_to_connect
@@ -117,10 +132,14 @@ class _PubsubGrpcManager:
117132 version = momento_version
118133
119134 def __init__ (self , configuration : TopicConfiguration , credential_provider : CredentialProvider ):
135+ # NOTE: This is hard-coded for now but we may want to expose it via TopicConfiguration in the future, as we do with some of the other clients.
136+ grpc_config = StaticGrpcConfiguration (deadline = timedelta (milliseconds = 1100 ))
137+
120138 self ._secure_channel = grpc .aio .secure_channel (
121139 target = credential_provider .cache_endpoint ,
122140 credentials = grpc .ssl_channel_credentials (),
123141 interceptors = _interceptors (credential_provider .auth_token , None ),
142+ options = grpc_data_channel_options_from_grpc_config (grpc_config ),
124143 )
125144
126145 async def close (self ) -> None :
@@ -136,10 +155,14 @@ class _PubsubGrpcStreamManager:
136155 version = momento_version
137156
138157 def __init__ (self , configuration : TopicConfiguration , credential_provider : CredentialProvider ):
158+ # NOTE: This is hard-coded for now but we may want to expose it via TopicConfiguration in the future, as we do with some of the other clients.
159+ grpc_config = StaticGrpcConfiguration (deadline = timedelta (milliseconds = 1100 ))
160+
139161 self ._secure_channel = grpc .aio .secure_channel (
140162 target = credential_provider .cache_endpoint ,
141163 credentials = grpc .ssl_channel_credentials (),
142164 interceptors = _stream_interceptors (credential_provider .auth_token ),
165+ options = grpc_data_channel_options_from_grpc_config (grpc_config ),
143166 )
144167
145168 async def close (self ) -> None :
0 commit comments