Skip to content

Drop multi-host connect workarounds once asyncpg supports connect_timeout and load_balance_hosts #67

Open

Description

@lesnik512

build_connection_factory works around two gaps in asyncpg's multi-host connect. Both can go once asyncpg covers them.

Host-by-host fallback (MagicStack/asyncpg#1155)

With a multi-host DSN, a host that hangs instead of refusing uses up the whole timeout in asyncpg.connect(), and the other hosts are never tried. That is why the factory catches TimeoutError from the bulk attempt and then connects host by host (db_retry/connections.py).

MagicStack/asyncpg#1393 adds connect_timeout, a deadline for each host address, matching libpq. With it, the bulk attempt can pass connect_timeout=timeout and timeout=timeout * len(hosts). That keeps today's budget, where every host gets the full timeout in the fallback, and the fallback loop can be deleted.

Per-connection shuffle

asyncpg always tries hosts in DSN order, so the factory shuffles them for each connection to spread connections across replicas. libpq does this with load_balance_hosts=random. asyncpg has no equivalent yet; MagicStack/asyncpg#1394 adds load_balance_hosts.

Blocked on: both asyncpg changes being merged and released (0.33.0 at the earliest).

When unblocked, pick one:

  • raise the asyncpg minimum to the release that has both and reduce the factory to passing connect_timeout and load_balance_hosts=random, or
  • detect support in the installed asyncpg and skip the workarounds only on versions that have it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions