-
Notifications
You must be signed in to change notification settings - Fork 84
feat: reset connection when the DNS record changes #1241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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}" | ||
|
There was a problem hiding this comment.
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)] |
There was a problem hiding this comment.
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
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 |
Code coverage drop from 94% to 93% is negligible |
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. |
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.
If
db.example.com
fails over frommy-project:us-central1:pg
tomy-project:us-central1:pg-replica
then the connections will be closed and re-opened to new instance.Closes #1169