Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

jackwotherspoon
Copy link
Collaborator

@jackwotherspoon jackwotherspoon commented Mar 3, 2025

If the connector is configured with a domain name, when that domain name record changes to
resolve to a new instance, the connector should detect that change, close all connections to the old
instance, and create connections to the new instance.

import sqlalchemy
from google.cloud.sql.connector import Connector, DnsResolver

# using default failover period of 30 seconds
connector = Connector(resolver=DnsResolver)

# using custom failover period
# connector = Connector(resolver=DnsResolver, failover_period=60)

# establish connection using domain name
engine = sqlalchemy.create_engine(
    "postgresql+pg8000://",
    creator=lambda: connector.connect(
        "db.example.com",
        "pg8000",
        user="my-user",
        password="my-password",
        db="my-db",
        ip_type="public",  # can also be "private" or "psc"
    ),
)
with engine.connect() as conn:
      curr_time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone()
      conn.commit()

If db.example.com fails over from my-project:us-central1:pg to my-project:us-central1:pg-replica then the connections will be closed and re-opened to new instance.

2025-03-20 14:07:05,504 [DEBUG]: ['db.example.com -> my-project:us-central1:pg']: Connecting to 34.172.89.123:3307
2025-03-20 14:07:16,137 [DEBUG]: ['db.example.com -> my-project:us-central1:pg-replica']: Connecting to 34.60.127.180:3307
2025-03-20 14:07:19,387 [DEBUG]: ['db.example.com -> my-project:us-central1:pg']: Cloud SQL instance changed from my-project:us-central1:pg to my-project:us-central1:pg-replica, closing all connections!
2025-03-20 14:07:19,387 [DEBUG]: ['db.example.com -> my-project:us-central1:pg']: Cancelled domain name polling task.
2025-03-20 14:07:19,387 [DEBUG]: ['db.example.com -> my-project:us-central1:pg']: Canceling connection info refresh operation tasks
2025-03-20 14:07:19,387 [DEBUG]: ['db.example.com -> my-project:us-central1:pg']: Scheduled refresh operation cancelled
2025-03-20 14:07:21,472 [DEBUG]: ['db.example.com -> my-project:us-central1:pg-replica']: Connecting to 34.60.127.180:3307

Closes #1169

@jackwotherspoon jackwotherspoon self-assigned this Mar 3, 2025
Comment on lines +41 to +44
def get_connection_string(self) -> str:
"""Get the instance connection string for the Cloud SQL instance."""
return f"{self.project}:{self.region}:{self.instance_name}"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows for a reliable way to get the instance connection name for a Cloud SQL instance whether the connector is connecting via domain name or instance connection name.


conn_name = await self._resolver.resolve(instance_connection_string)
if (str(conn_name), enable_iam_auth) in self._cache:
monitored_cache = self._cache[(str(conn_name), enable_iam_auth)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update cache key to be str(conn_name) which will result in cache key either being :

domain name -> instance connection name or just instance connection name

@jackwotherspoon jackwotherspoon marked this pull request as ready for review March 20, 2025 15:24
@jackwotherspoon jackwotherspoon requested a review from a team as a code owner March 20, 2025 15:24
@jackwotherspoon
Copy link
Collaborator Author

Example of failover working:

2025-03-20 14:07:05,504 [DEBUG]: ['prod-db.mycompany.example.com -> my-project:us-central1:pg']: Connecting to 34.172.89.123:3307
2025-03-20 14:07:16,137 [DEBUG]: ['prod-db.mycompany.example.com -> my-project:us-central1:pg-replica']: Connecting to 34.60.127.180:3307
2025-03-20 14:07:19,387 [DEBUG]: ['prod-db.mycompany.example.com -> my-project:us-central1:pg']: Cloud SQL instance changed from my-project:us-central1:pg to my-project:us-central1:pg-replica, closing all connections!
2025-03-20 14:07:19,387 [DEBUG]: ['prod-db.mycompany.example.com -> my-project:us-central1:pg']: Cancelled domain name polling task.
2025-03-20 14:07:19,387 [DEBUG]: ['prod-db.mycompany.example.com -> my-project:us-central1:pg']: Canceling connection info refresh operation tasks
2025-03-20 14:07:19,387 [DEBUG]: ['prod-db.mycompany.example.com -> my-project:us-central1:pg']: Scheduled refresh operation cancelled
2025-03-20 14:07:21,472 [DEBUG]: ['prod-db.mycompany.example.com -> my-project:us-central1:pg-replica']: Connecting to 34.60.127.180:3307

@jackwotherspoon
Copy link
Collaborator Author

Code coverage drop from 94% to 93% is negligible

@jackwotherspoon jackwotherspoon requested a review from hessjcg March 20, 2025 20:10
@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

@jackwotherspoon jackwotherspoon merged commit 1405f56 into main Mar 21, 2025
16 of 17 checks passed
@jackwotherspoon jackwotherspoon deleted the dns-reset-connection branch March 21, 2025 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatically configure and fail-over connections using DNS
2 participants