pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/MagicStack/asyncpg/pull/1313

ts/global-0bd78641c0a1f3e0.css" /> Add support for connector factory in connect args by enocom · Pull Request #1313 · MagicStack/asyncpg · GitHub
Skip to content

Add support for connector factory in connect args#1313

Open
enocom wants to merge 1 commit intoMagicStack:masterfrom
enocom:master
Open

Add support for connector factory in connect args#1313
enocom wants to merge 1 commit intoMagicStack:masterfrom
enocom:master

Conversation

@enocom
Copy link
Copy Markdown

@enocom enocom commented Mar 24, 2026

This commit adds support for providing a connector factory, a callable that provides full control over how the network connection to the PostgreSQL server is established. When specified, this factory replaces the default connection logic. The callable receives the following arguments:

  • proto_factory - a callable that returns the asyncpg protocol instance
  • host - the target hostname (positional)
  • port - the target port (positional)
  • loop - the event loop (keyword argument)
  • ssl - the SSL context, or None (keyword argument)

The callable must return an awaitable that resolves to a (transport, protocol) tuple, compatible with
asyncio.loop.create_connection.

This is useful for scenarios such as connecting through a proxy, establishing an SSH tunnel, or performing custom socket setup before the PostgreSQL protocol begins.

Usage looks like this:

async def my_connector(proto_factory, host, port, *, loop, ssl):
    tunnel_sock = await open_ssh_tunnel(host, port)
    return await loop.create_connection(
        proto_factory, sock=tunnel_sock, ssl=ssl)

conn = await asyncpg.connect(
    connector_factory=my_connector,
    host='db.example.com',
    user='postgres',
)

Fixes #1054.

This commit adds support for providing a connector factory, a callable
that provides full control over how the network connection to the
PostgreSQL server is established. When specified, this factory replaces
the default connection logic. The callable receives the following
arguments:

- *proto_factory* - a callable that returns the asyncpg protocol
  instance
- *host* - the target hostname (positional)
- *port* - the target port (positional)
- *loop* - the event loop (keyword argument)
- *ssl* - the SSL context, or ``None`` (keyword argument)

The callable must return an awaitable that resolves to a
`(transport, protocol)` tuple, compatible with
`asyncio.loop.create_connection`.

This is useful for scenarios such as connecting through a proxy,
establishing an SSH tunnel, or performing custom socket setup
before the PostgreSQL protocol begins.

Usage looks like this:

```
async def my_connector(proto_factory, host, port, *, loop, ssl):
    tunnel_sock = await open_ssh_tunnel(host, port)
    return await loop.create_connection(
        proto_factory, sock=tunnel_sock, ssl=ssl)

conn = await asyncpg.connect(
    connector_factory=my_connector,
    host='db.example.com',
    user='postgres',
)
```

Fixes MagicStack#1054.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: allow connection with pre-configured socket

1 participant

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy