
    2i                        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 	 ddlZ ej&                  d	      Zdd
ZddZy)z8
Separate connection attempts from a connection string.
    )annotationsN)shuffle   )errors)ConnDictConnMapping)	get_paramget_param_defis_ip_addresssplit_attemptspsycopgc                0  K   d}g }t        | d      dk(  x}r)| j                         D ci c]  \  }}|dk7  s|| } }}t        |       D ]%  }	 |j                  t	        |       d{          ' |s|sJ |t        | d      d	k(  rt        |       |r|D cg c]
  }i |dd
i c}|z   }|S c c}}w 7 O# t
        $ rJ}t        j                  d|j                  d      d|       }t        j                  d|       Y d}~d}~ww xY wc c}w w)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-standbyzfailed to resolve host hostz: z%sload_balance_hostsrandomstandby)r	   itemsr   extend_resolve_hostnamesOSErroreOperationalErrorgetloggerdebugr   )	paramslast_excattemptsprefer_standbykvattemptexas	            _/var/www/html/secretsanta/venv/lib/python3.12/site-packages/psycopg/_conninfo_attempts_async.pyconninfo_attempts_asyncr'      s>     HH"6+ABFVVV~V#)<<>Q41aQ:P5P!Q$QQ!&) )	)OO"4W"==>) x-.(: >F
9:4q4()4
 O5 R > 	))))'++f*=)@2$GH LLx((		)"
s]   )DB3B3DB;)B9
*B;2*DD+D9B;;	DA D	D	DDc                  K   t        | d      }|r|j                  d      s|dd dk(  r| gS t        | d      r| gS t        |      r	i | d|igS t        | d      x}st        d      }|xr |j                  xs d}	 t        j                         }|j                  ||t        j                  t        j                  	       d
{   }|D cg c]  }i | d|d   d   i c}S 7  c c}w w)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typeN   r   )r	   
startswithr   r
   compiledasyncioget_running_loopgetaddrinfosocketIPPROTO_TCPSOCK_STREAM)r   r   r-   port_defloopansitems          r&   r   r   D   s     VV$D4??3'4!9+;x$xT,6,:t,--ff--D- (-H--7'')$$$f00v7I7I % 
 
 <??4.v.z471:.??
 @s$   CC.C'C.C)$C.)C.)r   r   returnlist[ConnDict])r   r   r>   r?   )__doc__
__future__r   r7   loggingr   r    r   r   abcr   r   _conninfo_utilsr	   r
   r   r   r4   	getLoggerr   r'   r        r&   <module>rI      sH    #     & T T			9	%*Z*@rH   