
    g                         d Z ddlZddlmZ  G d dej                        Z G d d      Z G d d	e      Z G d
 de      Zy)z`Connection pooling for psycopg2

This module implements thread-safe (and not) connection pools.
    N)
extensionsc                       e Zd Zy)	PoolErrorN)__name__
__module____qualname__     M/var/www/api/v1/venv_getwork_v1/lib/python3.12/site-packages/psycopg2/pool.pyr   r       s    r
   r   c                   :    e Zd ZdZd Zd	dZd Zd	dZd
dZd Z	y)AbstractConnectionPoolzGeneric key-based pooling code.c                     t        |      | _        t        |      | _        d| _        || _        || _        g | _        i | _        i | _        d| _	        t        | j                        D ]  }| j                           y)zInitialize the connection pool.

        New 'minconn' connections are created immediately calling 'connfunc'
        with given parameters. The connection pool will support a maximum of
        about 'maxconn' connections.
        Fr   N)intminconnmaxconnclosed_args_kwargs_pool_used_rused_keysrange_connect)selfr   r   argskwargsis         r   __init__zAbstractConnectionPool.__init__'   sg     7|7|



t||$AMMO %r
   Nc                     t        j                  | j                  i | j                  }|)|| j                  |<   || j
                  t        |      <   |S | j                  j                  |       |S )z;Create a new connection and assign it to 'key' if not None.)	psycopg2connectr   r   r   r   idr   appendr   keyconns      r   r   zAbstractConnectionPool._connect=   sc    <t||<?"DJJsO$'DKK4!  JJd#r
   c                 D    | xj                   dz  c_         | j                   S )zReturn a new unique key.   )r   r   s    r   _getkeyzAbstractConnectionPool._getkeyG   s    

a
zzr
   c                    | j                   rt        d      || j                         }|| j                  v r| j                  |   S | j                  rC| j                  j                         x| j                  |<   }|| j                  t        |      <   |S t        | j                        | j                  k(  rt        d      | j                  |      S )9Get a free connection and assign it to 'key' if not None.connection pool is closedzconnection pool exhausted)r   r   r+   r   r   popr   r#   lenr   r   r%   s      r   _getconnzAbstractConnectionPool._getconnL   s    ;;788;,,.C$**::c?"::%)ZZ^^%55DJJsOd$'DKK4!K4::$,,. ;<<==%%r
   c                    | j                   rt        d      |1| j                  j                  t	        |            }|t        d      t        | j                        | j                  k  r|s|j                   s|j                  j                  }|t        j                  k(  r|j                          nk|t        j                  k7  r,|j                          | j                  j                  |       n,| j                  j                  |       n|j                          | j                   r|| j                   v r$| j                   |= | j                  t	        |      = yy)zPut away a connection.r.   Nz trying to put unkeyed connection)r   r   r   getr#   r0   r   r   infotransaction_status_extTRANSACTION_STATUS_UNKNOWNcloseTRANSACTION_STATUS_IDLErollbackr$   r   )r   r'   r&   r8   statuss        r   _putconnzAbstractConnectionPool._putconn_   s   ;;788;++//"T(+C{ BCCtzz?T\\)% ;;55T<<<JJLt;;;MMOJJ%%d+ JJ%%d+ JJL {{cTZZ/

3BtH% 0r
   c                     | j                   rt        d      | j                  t        | j                  j                               z   D ]  }	 |j                           d| _         y# t        $ r Y )w xY w)zClose all connections.

        Note that this can lead to some code fail badly when trying to use
        an already closed connection. If you call .closeall() make sure
        your code can deal with it.
        r.   TN)r   r   r   listr   valuesr8   	Exception)r   r'   s     r   	_closeallz AbstractConnectionPool._closeall   sg     ;;788JJdjj&7&7&9!::D

 ;
   s   A&&	A21A2N)NF)
r   r   r   __doc__r   r   r+   r1   r<   rA   r	   r
   r   r   r   $   s$    ),
&&!&Fr
   r   c                   X    e Zd ZdZej
                  Zej                  Zej                  Z
y)SimpleConnectionPoolz@A connection pool that can't be shared across different threads.N)r   r   r   rC   r   r1   getconnr<   putconnrA   closeallr	   r
   r   rE   rE      s&    J$--G$--G%//Hr
   rE   c                   ,    e Zd ZdZd ZddZddZd Zy)	ThreadedConnectionPoolz7A connection pool that works with the threading module.c                 l    ddl }t        j                  | ||g|i | |j                         | _        y)zInitialize the threading lock.r   N)	threadingr   r   Lock_lock)r   r   r   r   r   rL   s         r   r   zThreadedConnectionPool.__init__   s8    '''7	5%)	5-3	5^^%
r
   Nc                     | j                   j                          	 | j                  |      | j                   j                          S # | j                   j                          w xY w)r-   )rN   acquirer1   release)r   r&   s     r   rF   zThreadedConnectionPool.getconn   sD    

	!==%JJ DJJ    A A#c                     | j                   j                          	 | j                  |||       | j                   j                          y# | j                   j                          w xY w)zPut away an unused connection.N)rN   rP   r<   rQ   )r   r'   r&   r8   s       r   rG   zThreadedConnectionPool.putconn   sH    

	!MM$U+JJ DJJ s   A
 
A&c                     | j                   j                          	 | j                          | j                   j                          y# | j                   j                          w xY w)z6Close all connections (even the one currently in use.)N)rN   rP   rA   rQ   r*   s    r   rH   zThreadedConnectionPool.closeall   sB    

	!NNJJ DJJ rR   rB   )NNF)r   r   r   rC   r   rF   rG   rH   r	   r
   r   rJ   rJ      s    A&!!!r
   rJ   )	rC   r!   r   r6   Errorr   r   rE   rJ   r	   r
   r   <module>rV      sI   6  '	 	l l^01 0 !3  !r
   