@@ -503,59 +503,52 @@ async def connect_async(
503503 monitored_cache = await self ._get_cache (
504504 instance_connection_string , enable_iam_auth , ip_type , driver
505505 )
506- conn_info = await monitored_cache .connect_info ()
507- ip_address = conn_info .get_preferred_ip (ip_type )
508506
509507 try :
510508 conn_info = await monitored_cache .connect_info ()
511509 # validate driver matches intended database engine
512510 DriverMapping .validate_engine (driver , conn_info .database_version )
513511 ip_address = conn_info .get_preferred_ip (ip_type )
514- except Exception :
515- # with an error from Cloud SQL Admin API call or IP type, invalidate
516- # the cache and re-raise the error
517- await self ._remove_cached (str (conn_name ), enable_iam_auth )
518- raise
519512
520- # If the connector is configured with a custom DNS name, attempt to use
521- # that DNS name to connect to the instance. Fall back to the metadata IP
522- # address if the DNS name does not resolve to an IP address.
523- if conn_info .conn_name .domain_name and isinstance (self ._resolver , DnsResolver ):
524- try :
525- ips = await self ._resolver .resolve_a_record (conn_info .conn_name .domain_name )
526- if ips :
527- ip_address = ips [0 ]
513+ # If the connector is configured with a custom DNS name, attempt to use
514+ # that DNS name to connect to the instance. Fall back to the metadata IP
515+ # address if the DNS name does not resolve to an IP address.
516+ if conn_info .conn_name .domain_name and isinstance (self ._resolver , DnsResolver ):
517+ try :
518+ ips = await self ._resolver .resolve_a_record (conn_info .conn_name .domain_name )
519+ if ips :
520+ ip_address = ips [0 ]
521+ logger .debug (
522+ f"['{ instance_connection_string } ']: Custom DNS name "
523+ f"'{ conn_info .conn_name .domain_name } ' resolved to '{ ip_address } ', "
524+ "using it to connect"
525+ )
526+ else :
527+ logger .debug (
528+ f"['{ instance_connection_string } ']: Custom DNS name "
529+ f"'{ conn_info .conn_name .domain_name } ' resolved but returned no "
530+ f"entries, using '{ ip_address } ' from instance metadata"
531+ )
532+ except Exception as e :
528533 logger .debug (
529534 f"['{ instance_connection_string } ']: Custom DNS name "
530- f"'{ conn_info .conn_name .domain_name } ' resolved to ' { ip_address } ', "
531- " using it to connect "
535+ f"'{ conn_info .conn_name .domain_name } ' did not resolve to an IP "
536+ f"address: { e } , using ' { ip_address } ' from instance metadata "
532537 )
533- else :
534- logger .debug (
535- f"['{ instance_connection_string } ']: Custom DNS name "
536- f"'{ conn_info .conn_name .domain_name } ' resolved but returned no "
537- f"entries, using '{ ip_address } ' from instance metadata"
538- )
539- except Exception as e :
540- logger .debug (
541- f"['{ instance_connection_string } ']: Custom DNS name "
542- f"'{ conn_info .conn_name .domain_name } ' did not resolve to an IP "
543- f"address: { e } , using '{ ip_address } ' from instance metadata"
544- )
545538
546- logger .debug (f"['{ conn_info .conn_name } ']: Connecting to { ip_address } :3307" )
547- # format `user` param for automatic IAM database authn
548- if enable_iam_auth :
549- formatted_user = format_database_user (
550- conn_info .database_version , kwargs ["user" ]
551- )
552- if formatted_user != kwargs ["user" ]:
553- logger .debug (
554- f"['{ instance_connection_string } ']: "
555- "Truncated IAM database username from "
556- f"{ kwargs ['user' ]} to { formatted_user } "
539+ logger .debug (f"['{ conn_info .conn_name } ']: Connecting to { ip_address } :3307" )
540+ # format `user` param for automatic IAM database authn
541+ if enable_iam_auth :
542+ formatted_user = format_database_user (
543+ conn_info .database_version , kwargs ["user" ]
557544 )
558- kwargs ["user" ] = formatted_user
545+ if formatted_user != kwargs ["user" ]:
546+ logger .debug (
547+ f"['{ instance_connection_string } ']: "
548+ "Truncated IAM database username from "
549+ f"{ kwargs ['user' ]} to { formatted_user } "
550+ )
551+ kwargs ["user" ] = formatted_user
559552
560553 ctx = await conn_info .create_ssl_context (enable_iam_auth )
561554 # async drivers are unblocking and can be awaited directly
@@ -576,11 +569,9 @@ async def connect_async(
576569 return await self ._loop .run_in_executor (None , connect_partial )
577570
578571 except Exception :
579- # with any exception, we attempt a force refresh, then throw the error
580- monitored_cache = await self ._get_cache (
581- instance_connection_string , enable_iam_auth , ip_type , driver
582- )
583- await monitored_cache .force_refresh ()
572+ # with an error from Cloud SQL Admin API call or connection, invalidate
573+ # the cache and re-raise the error
574+ await self ._remove_cached (str (conn_name ), enable_iam_auth )
584575 raise
585576
586577 async def start_unix_socket_proxy_async (
0 commit comments