1313 DEFAULT_ENDPOINT ,
1414 DEFAULT_REGION ,
1515 DEFAULT_RUNTIME ,
16+ DEFAULT_READ_TIMEOUT_SECONDS ,
1617 DEFAULT_SESSION_WAIT_TIMEOUT_SECONDS ,
1718 MAX_MESSAGE_SIZE ,
1819)
@@ -35,7 +36,8 @@ def connect(
3536 api_key : str = None ,
3637 runtime : Runtime = None ,
3738 region : Region = None ,
38- wait_timeout_seconds : int = DEFAULT_SESSION_WAIT_TIMEOUT_SECONDS ,
39+ wait_timeout : float = DEFAULT_SESSION_WAIT_TIMEOUT_SECONDS ,
40+ read_timeout : float = DEFAULT_READ_TIMEOUT_SECONDS ,
3941) -> Connection :
4042 if not token and not api_key :
4143 raise ValueError ("At least one of `token` or `api_key` is required" )
@@ -79,7 +81,7 @@ def connect(
7981 session_id_url = resp .url
8082
8183 @tenacity .retry (
82- stop = tenacity .stop_after_delay (wait_timeout_seconds ),
84+ stop = tenacity .stop_after_delay (wait_timeout ),
8385 wait = tenacity .wait_exponential (multiplier = 1 , min = 1 , max = 5 ),
8486 retry = tenacity .retry_if_not_exception_type (
8587 (requests .HTTPError , OperationalError )
@@ -105,7 +107,11 @@ def get_session_uri() -> str:
105107 except Exception as e :
106108 raise InterfaceError ("Could not acquire SQL session!" , e )
107109
108- return connect_direct (http_to_ws (session_uri ), headers )
110+ return connect_direct (
111+ uri = http_to_ws (session_uri ),
112+ headers = headers ,
113+ read_timeout = read_timeout ,
114+ )
109115
110116
111117def http_to_ws (uri : str ) -> str :
@@ -117,13 +123,17 @@ def http_to_ws(uri: str) -> str:
117123 return str (urllib .parse .urlunparse (parsed ))
118124
119125
120- def connect_direct (uri : str , headers : dict [str , str ] = None ) -> Connection :
126+ def connect_direct (
127+ uri : str ,
128+ headers : dict [str , str ] = None ,
129+ read_timeout : float = DEFAULT_READ_TIMEOUT_SECONDS ,
130+ ) -> Connection :
121131 logging .info ("Connecting to SQL session at %s ..." , uri )
122132 try :
123133 ws = websockets .sync .client .connect (
124134 uri = uri , additional_headers = headers , max_size = MAX_MESSAGE_SIZE
125135 )
126- session = Connection (ws )
136+ session = Connection (ws , read_timeout )
127137 return session
128138 except Exception as e :
129139 raise InterfaceError ("Failed to connect to SQL session!" ) from e
0 commit comments