Adds max_consecutive_exceptions to pool #253
Open
+43
−3
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This change might not be for everyone, so I respect the right of the core maintainers to downvote/close.
The backstory here is that I am using
asyncpg
to connect to an HA postgres cluster. In the event of a failover, thewriter
(where theasyncpg
connection pool is connected to) crashes/fails, and is rebooted. Thereader
is then promoted towriter
. The DB DNS is automatically updated, but this takes time. In addition, the connections in the pool continue operating once the crashed DB recovers, except now that DB is the reader (i.e.SHOW transaction_read_only; -> on
). As a result, INSERT/UPDATE/DELETE operations result in (among other various connection errors):Other errors, like timeouts, are also possible offenders. Unfortunately, the only way I've found around this to refresh the connections is to set a low
max_queries
config param. This is generally ok, but degrades performance due to increased cycles of closing and opening new connections.With this PR, a configurable
max_consecutive_exceptions
pool param is introduced. This param is checked against every time a pool executes a connection method (fetch
,execute
, etc) and itresults in an appropriate exception. It's important to note that this PR only manages the consecutive exception state via context, not directcon = await pool.acquire()
(in which case it's up to the user to handle).Supersedes #249