Add connect_fn kwarg to Pool to better support GCP's CloudSQL #1170
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 relates to an issue I created a few months back and then updated last week.
GCP's cloud-sql-python-connector offers asyncpg as one of its drivers - see logic here - however that can only be used as a single connection, not in conjunction with asyncpg's
Pool
class because it wraps theconnection.connect(..)
call in various bits of logic to do with authentication and SSL (which I admit I haven't tried to fully understand).This PR simply turns the call to
connection.connect
into a kwarg parameter on the Pool class so it can be overridden.Usage would be something like this:
Note the gcp repo's docs suggest using SQLAlchemy for a pool, but that's not much use if your codebase already uses asyncpg's Pool API (at least as far as I understand from SQLAlchemy).
--
Couple of additional thoughts:
It is perhaps a bit confusing that there will now be three functions that all see somewhat similar in name -
init
,setup
and aconnect_fn
, but I don't know enough to say if they can be refactored into fewer kwargs somehow.As noted in my issue, linked above, it is possible to work around this by subclassing the Pool class, but that's not especially nice and this seemed like a small ask so thought it was worth suggesting.