Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ docs/_build
/.eggs
/.vscode
/.mypy_cache
.venv
21 changes: 13 additions & 8 deletions asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import enum
import functools
import getpass
import inspect
import logging
import random
import os
import pathlib
import platform
Expand All @@ -23,12 +26,10 @@
import typing
import urllib.parse
import warnings
import inspect

from . import compat
from . import exceptions
from . import protocol
from . import compat, exceptions, protocol

logger = logging.getLogger(__name__)

class SSLMode(enum.IntEnum):
disable = 0
Expand Down Expand Up @@ -882,28 +883,32 @@ async def __connect_addr(
return con


async def _connect(*, loop, timeout, connection_class, record_class, **kwargs):
async def _connect(*, loop, timeout, connection_class, record_class, connect_timeout=60, **kwargs):
if loop is None:
loop = asyncio.get_event_loop()

addrs, params, config = _parse_connect_arguments(timeout=timeout, **kwargs)
addrs, params, config = _parse_connect_arguments(timeout=connect_timeout, **kwargs)

last_error = None
random.shuffle(addrs)
last_error = ConnectionError(f"Can't connect to all hosts {addrs}")
addr = None
for addr in addrs:
if timeout <= 0:
raise ConnectionError("Timeout")
before = time.monotonic()
try:
return await _connect_addr(
addr=addr,
loop=loop,
timeout=timeout,
timeout=connect_timeout,
params=params,
config=config,
connection_class=connection_class,
record_class=record_class,
)
except (OSError, asyncio.TimeoutError, ConnectionError) as ex:
last_error = ex
logger.warning("Can't connect to %s: %s", addr, ex, exc_info=True)
finally:
timeout -= time.monotonic() - before

Expand Down
2 changes: 2 additions & 0 deletions asyncpg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@ async def connect(dsn=None, *,
database=None,
loop=None,
timeout=60,
connect_timeout=60,
statement_cache_size=100,
max_cached_statement_lifetime=300,
max_cacheable_statement_size=1024 * 15,
Expand Down Expand Up @@ -2104,6 +2105,7 @@ async def connect(dsn=None, *,
ssl=ssl,
direct_tls=direct_tls,
database=database,
connect_timeout=connect_timeout,
server_settings=server_settings,
command_timeout=command_timeout,
statement_cache_size=statement_cache_size,
Expand Down