Skip to content

Commit

Permalink
fix: add debug logging when a connection attempt is discarded
Browse files Browse the repository at this point in the history
  • Loading branch information
dvarrazzo committed Dec 13, 2023
1 parent 8dd0204 commit c50a41d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
12 changes: 11 additions & 1 deletion psycopg/psycopg/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,12 +732,22 @@ def connect(
params = cls._get_connection_params(conninfo, **kwargs)
timeout = int(params["connect_timeout"])
rv = None
for attempt in conninfo_attempts(params):
attempts = conninfo_attempts(params)
for attempt in attempts:
try:
conninfo = make_conninfo(**attempt)
rv = cls._wait_conn(cls._connect_gen(conninfo), timeout=timeout)
break
except e._NO_TRACEBACK as ex:
if len(attempts) > 1:
logger.debug(
"connection attempt failed on host: %r, port: %r,"
" hostaddr: %r: %s",
attempt.get("host"),
attempt.get("port"),
attempt.get("hostaddr"),
str(ex),
)
last_ex = ex

if not rv:
Expand Down
12 changes: 11 additions & 1 deletion psycopg/psycopg/connection_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,22 @@ async def connect(
params = await cls._get_connection_params(conninfo, **kwargs)
timeout = int(params["connect_timeout"])
rv = None
for attempt in await conninfo_attempts_async(params):
attempts = await conninfo_attempts_async(params)
for attempt in attempts:
try:
conninfo = make_conninfo(**attempt)
rv = await cls._wait_conn(cls._connect_gen(conninfo), timeout=timeout)
break
except e._NO_TRACEBACK as ex:
if len(attempts) > 1:
logger.debug(
"connection attempt failed on host: %r, port: %r,"
" hostaddr: %r: %s",
attempt.get("host"),
attempt.get("port"),
attempt.get("hostaddr"),
str(ex),
)
last_ex = ex

if not rv:
Expand Down
4 changes: 4 additions & 0 deletions psycopg/psycopg/conninfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re
import socket
import asyncio
import logging
from typing import Any
from random import shuffle
from pathlib import Path
Expand All @@ -26,6 +27,8 @@

ConnDict: TypeAlias = "dict[str, Any]"

logger = logging.getLogger("psycopg")


def make_conninfo(conninfo: str = "", **kwargs: Any) -> str:
"""
Expand Down Expand Up @@ -321,6 +324,7 @@ async def conninfo_attempts_async(params: ConnDict) -> list[ConnDict]:
try:
attempts.extend(await _resolve_hostnames(attempt))
except OSError as ex:
logger.debug("failed to resolve host %r: %s", attempt.get("host"), str(ex))
last_exc = ex

if not attempts:
Expand Down

0 comments on commit c50a41d

Please sign in to comment.