Skip to content
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

Add support for asyncpg driver. #218

Closed
jackwotherspoon opened this issue Dec 10, 2021 · 5 comments · Fixed by #390
Closed

Add support for asyncpg driver. #218

jackwotherspoon opened this issue Dec 10, 2021 · 5 comments · Fixed by #390
Assignees
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@jackwotherspoon
Copy link
Collaborator

Support PostgreSQL asyncpg async driver with connector.

@jackwotherspoon jackwotherspoon added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. labels Dec 10, 2021
@jackwotherspoon jackwotherspoon self-assigned this Dec 10, 2021
@sholmesy
Copy link

How'd you go with this?

We're looking at moving to cloud-sql-python connector, but are heavily dependant on asyncpg at the moment.

From what I can gather from a quick gaze, is that we need to skip the custom _create_ssl_connection that gets called when a SSL Context is passed in with the connection args tlsproxy=True.

# asyncpg.connection.connect:


async def connect(
    dsn=None, 
    *,
    # ... other kwargs ... 
    tls_proxy=None # NOTE: new_field
):

# ... gets passed into asyncpg.connect_utils._connect ...

# which calls _parse_connect_arguments:

def _parse_connect_arguments(
    *,
    # ... other kwargs ...
    tls_proxy # NOTE: new_field
):

# ... gets passed into asyncpg.connect_utils._connect_addr ...

# which could look something like this:

async def __connect_addr(
    params,
    timeout,
    retry,
    addr,
    loop,
    config,
    connection_class,
    record_class,
    params_input,
):
    connected = _create_future(loop)

    proto_factory = lambda: protocol.Protocol(
        addr, connected, params, record_class, loop)

    if isinstance(addr, str):
        # UNIX socket
        connector = loop.create_unix_connection(proto_factory, addr)
    # NOTE: new_code
    elif params.ssl and params.tls_proxy:
        # passing ctx, to asyncio.events.create_connection,
        # bypassing asyncpg's _create_ssl_connection
        connector = loop.create_connection(
            proto_factory, *addr, ssl=params.ssl
        )

    elif params.ssl:
        connector = _create_ssl_connection(
            proto_factory, *addr, loop=loop, ssl_context=params.ssl,
            ssl_is_advisory=params.sslmode == SSLMode.prefer)
    else:
        connector = loop.create_connection(proto_factory, *addr)

@jackwotherspoon
Copy link
Collaborator Author

@sholmesy Thanks for the comment! I am currently working on a PR to update the asyncpg repository to allow for connections from the Cloud SQL Python Connector. The change looks very similar to what you have mentioned here, just working through some final tweaks before I put up the PR. Once the change is implemented within asyncpg, the update to this repository is pretty straightforward.

Hopefully if all goes smoothly, asyncpg will be supported sometime in the following couple of weeks. I will link the issue here and follow up on this thread with any new updates or developments.

Thanks for showing your interest in using the Python Connector and have an awesome day!

Linked Issue: MagicStack/asyncpg/#906

@sholmesy
Copy link

Sounds good, let me know if you need a hand with anything.

@jackwotherspoon
Copy link
Collaborator Author

@sholmesy The PR has been merged upstream into asyncpg. A new release of asyncpg should be coming out in the next week or two that has the new changes reflected . I will finish up the required changes here in the python connector in the meantime so that support for asyncpg is ready upon the new release.

@sholmesy
Copy link

Thanks, good news.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p1 Important issue which blocks shipping the next release. Will be fixed prior to next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants