def connect_with_retry()

in src/hologres_mcp_server/utils.py [0:0]


def connect_with_retry(retries=3):
    attempt = 0
    err_msg = ""
    while attempt <= retries:
        try:
            config = get_db_config()
            conn = psycopg.connect(**config)
            conn.autocommit = True
            with conn.cursor() as cursor:
                cursor.execute("SELECT 1;")
                cursor.fetchone()
            return conn
        except psycopg.Error as e:
            err_msg = f"Connection failed: {e}"
            attempt += 1
            if attempt <= retries:
                print(f"Retrying connection (attempt {attempt + 1} of {retries + 1})...")
                time.sleep(5)  # 等待 2 秒后再次尝试连接
    raise psycopg.Error(f"Failed to connect to Hologres database after retrying: {err_msg}")