
    V.j                     T   d dl Z d dlZd dlZd dlZd dlZd dlZddlmZ ddlmZ ddlm	Z	 ddlm
Z
  ej                  e      Z G d de      Z G d	 d
ej                   ed      Z G d d      Z G d d      Z G d d      Zddddddddddej*                  e
j,                  ddZy)    N   )compat
connection)
exceptions)protocolc                   4     e Zd Zdd fd
Zed        Z xZS )PoolConnectionProxyMetaF)wrapc                   |rt        t        j                        D ]  }|j                  d      s||v rt	        t        j                  |      }t        j                  |      sIt        j                  |      }| j                  ||      }t        j                  ||      }|||<    d|vrt        j                  j                  |d<   t        	| 5  | |||      S )N___doc__)dirr   
Connection
startswithgetattrinspect
isfunctioniscoroutinefunction_wrap_connection_method	functoolsupdate_wrapperr   super__new__)
mclsnamebasesdctr   attrnamemethiscoroutinewrapper	__class__s
            NC:\xampp\htdocs\tradingbinance\backend\.venv\Lib\site-packages\asyncpg/pool.pyr   zPoolConnectionProxyMeta.__new__   s    
 5 56&&s+x3z44h?))$/%99$?66xM#227DA 'H 7 #!+!6!6!>!>IwtT5#66    c                 @      fd}|rt        j                  |       |S )Nc                     | j                   $t        j                  dj                              t	        | j                   j
                        } || j                   g|i |S )NzJcannot call Connection.{}(): connection has been released back to the pool)_conr   InterfaceErrorformatr   r#   )selfargskwargsr    	meth_names       r$   call_con_methodzHPoolConnectionProxyMeta._wrap_connection_method.<locals>.call_con_method0   se    yy  //DDJF!E#$ $
 499..	:D		3D3F33r%   )r   markcoroutinefunction)r.   r!   r/   s   `  r$   r   z/PoolConnectionProxyMeta._wrap_connection_method.   s!    		4 ((9r%   )__name__
__module____qualname__r   staticmethodr   __classcell__)r#   s   @r$   r
   r
      s    05 7(  r%   r
   c                   `    e Zd ZdZdddej
                  fdZd Zdej
                  fdZd	 Z	y
)PoolConnectionProxy)r(   _holderholderPoolConnectionHolderconc                 B    || _         || _        |j                  |        y N)r(   r8   
_set_proxy)r+   r9   r;   s      r$   __init__zPoolConnectionProxy.__init__G   s    	tr%   c                 .    t        | j                  |      S r=   )r   r(   )r+   attrs     r$   __getattr__zPoolConnectionProxy.__getattr__M   s    tyy$''r%   returnc                 j    | j                   y | j                   d c}| _         |j                  d        |S r=   )r(   r>   )r+   r;   s     r$   _detachzPoolConnectionProxy._detachQ   s1    99DTYt
r%   c                     | j                   0dj                  | j                  j                  t	        |             S dj                  | j                  j                  | j                   t	        |             S )Nz <{classname} [released] {id:#x}>)	classnameidz<{classname} {con!r} {id:#x}>)rG   r;   rH   )r(   r*   r#   r1   rH   r+   s    r$   __repr__zPoolConnectionProxy.__repr__Y   sn    995<<..11bh = @ @ 399..11tyyRX : O Or%   N)
r1   r2   r3   	__slots__r   r   r?   rB   rE   rJ    r%   r$   r7   r7   A   sA     $I5  ++(.. Or%   r7   T)	metaclassr   c                   j    e Zd ZdZd Zd Zd Zd ZdefdZ	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zy)r:   )r(   _pool_loop_proxy_max_queries_setup_max_inactive_time_in_use_inactive_callback_timeout_generationc                    || _         d | _        d | _        || _        || _        || _        d | _        d | _        d | _        d | _	        y r=   )
rO   r(   rQ   rR   rT   rS   rV   rU   rW   rX   )r+   poolmax_queriessetupmax_inactive_times        r$   r?   zPoolConnectionHolder.__init__j   sL    
	'"3"&r%   c                 X    | j                   d uxr | j                   j                          S r=   )r(   	is_closedrI   s    r$   is_connectedz!PoolConnectionHolder.is_connectedx   s&    yy$BTYY-@-@-B)BBr%   c                     | j                    S r=   rU   rI   s    r$   is_idlezPoolConnectionHolder.is_idle{   s    <<r%   c                   K   | j                   t        j                  d      | j                  j	                          d {   | _         | j                  j
                  | _        | j                          | j                          y 7 Ew)NzMPoolConnectionHolder.connect() called while another connection already exists)r(   r   InternalClientErrorrO   _get_new_connectionrX   _maybe_cancel_inactive_callback_setup_inactive_callbackrI   s    r$   connectzPoolConnectionHolder.connect~   sp     99 00,- - **88::	::11,,.%%' ;s   ?B	BAB	rC   c                 P  K   | j                   | j                   j                         r d | _         | j                          d {    n| j                  | j                  j                  k7  rh| j                  j
                  j                  | j                   j                  | j                               d | _         | j                          d {    | j                          t        | | j                         x| _        }| j                  	 | j                  |       d {    | j                  j
                  j!                         | _        |S 7 7 7 4# t        t        j                  f$ r1}	 | j                   j                          d {  7   |# |xY wd }~ww xY ww)Ntimeout)r(   r_   ri   rX   rO   rP   create_taskcloserW   rg   r7   rQ   rS   	ExceptionasyncioCancelledErrorcreate_futurerU   )r+   proxyexs      r$   acquirezPoolConnectionHolder.acquire   sG    99		 3 3 5DI,,.  !7!77JJ((		68DI,,.  ,,.1$		BBe;;"kk%((( zz''557A ! ! )w556 

 ))//+++H"Hsy   AF&EBF&E=F&E "E#E ',F&F&E F#3FFFFFFF##F&c                 z  K   | j                   t        j                  d      | j                  j	                         ry d | _        | j                  j                  j                  | j                  k\  r%| j                  j                  |       d {    y | j                  | j                  j                  k7  r%| j                  j                  |       d {    y 	 |}| j                  j                  j                         rpt        j                         }t        j                   | j                  j                  j#                         |       d {    ||t        j                         |z
  z  }| j                  j$                  ~t        j&                  |      4 d {    | j                  j%                          d {    | j                  j%                  | j                         d {    d d d       d {    n$| j                  j)                  |       d {    | j3                          | j5                          y 7 7 y7 7 7 7 f7 X# 1 d {  7  sw Y   CxY w7 J# t*        t,        j.                  f$ r(}	 | j                  j1                          |# |xY wd }~ww xY ww)NzAPoolConnectionHolder.release() called on a free connection holderrk   )rU   r   re   r(   r_   rW   	_protocolqueries_countrR   rn   rX   rO   _is_cancellingtime	monotonicr   wait_for_wait_for_cancellation_resetrl   resetro   rp   rq   	terminate_releaserh   )r+   rl   budgetstartedrt   s        r$   releasezPoolConnectionHolder.release   s1    <<00+, , 99  99,,0A0AA ))//'/222tzz555 ))//'/222	Fyy""113 ..*ooII''>>@   %dnn.88Fzz  ,!>>&11))**,,,**++DII666 211 iioofo555 	 	%%'[ 3 3 2,6 2111 67112 		 		##%b		s   BJ;IAJ;IJ;!A5I7 IAI7 &I'I7 *I I	,I 5I6I :I7 I$I7 *I5+I7 /"J;J;I7 I7 I I I7  I2&I)'I2.I7 7J8J-+J3-J00J33J88J;c                 R   K   | j                   y | j                    d {    y 7 wr=   rb   rI   s    r$   wait_until_releasedz(PoolConnectionHolder.wait_until_released   s      <<,,s   '%'c                 n   K   | j                   #| j                   j                          d {    y y 7 wr=   )r(   rn   rI   s    r$   rn   zPoolConnectionHolder.close   s0     99  ))//### ! $s   *535c                 R    | j                   | j                   j                          y y r=   )r(   r   rI   s    r$   r   zPoolConnectionHolder.terminate   s#    99  II! !r%   c                     | j                   t        j                  d      | j                  r@| j                  j
                  j                  | j                  | j                        | _         y y )Nz/pool connection inactivity timer already exists)rV   r   re   rT   rO   rP   
call_later_deactivate_inactive_connectionrI   s    r$   rh   z-PoolConnectionHolder._setup_inactive_callback   se    "".00AC C ""&*jj&6&6&A&A'')M)M'OD# #r%   c                 `    | j                   "| j                   j                          d | _         y y r=   )rV   cancelrI   s    r$   rg   z4PoolConnectionHolder._maybe_cancel_inactive_callback  s-    "".##**,&*D# /r%   c                     | j                   t        j                  d      | j                  +| j                  j	                          | j                          y y )Nz/attempting to deactivate an acquired connection)rU   r   re   r(   r   _release_on_closerI   s    r$   r   z4PoolConnectionHolder._deactivate_inactive_connection  sU    <<#00AC C 99  II! ""$ !r%   c                 R    | j                          | j                          d | _        y r=   )rg   r   r(   rI   s    r$   r   z&PoolConnectionHolder._release_on_close  s    ,,.	r%   c                 :   | j                   y| j                   j                         s| j                   j                  d       d| _         | j                  !| j                  j	                          d| _        | j
                  j                  j                  |        y)zRelease this connection holder.N)rU   done
set_resultrQ   rE   rO   _queue
put_nowaitrI   s    r$   r   zPoolConnectionHolder._release   su    <<||  "LL##D) ;;"KK!DK 	

$$T*r%   N)r1   r2   r3   rK   r?   r`   rc   ri   r7   ru   r   r   rn   r   rh   rg   r   r   r   rL   r%   r$   r:   r:   b   s[     I C 	(#2 #J=(~$"O+
%
+r%   r:   c                      e Zd ZdZdZddddddZd Zd Zd Zd	 Z	d
 Z
d Zd Zd2dZd ZdddededefdZdddedefdZddddefdZddddZddddZddddZddddddddddddddZddddddddddd
d Zddddddddddddddddd!d"Zddddd#d$Zddd%Zd& Zddd'Zd( Zd) Z d* Z!d+ Z"d, Z#d- Z$d. Z%d/ Z&d0 Z'd1 Z(y)3Poola  A connection pool.

    Connection pool can be used to manage a set of connections to the database.
    Connections are first acquired from the pool, then used, and then released
    back to the pool.  Once a connection is released, it's reset to close all
    open cursors and other resources *except* prepared statements.

    Pools are created by calling :func:`~asyncpg.pool.create_pool`.
    )r   rP   _minsize_maxsize_init_connectr~   _connect_args_connect_kwargs_holders_initialized_initializing_closing_closed_connection_class_record_classrX   rS   rR   !_max_inactive_connection_lifetimeN)ri   r\   initr   c                0   t        |      dkD  rt        j                  dt        d       |	t	        j
                         }	|	| _        |dk  rt        d      |dk  rt        d      ||kD  rt        d      |dk  rt        d	      |dk  rt        d
      t        |
t        j                        st        dj                  |
            t        |t        j                        st        dj                  |            || _        || _        g | _        d| _        d| _        d | _        |
| _        || _        d| _        d| _        d| _        ||nt        j4                  | _        || _        || _        || _        || _        || _         || _!        || _"        y )Nr   zPassing multiple positional arguments to asyncpg.Pool constructor is deprecated and will be removed in asyncpg 0.17.0.  The non-deprecated form is asyncpg.Pool(<dsn>, **kwargs)   )
stacklevelr   z,max_size is expected to be greater than zeroz3min_size is expected to be greater or equal to zeroz!min_size is greater than max_sizez/max_queries is expected to be greater than zerozKmax_inactive_connection_lifetime is expected to be greater or equal to zerozMconnection_class is expected to be a subclass of asyncpg.Connection, got {!r}zErecord_class is expected to be a subclass of asyncpg.Record, got {!r}F)#lenwarningswarnDeprecationWarningrp   get_event_looprP   
ValueError
issubclassr   r   	TypeErrorr*   r   Recordr   r   r   r   r   r   r   r   r   r   rX   ri   r   r   r   rS   r   r~   rR   r   )r+   min_sizemax_sizer[    max_inactive_connection_lifetimeri   r\   r   r   loopconnection_classrecord_classconnect_argsconnect_kwargss                 r$   r?   zPool.__init__G  s    |q MM0 #q2 <))+D
q=KLLa<EG G h@AA!NOO+a/#$ $ *J,A,AB//5v6F/GI I ,8++16,+?A A ! !"!1)#*#6J<N<N)-
', 	.r%   c                 4  K   | j                   r| S | j                  rt        j                  d      | j                  rt        j                  d      d| _        	 | j                          d {    | d| _        d| _         S 7 # d| _        d| _         w xY ww)Nz)pool is being initialized in another taskpool is closedTF)r   r   r   r)   r   _initializerI   s    r$   _async__init__zPool._async__init__  s     K++;= =<<++,<==!	%""$$$!&D $D	 % "'D $Ds0   ABB .B/B 4BB BBc                   K   t        j                  | j                        | _        t	        | j                        D ]e  }t        | | j                  | j                  | j                        }| j                  j                  |       | j                  j                  |       g | j                  r| j                  d   }|j                          d {    | j                  dkD  ryg }t        t        | j                  d d             D ]8  \  }}|| j                  dz
  k\  r n!|j                  |j                                : t        j                   |  d {    y y y 7 7 	w)N)maxsize)r[   r]   r\   r   )rp   	LifoQueuer   r   ranger:   rR   r   rS   r   appendr   r   ri   	enumeratereversedgather)r+   r   chfirst_chconnect_tasksis         r$   r   zPool._initialize  s,    ''>t}}%A% --"&"H"Hkk	#B MM  $KK""2& & == }}R(H""$$$}}q  "&xcr0B'CDEArDMMA--!((6	 E nnm444 !  % 5s%   CE&E"BE&E$E&$E&c                 6    | j                   xs | j                  S )z_Return ``True`` if the pool is closing or is closed.

        .. versionadded:: 0.28.0
        )r   r   rI   s    r$   
is_closingzPool.is_closing  s    
 ||,t}},r%   c                 :    t        d | j                  D              S )zaReturn the current number of connections in this pool.

        .. versionadded:: 0.25.0
        c              3   <   K   | ]  }|j                           y wr=   )r`   .0hs     r$   	<genexpr>z Pool.get_size.<locals>.<genexpr>  s     ;]1>>#]s   sumr   rI   s    r$   get_sizezPool.get_size  s    
 ;T]];;;r%   c                     | j                   S )zaReturn the minimum number of connections in this pool.

        .. versionadded:: 0.25.0
        )r   rI   s    r$   get_min_sizezPool.get_min_size      
 }}r%   c                     | j                   S )ziReturn the maximum allowed number of connections in this pool.

        .. versionadded:: 0.25.0
        )r   rI   s    r$   get_max_sizezPool.get_max_size  r   r%   c                 :    t        d | j                  D              S )zfReturn the current number of idle connections in this pool.

        .. versionadded:: 0.25.0
        c              3   `   K   | ]&  }|j                         xr |j                          ( y wr=   )r`   rc   r   s     r$   r   z%Pool.get_idle_size.<locals>.<genexpr>  s&     K]1>>#3		3]s   ,.r   rI   s    r$   get_idle_sizezPool.get_idle_size  s    
 KT]]KKKr%   c                 "    |g| _         || _        y)a  Set the new connection arguments for this pool.

        The new connection arguments will be used for all subsequent
        new connection attempts.  Existing connections will remain until
        they expire. Use :meth:`Pool.expire_connections()
        <asyncpg.pool.Pool.expire_connections>` to expedite the connection
        expiry.

        :param str dsn:
            Connection arguments specified using as a single string in
            the following format:
            ``postgres://user:pass@host:port/database?option=value``.

        :param \*\*connect_kwargs:
            Keyword arguments for the :func:`~asyncpg.connection.connect`
            function.

        .. versionadded:: 0.16.0
        N)r   r   )r+   dsnr   s      r$   set_connect_argszPool.set_connect_args  s    * "U-r%   c                   K    | j                   | j                  | j                  | j                  | j                  d| j
                   d {   }t        || j                        s| j                  }|j                   d|j                   }t        |      }|j                  dk(  r|j                  }n|j                   d|j                   }t        j                  d| d| d      | j                  	 | j                  |       d {    |S |S 7 7 
# t        t        j                  f$ r'}	 |j!                          d {  7   |# |xY wd }~ww xY ww)N)r   r   r   .builtinsz9expected pool connect callback to return an instance of 'z', got '')r   r   rP   r   r   r   
isinstancer2   r1   typer   r)   r   ro   rp   rq   rn   )r+   r;   goodgood_nbadbad_nrt   s          r$   rf   zPool._get_new_connection  sW    !DMM!33++	

 ""
 
 #t556))D($--9Fs)C~~+>>*!CLL>:++8<wa1 
 ::!jjo%% 
s
K
, &w556 

 ))+%%H"Hsg   AEDB+E;D DD ED E6E	E
EEEEEErk   queryrl   rC   c                   K   | j                         4 d{   } |j                  |g|d|i d{   cddd      d{    S 7 37 7 	# 1 d{  7  sw Y   yxY ww)a  Execute an SQL command (or commands).

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.execute() <asyncpg.connection.Connection.execute>`.

        .. versionadded:: 0.10.0
        Nrl   )ru   execute)r+   r   rl   r,   r;   s        r$   r   zPool.execute,  sJ      <<>>S$UCTC7CC ">>C ">>>sS   A&AA&AAAA&AA&AA&A#AA#A&commandc                   K   | j                         4 d{   }|j                  |||       d{   cddd      d{    S 7 17 7 	# 1 d{  7  sw Y   yxY ww)aC  Execute an SQL *command* for each sequence of arguments in *args*.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.executemany()
        <asyncpg.connection.Connection.executemany>`.

        .. versionadded:: 0.10.0
        Nrk   )ru   executemany)r+   r   r,   rl   r;   s        r$   r   zPool.executemany8  sD      <<>>S$HH ">>H ">>>sS   A$A	A$AAAA$AA$AA$A!AA!A$rl   r   c                   K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a-  Run a query and return the results as a list of :class:`Record`.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetch() <asyncpg.connection.Connection.fetch>`.

        .. versionadded:: 0.10.0
        Nr   )ru   fetchr+   r   rl   r   r,   r;   s         r$   r   z
Pool.fetchE  s\      <<>>S"  )	  ">> ">>>S   A'AA'AAAA'AA'AA'A$AA$ A'r   columnrl   c                   K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a+  Run a query and return a value in the first row.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchval()
        <asyncpg.connection.Connection.fetchval>`.

        .. versionadded:: 0.10.0
        Nr   )ru   fetchval)r+   r   r   rl   r,   r;   s         r$   r   zPool.fetchval\  sY      <<>>S%>>%+W> > ">>> ">>>r   c                   K   | j                         4 d{   } |j                  |g|||d d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)a  Run a query and return the first row.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchrow() <asyncpg.connection.Connection.fetchrow>`.

        .. versionadded:: 0.10.0
        Nr   )ru   fetchrowr   s         r$   r   zPool.fetchrowj  s\      <<>>S%  )	  ">> ">>>r   c                   K   | j                         4 d{   }|j                  ||||       d{   cddd      d{    S 7 27 7 	# 1 d{  7  sw Y   yxY ww)an  Run a query for each sequence of arguments in *args*
        and return the results as a list of :class:`Record`.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.fetchmany()
        <asyncpg.connection.Connection.fetchmany>`.

        .. versionadded:: 0.30.0
        Nr   )ru   	fetchmany)r+   r   r,   rl   r   r;   s         r$   r   zPool.fetchmany{  sP      <<>>StW< '   ">> ">>>sS   A%A
A%AAAA%AA%AA%A"AA"A%)columnsschema_namerl   r*   oids	delimiternullheaderquoteescapeforce_quoteencodingc                   K   | j                         4 d{   }|j                  |||||||||	|
||||       d{   cddd      d{    S 7 <7 7 	# 1 d{  7  sw Y   yxY ww)a;  Copy table contents to a file or file-like object.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_from_table()
        <asyncpg.connection.Connection.copy_from_table>`.

        .. versionadded:: 0.24.0
        N)outputr   r   rl   r*   r  r  r  r  r  r  r  r  )ru   copy_from_table)r+   
table_namer
  r   r   rl   r*   r  r  r  r  r  r  r  r  r;   s                   r$   r  zPool.copy_from_table  ss     6 <<>>S,,'#'! -   ">> ">>>sT   A/AA/#AAAA/AA/AA/A, A#!A,(A/)
rl   r*   r  r  r  r  r  r  r  r  c                   K   | j                         4 d{   } |j                  |g|||||||||	|
||d d{   cddd      d{    S 7 =7 7 	# 1 d{  7  sw Y   yxY ww)aC  Copy the results of a query to a file or file-like object.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_from_query()
        <asyncpg.connection.Connection.copy_from_query>`.

        .. versionadded:: 0.24.0
        N)r
  rl   r*   r  r  r  r  r  r  r  r  )ru   copy_from_query)r+   r   r
  rl   r*   r  r  r  r  r  r  r  r  r,   r;   s                  r$   r  zPool.copy_from_query  sy     2 <<>>S,,, #'!  ">> ">>>sU   A0AA0$AA AA0AA0AA0A-!A$"A-)A0)r   r   rl   r*   r  freezer  r  r  r  r  r  force_not_null
force_nullr  wherec                   K   | j                         4 d{   }|j                  |||||||||	|
||||||||       d{   cddd      d{    S 7 @7 7 	# 1 d{  7  sw Y   yxY ww)a&  Copy data to the specified table.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_to_table()
        <asyncpg.connection.Connection.copy_to_table>`.

        .. versionadded:: 0.24.0
        N)sourcer   r   rl   r*   r  r  r  r  r  r  r  r  r  r  r  r  )ru   copy_to_table)r+   r  r  r   r   rl   r*   r  r  r  r  r  r  r  r  r  r  r  r  r;   s                       r$   r  zPool.copy_to_table  s     > <<>>S**'#'-%!% +   ">> ">>>sV   A3AA3'AAAA3AA3AA3A0$A'%A0,A3)r   r   rl   r  c          	         K   | j                         4 d{   }|j                  ||||||       d{   cddd      d{    S 7 47 7 	# 1 d{  7  sw Y   yxY ww)aU  Copy a list of records to the specified table using binary COPY.

        Pool performs this operation using one of its connections.  Other than
        that, it behaves identically to
        :meth:`Connection.copy_records_to_table()
        <asyncpg.connection.Connection.copy_records_to_table>`.

        .. versionadded:: 0.24.0
        N)recordsr   r   rl   r  )ru   copy_records_to_table)r+   r  r  r   r   rl   r  r;   s           r$   r  zPool.copy_records_to_table  s[     & <<>>S22' 3   ">> ">>>r   c                    t        | |      S )aT  Acquire a database connection from the pool.

        :param float timeout: A timeout for acquiring a Connection.
        :return: An instance of :class:`~asyncpg.connection.Connection`.

        Can be used in an ``await`` expression or with an ``async with`` block.

        .. code-block:: python

            async with pool.acquire() as con:
                await con.execute(...)

        Or:

        .. code-block:: python

            con = await pool.acquire()
            try:
                await con.execute(...)
            finally:
                await pool.release(con)
        )PoolAcquireContext)r+   rl   s     r$   ru   zPool.acquire4  s    . "$00r%   c                     K    fd} j                   rt        j                  d       j                           |        d {   S t	        j
                   |              d {   S 7 (7 w)Nc                    K   j                   j                          d {   } 	 | j                          d {   }| _        |S 7 &7 # t        t
        j                  f$ r j                   j                  |         w xY wwr=   )r   getru   rW   ro   rp   rq   r   )r   rs   r+   rl   s     r$   _acquire_implz$Pool._acquire.<locals>._acquire_implN  sr     {{((B	 jjl* & )*w556 &&r*s5   BABA A
A 
B
A 6BBzpool is closingrk   )r   r   r)   _check_initr   r|   )r+   rl   r  s   `` r$   _acquirezPool._acquireM  sk     	 ==++,=>>?&((2 2 2 )2s$   AA5A1	#A5,A3-A53A5c                  K   t        |      t        us|j                  j                  | ur%t	        j
                  dj                  |            |j                  y| j                          |j                  j                          |j                  }||j                  }t        j                  |j                  |             d{   S 7 w)a  Release a database connection back to the pool.

        :param Connection connection:
            A :class:`~asyncpg.connection.Connection` object to release.
        :param float timeout:
            A timeout for releasing the connection.  If not specified, defaults
            to the timeout provided in the corresponding call to the
            :meth:`Pool.acquire() <asyncpg.pool.Pool.acquire>` method.

        .. versionchanged:: 0.14.0
            Added the *timeout* parameter.
        zWPool.release() received invalid connection: {connection!r} is not a member of this poolr   N)r   r7   r8   rO   r   r)   r*   r(   r  _on_releaserW   rp   shieldr   )r+   r   rl   r   s       r$   r   zPool.releasee  s      $77""((4++>>Df) ?E ?+, ,
 ??" 	##%?kkG
 ^^BJJw$78888s   CC	C
Cc                   K   | j                   ry| j                          d| _        d}	 | j                  j	                  d| j
                        }| j                  D cg c]  }|j                          }}t        j                  |  d{    | j                  D cg c]  }|j                          }}t        j                  |  d{    	 ||j                          d| _         d| _        yc c}w 7 lc c}w 7 2# t        t        j                  f$ r | j                           w xY w# ||j                          d| _         d| _        w xY ww)a  Attempt to gracefully close all connections in the pool.

        Wait until all pool connections are released, close them and
        shut down the pool.  If any error (including cancellation) occurs
        in ``close()`` the pool will terminate by calling
        :meth:`Pool.terminate() <pool.Pool.terminate>`.

        It is advisable to use :func:`python:asyncio.wait_for` to set
        a timeout.

        .. versionchanged:: 0.16.0
            ``close()`` now waits until all pool connections are released
            before closing them and the pool.  Errors raised in ``close()``
            will cause immediate pool termination.
        NT<   F)r   r  r   rP   r   _warn_on_long_closer   r   rp   r   rn   ro   rq   r   r   )r+   warning_callbackr   release_corosclose_coross        r$   rn   z
Pool.close  sE      <<	"#zz44D,, . 48==B3@R&&(=  B..-000 &*]]4%2r
]  4..+...  + '')DL!DM!B04.7112 	NN	
  + '')DL!DMsd   'E4D C45D C9D C;6D D D !E4D ;D +D--D0 0"EEc                 .    t         j                  d       y )NzPool.close() is taking over 60 seconds to complete. Check if you have any unreleased connections left. Use asyncio.wait_for() to set a timeout for Pool.close().)loggerwarningrI   s    r$   r&  zPool._warn_on_long_close  s     ' 	(r%   c                     | j                   ry| j                          | j                  D ]  }|j                           d| _         y)z&Terminate all connections in the pool.NT)r   r  r   r   r+   r   s     r$   r   zPool.terminate  s6    <<--BLLN  r%   c                 6   K   | xj                   dz  c_         yw)zExpire all currently open connections.

        Cause all currently open connections to get replaced on the
        next :meth:`~asyncpg.pool.Pool.acquire()` call.

        .. versionadded:: 0.16.0
        r   N)rX   rI   s    r$   expire_connectionszPool.expire_connections  s      	As   c                     | j                   s6| j                  rt        j                  d      t        j                  d      | j                  rt        j                  d      y )Nzipool is being initialized, but not yet ready: likely there is a race between creating a pool and using itzpool is not initializedr   )r   r   r   r)   r   rI   s    r$   r  zPool._check_init  s^      !! //    ++,EFF<<++,<== r%   c                 t    | j                   D ])  }|j                  |j                  j                          + y r=   )r   r(   _drop_local_statement_cacher.  s     r$   _drop_statement_cachezPool._drop_statement_cache  s*    --Bww"335  r%   c                 t    | j                   D ])  }|j                  |j                  j                          + y r=   )r   r(   _drop_local_type_cacher.  s     r$   _drop_type_cachezPool._drop_type_cache  s*    --Bww"..0  r%   c                 >    | j                         j                         S r=   )r   	__await__rI   s    r$   r9  zPool.__await__  s    ""$..00r%   c                 B   K   | j                          d {    | S 7 wr=   )r   rI   s    r$   
__aenter__zPool.__aenter__  s"     !!### 	$s   c                 @   K   | j                          d {    y 7 wr=   )rn   )r+   excs     r$   	__aexit__zPool.__aexit__  s     jjls   r=   ))r1   r2   r3   r   rK   r?   r   r   r   r   r   r   r   r   rf   strfloatr   r   listr   r   r   r   r  r  r  r  ru   r   r   rn   r&  r   r0  r  r4  r7  r9  r;  r>  rL   r%   r$   r   r   4  s   I O-b% 5B-<L.0&P ?C 
D3 
D 
Ds 
D GK I I I"  
. 34T > 48d " 7; * !+d (^ )3t : "& 1220 48 $9L+"Z(	>611r%   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)r  )rl   r   r   rZ   c                 <    || _         || _        d | _        d| _        y )NF)rZ   rl   r   r   )r+   rZ   rl   s      r$   r?   zPoolAcquireContext.__init__  s    		r%   c                    K   | j                   | j                  rt        j                  d      | j                  j                  | j                         d {   | _         | j                   S 7 w)Nz a connection is already acquired)r   r   r   r)   rZ   r   rl   rI   s    r$   r;  zPoolAcquireContext.__aenter__  sU     ??&$))++,NOO $		 2 24<< @@ As   AA0A.A0c                    K   d| _         | j                  }d | _        | j                  j                  |       d {    y 7 wNT)r   r   rZ   r   )r+   r=  r;   s      r$   r>  zPoolAcquireContext.__aexit__  s5     	ooii$$$s   9AAAc                 v    d| _         | j                  j                  | j                        j	                         S rF  )r   rZ   r   rl   r9  rI   s    r$   r9  zPoolAcquireContext.__await__	  s+    	yy!!$,,/99;;r%   N)r1   r2   r3   rK   r?   r;  r>  r9  rL   r%   r$   r  r    s    9I%<r%   r  
   iP  g     r@)r   r   r[   r   ri   r\   r   r   r   r   r   c                2    t        | f|
|||||	|||||d|S )a8  Create a connection pool.

    Can be used either with an ``async with`` block:

    .. code-block:: python

        async with asyncpg.create_pool(user='postgres',
                                       command_timeout=60) as pool:
            await pool.fetch('SELECT 1')

    Or to perform multiple operations on a single connection:

    .. code-block:: python

        async with asyncpg.create_pool(user='postgres',
                                       command_timeout=60) as pool:
            async with pool.acquire() as con:
                await con.execute('''
                   CREATE TABLE names (
                      id serial PRIMARY KEY,
                      name VARCHAR (255) NOT NULL)
                ''')
                await con.fetch('SELECT 1')

    Or directly with ``await`` (not recommended):

    .. code-block:: python

        pool = await asyncpg.create_pool(user='postgres', command_timeout=60)
        con = await pool.acquire()
        try:
            await con.fetch('SELECT 1')
        finally:
            await pool.release(con)

    .. warning::
        Prepared statements and cursors returned by
        :meth:`Connection.prepare() <asyncpg.connection.Connection.prepare>`
        and :meth:`Connection.cursor() <asyncpg.connection.Connection.cursor>`
        become invalid once the connection is released.  Likewise, all
        notification and log listeners are removed, and ``asyncpg`` will
        issue a warning if there are any listener callbacks registered on a
        connection that is being released to the pool.

    :param str dsn:
        Connection arguments specified using as a single string in
        the following format:
        ``postgres://user:pass@host:port/database?option=value``.

    :param \*\*connect_kwargs:
        Keyword arguments for the :func:`~asyncpg.connection.connect`
        function.

    :param Connection connection_class:
        The class to use for connections.  Must be a subclass of
        :class:`~asyncpg.connection.Connection`.

    :param type record_class:
        If specified, the class to use for records returned by queries on
        the connections in this pool.  Must be a subclass of
        :class:`~asyncpg.Record`.

    :param int min_size:
        Number of connection the pool will be initialized with.

    :param int max_size:
        Max number of connections in the pool.

    :param int max_queries:
        Number of queries after a connection is closed and replaced
        with a new connection.

    :param float max_inactive_connection_lifetime:
        Number of seconds after which inactive connections in the
        pool will be closed.  Pass ``0`` to disable this mechanism.

    :param coroutine connect:
        A coroutine that is called instead of
        :func:`~asyncpg.connection.connect` whenever the pool needs to make a
        new connection.  Must return an instance of type specified by
        *connection_class* or :class:`~asyncpg.connection.Connection` if
        *connection_class* was not specified.

    :param coroutine setup:
        A coroutine to prepare a connection right before it is returned
        from :meth:`Pool.acquire()`.  An example use
        case would be to automatically set up notifications listeners for
        all connections of a pool.

    :param coroutine init:
        A coroutine to initialize a connection when it is created.
        An example use case would be to setup type codecs with
        :meth:`Connection.set_builtin_type_codec() <\
        asyncpg.connection.Connection.set_builtin_type_codec>`
        or :meth:`Connection.set_type_codec() <\
        asyncpg.connection.Connection.set_type_codec>`.

    :param coroutine reset:
        A coroutine to reset a connection before it is returned to the pool by
        :meth:`Pool.release()`.  The function is supposed
        to reset any changes made to the database session so that the next
        acquirer gets the connection in a well-defined state.

        The default implementation calls :meth:`Connection.reset() <\
        asyncpg.connection.Connection.reset>`, which runs the following::

            SELECT pg_advisory_unlock_all();
            CLOSE ALL;
            UNLISTEN *;
            RESET ALL;

        The exact reset query is determined by detected server capabilities,
        and a custom *reset* implementation can obtain the default query
        by calling :meth:`Connection.get_reset_query() <\
        asyncpg.connection.Connection.get_reset_query>`.

    :param loop:
        An asyncio event loop instance.  If ``None``, the default
        event loop will be used.

    :return: An instance of :class:`~asyncpg.pool.Pool`.

    .. versionchanged:: 0.10.0
       An :exc:`~asyncpg.exceptions.InterfaceError` will be raised on any
       attempted operation on a released connection.

    .. versionchanged:: 0.13.0
       An :exc:`~asyncpg.exceptions.InterfaceError` will be raised on any
       attempted operation on a prepared statement or a cursor created
       on a connection that has been released to the pool.

    .. versionchanged:: 0.13.0
       An :exc:`~asyncpg.exceptions.InterfaceWarning` will be produced
       if there are any active listeners (added via
       :meth:`Connection.add_listener()
       <asyncpg.connection.Connection.add_listener>`
       or :meth:`Connection.add_log_listener()
       <asyncpg.connection.Connection.add_log_listener>`) present on the
       connection at the moment of its release to the pool.

    .. versionchanged:: 0.22.0
       Added the *record_class* parameter.

    .. versionchanged:: 0.30.0
       Added the *connect* and *reset* parameters.
    )r   r   r   r   r[   r   ri   r\   r   r   r   )r   )r   r   r   r[   r   ri   r\   r   r   r   r   r   r   s                r$   create_poolrJ    sE    ~ )!)I  r%   r=   )rp   r   r   loggingrz   r    r   r   r   r   	getLoggerr1   r+  r   r
   _ConnectionProxyr7   r:   r   r  r   r   rJ  rL   r%   r$   <module>rO     s              
		8	$&d &RO*55$;#OBO+ O+d|
 |
~< <6m!16!+!6!6%__mr%   