MySqlConnector icon indicating copy to clipboard operation
MySqlConnector copied to clipboard

Add `DNS Recheck Seconds` connection string option

Open bgrainger opened this issue 2 years ago • 0 comments

In a HA scenario, failover can be accomplished by updating DNS to point to the new primary server. (E.g., "DNS is updated to redirect connections to the new primary when the client reconnects" in Azure MySQL.)

MySqlConnector does not detect this, so connections can remain open (in the pool) to the old IP address, which may be a server that is now read-only. The client has to reconnect to access the new primary.

Proposal: Solve this by adding a new DNS Recheck Seconds connection string option. If this is set to a positive number, n, then every n seconds, MySqlConnector checks all hostnames for the Server option, and resolves each to its IP address(es). If any has changed, the pool is cleared, then Minimum Pool Size connections are reestablished, etc. (A more advanced implementation could clear only connections to IP addresses that are no longer returned from DNS, but that doesn't have to be in v1.)

Having a background task wake up and perform this check very frequently (e.g., every 5-10 seconds may be typical) may be overkill for infrequently-accessed servers; in that case it may be preferable to check only when an existing connection is retrieved (e.g., like Connection Lifetime). However, that kind of access pattern may not be using an HA setup in the first place. The background task may be less overhead than determining if a DNS check is necessary when opening each connection, in scenarios that open dozens of connections per second.

bgrainger avatar Aug 12 '22 13:08 bgrainger