o
    DDi                     @  s   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mZ ed	ZdddZdddZdS )z8
Separate connection attempts from a connection string.
    )annotationsN)shuffle   )errors)ConnDictConnMapping)	get_paramget_param_defis_ip_addresssplit_attemptspsycopgparamsr   returnlist[ConnDict]c                 C  s   d}g }t | ddk }rdd |  D } t| D ]1}z	|t| W q tyK } ztd|dd| }t	
d	| W Y d}~qd}~ww |sT|sRJ |t | d
dkr_t| |rjdd |D | }|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.
    Ntarget_session_attrszprefer-standbyc                 S  s   i | ]\}}|d kr||qS )r    ).0kvr   r   S/var/www/Datamplify/venv/lib/python3.10/site-packages/psycopg/_conninfo_attempts.py
<dictcomp>)   s    z%conninfo_attempts.<locals>.<dictcomp>zfailed to resolve host hostz: z%sload_balance_hostsrandomc                 S  s   g | ]
}i |d diqS )r   standbyr   )r   ar   r   r   
<listcomp>?   s    z%conninfo_attempts.<locals>.<listcomp>)r   itemsr   extend_resolve_hostnamesOSErroreOperationalErrorgetloggerdebugr   )r   last_excattemptsprefer_standbyattemptexr   r   r   conninfo_attempts   s6   r+   r   c                   s   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d	} fd
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                   s$   g | ]}i  d |d d iqS )r/      r   r   )r   itemr   r   r   r   j   s   $ z&_resolve_hostnames.<locals>.<listcomp>)	r   
startswithr
   r	   compiledsocketgetaddrinfoIPPROTO_TCPSOCK_STREAM)r   r   r0   port_defansr   r6   r   r   F   s   

r   )r   r   r   r   )r   r   r   r   )__doc__
__future__r   r9   loggingr   r    r   r!   abcr   r   _conninfo_utilsr   r	   r
   r   	getLoggerr$   r+   r   r   r   r   r   <module>   s   

/