@@ -142,6 +142,20 @@ async def close(self) -> None:
142142 except Exception : # noqa: BLE001, S110
143143 pass
144144
145+ async def _open_direct_connection (
146+ self ,
147+ target_ip : str ,
148+ port : int ,
149+ ssl_context : Any ,
150+ connect_timeout : float ,
151+ ) -> tuple [asyncio .StreamReader , asyncio .StreamWriter ]:
152+ return await asyncio .wait_for (
153+ asyncio .open_connection (
154+ target_ip , port , ssl = ssl_context , server_hostname = target_ip
155+ ),
156+ timeout = connect_timeout ,
157+ )
158+
145159 async def _handle_tunnel (
146160 self ,
147161 client_reader : asyncio .StreamReader ,
@@ -225,9 +239,9 @@ async def connect_grpc() -> tuple[grpc.aio.Channel, Any]:
225239 async def connect_direct () -> tuple [asyncio .StreamReader , asyncio .StreamWriter ]:
226240 logger .debug ("Fallback triggered, fetching connection info..." )
227241 conn_info = await get_conn_info ()
228- # Find a fallback IP address, prioritizing PUBLIC for direct fallback connectivity
242+ # Find a fallback IP address, prioritizing PRIVATE, PSC, PUBLIC
229243 targets : list [str ] = []
230- for t in [IPTypes .PUBLIC , IPTypes .PSC , IPTypes .PRIVATE ]:
244+ for t in [IPTypes .PRIVATE , IPTypes .PSC , IPTypes .PUBLIC ]:
231245 try :
232246 targets .extend (conn_info .get_preferred_ips (t ))
233247 except CloudSQLIPTypeError as e :
@@ -240,11 +254,11 @@ async def connect_direct() -> tuple[asyncio.StreamReader, asyncio.StreamWriter]:
240254 for target_ip in targets :
241255 logger .debug (f"Connecting directly to { target_ip } :{ SERVER_PROXY_PORT } " )
242256 try :
243- r , w = await asyncio . wait_for (
244- asyncio . open_connection (
245- target_ip , SERVER_PROXY_PORT , ssl = ssl_context , server_hostname = target_ip
246- ) ,
247- timeout = connect_timeout ,
257+ r , w = await self . _open_direct_connection (
258+ target_ip ,
259+ SERVER_PROXY_PORT ,
260+ ssl_context ,
261+ connect_timeout ,
248262 )
249263 self ._active_writers .add (w )
250264 return r , w
0 commit comments