
    pj                        d Z ddlmZ ddlZddlZddlmZ ddlmZ	 ddl
mZmZ ddlmZmZmZ dd	lmZ  ej        d
          ZddZddZdS )z8
Separate connection attempts from a connection string.
    )annotationsN)shuffle   )errors)ConnDictConnMapping)	get_paramis_ip_addressget_param_def)split_attemptspsycopgparamsr   returnlist[ConnDict]c                   d}g }t          |           D ]m}	 |                    t          |                     &# t          $ r;}t                              d|                    d          |           |}Y d}~fd}~ww xY w|s%|sJ t          j        t          |                    t          | d          dk    rt          |           |S )a  Split a set of connection params on the single attempts to perform.

    A connection param can perform more than one attempt more than one ``host``
    is provided.

    Also perform async resolution of the hostname into hostaddr. Because a host
    can resolve to more than one address, this can lead to yield more attempts
    too. Raise `OperationalError` if no host could be resolved.

    Because the libpq async function doesn't honour the timeout, we need to
    reimplement the repeated attempts.
    Nzfailed to resolve host %r: %shostload_balance_hostsrandom)r   extend_resolve_hostnamesOSErrorloggerdebuggeteOperationalErrorstrr	   r   )r   last_excattemptsattemptexs        j/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/psycopg/_conninfo_attempts.pyconninfo_attemptsr#      s     HH!&))  	OO.w778888 	 	 	LL8'++f:M:MrRRRHHHHHH	  0x X///-..(::Os   ":
A?1A::A?r   c                    t           d          }|r#|                    d          s|dd         dk    r gS t           d          }|r gS t          |          r	i  d|igS t           d          }|st          d          }|r|j        pd}t          j        ||t
          j        t
          j        	          } fd
|D             S )aD  
    Perform async DNS lookup of the hosts and return a list of connection attempts.

    If a ``host`` param is present but not ``hostname``, resolve the host
    addresses asynchronously.

    :param params: The input parameters, for instance as returned by
        `~psycopg.conninfo.conninfo_to_dict()`. The function expects at most
        a single entry for host, hostaddr because it is designed to further
        process the input of split_attempts().

    :return: A list of attempts to make (to include the case of a hostname
        resolving to more than one IP).
    r   /r      :hostaddrport5432)prototypec                8    g | ]}i d |d         d         iS )r(      r    ).0itemr   s     r"   
<listcomp>z&_resolve_hostnames.<locals>.<listcomp>`   s1    ???4.v.z471:..???    )	r	   
startswithr
   r   compiledsocketgetaddrinfoIPPROTO_TCPSOCK_STREAM)r   r   r(   r)   port_defanss   `     r"   r   r   :   s    VV$$D 4??3'' 4!9+;+;x,,H xT .,6,:t,,--VV$$D 8 ((-H-7

d&,63E  C @???3????r3   )r   r   r   r   )r   r   r   r   )__doc__
__future__r   r6   loggingr   r    r   r   abcr   r   _conninfo_utilsr	   r
   r   r   	getLoggerr   r#   r   r/   r3   r"   <module>rC      s     # " " " " "               & & & & & & & & D D D D D D D D D D + + + + + + 
	9	%	%   B&@ &@ &@ &@ &@ &@r3   