From: Daniele Varrazzo Date: Wed, 13 Dec 2023 03:34:40 +0000 (+0100) Subject: fix: add debug logging when a connection attempt is discarded X-Git-Tag: 3.1.15~1^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c50a41d983c68068b02ae19ef26757d99d4688fd;p=thirdparty%2Fpsycopg.git fix: add debug logging when a connection attempt is discarded --- diff --git a/psycopg/psycopg/connection.py b/psycopg/psycopg/connection.py index e90e30adb..e04a99403 100644 --- a/psycopg/psycopg/connection.py +++ b/psycopg/psycopg/connection.py @@ -732,12 +732,22 @@ class Connection(BaseConnection[Row]): 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: diff --git a/psycopg/psycopg/connection_async.py b/psycopg/psycopg/connection_async.py index 5e628d614..bea077fe8 100644 --- a/psycopg/psycopg/connection_async.py +++ b/psycopg/psycopg/connection_async.py @@ -120,12 +120,22 @@ class AsyncConnection(BaseConnection[Row]): 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: diff --git a/psycopg/psycopg/conninfo.py b/psycopg/psycopg/conninfo.py index 9351cc951..c38d7af7a 100644 --- a/psycopg/psycopg/conninfo.py +++ b/psycopg/psycopg/conninfo.py @@ -10,6 +10,7 @@ import os import re import socket import asyncio +import logging from typing import Any from random import shuffle from pathlib import Path @@ -26,6 +27,8 @@ from ._encodings import pgconn_encoding ConnDict: TypeAlias = "dict[str, Any]" +logger = logging.getLogger("psycopg") + def make_conninfo(conninfo: str = "", **kwargs: Any) -> str: """ @@ -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: