
    9h                         S r SSKrSSKJr   " S S\R                  5      r " S S5      r " S S	\5      r " S
 S\5      rg)z`Connection pooling for psycopg2

This module implements thread-safe (and not) connection pools.
    N)
extensionsc                       \ rS rSrSrg)	PoolError     N)__name__
__module____qualname____firstlineno____static_attributes__r       ?/var/www/html/env/lib/python3.13/site-packages/psycopg2/pool.pyr   r       s    r   r   c                   H    \ rS rSrSrS rSS jrS rSS jrSS jr	S	 r
S
rg)AbstractConnectionPool$   zGeneric key-based pooling code.c                     [        U5      U l        [        U5      U l        SU l        X0l        X@l        / U l        0 U l        0 U l        SU l	        [        U R                  5       H  nU R                  5         M     g)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__AbstractConnectionPool.__init__'   sc     7|7|



t||$AMMO %r   Nc                     [         R                  " U R                  0 U R                  D6nUb'  X R                  U'   XR
                  [        U5      '   U$ U R                  R                  U5        U$ )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   AbstractConnectionPool._connect=   s_    <t||<?"JJsO$'KK4!  JJd#r   c                 D    U =R                   S-  sl         U R                   $ )zReturn a new unique key.   )r   r   s    r   _getkeyAbstractConnectionPool._getkeyG   s    

a
zzr   c                    U R                   (       a  [        S5      eUc  U R                  5       nXR                  ;   a  U R                  U   $ U R                  (       aB  U R                  R                  5       =U R                  U'   nXR                  [        U5      '   U$ [        U R                  5      U R                  :X  a  [        S5      eU R                  U5      $ )9Get a free connection and assign it to 'key' if not None.connection pool is closedzconnection pool exhausted)r   r   r1   r   r   popr   r(   lenr   r   r*   s      r   _getconnAbstractConnectionPool._getconnL   s    ;;788;,,.C**::c?"::%)ZZ^^%55DJJsOd$'KK4!K4::$,,. ;<<==%%r   c                    U R                   (       a  [        S5      eUc2  U R                  R                  [	        U5      5      nUc  [        S5      e[        U R                  5      U R                  :  a  U(       d  UR                   (       d  UR                  R                  nU[        R                  :X  a  UR                  5         OlU[        R                  :w  a,  UR                  5         U R                  R                  U5        O,U R                  R                  U5        OUR                  5         U R                   (       a  X R                   ;   a$  U R                   U	 U R                  [	        U5      	 gg)zPut away a connection.r5   Nz trying to put unkeyed connection)r   r   r   getr(   r7   r   r   infotransaction_status_extTRANSACTION_STATUS_UNKNOWNcloseTRANSACTION_STATUS_IDLErollbackr)   r   )r   r,   r+   r@   statuss        r   _putconnAbstractConnectionPool._putconn_   s   ;;788;++//"T(+C{ BCCtzz?T\\)% ;;55T<<<JJLt;;;MMOJJ%%d+ JJ%%d+ JJL {{cZZ/

3BtH% 0r   c                     U R                   (       a  [        S5      eU R                  [        U R                  R                  5       5      -    H  n UR                  5         M     SU l         g! [         a     M-  f = f)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.
r5   TN)r   r   r   listr   valuesr@   	Exception)r   r,   s     r   	_closeall AbstractConnectionPool._closeall   sg     ;;788JJdjj&7&7&9!::D

 ;
   s   A--
A;:A;)	r   r   r   r   r   r   r   r   r   N)NF)r   r	   r
   r   __doc__r#   r   r1   r8   rD   rJ   r   r   r   r   r   r   $   s$    ),
&&!&Fr   r   c                   `    \ rS rSrSr\R                  r\R                  r	\R                  rSrg)SimpleConnectionPool   z@A connection pool that can't be shared across different threads.r   N)r   r	   r
   r   rM   r   r8   getconnrD   putconnrJ   closeallr   r   r   r   rO   rO      s&    J$--G$--G%//Hr   rO   c                   8    \ rS rSrSrS rS	S jrS
S jrS rSr	g)ThreadedConnectionPool   z7A connection pool that works with the threading module.c                 l    SSK n[        R                  " XU/UQ70 UD6  UR                  5       U l        g)zInitialize the threading lock.r   N)	threadingr   r#   Lock_lock)r   r   r   r    r!   rX   s         r   r#   ThreadedConnectionPool.__init__   s6    ''7	5%)	5-3	5^^%
r   Nc                     U R                   R                  5          U R                  U5      U R                   R                  5         $ ! U R                   R                  5         f = f)r4   )rZ   acquirer8   release)r   r+   s     r   rQ   ThreadedConnectionPool.getconn   sD    

	!==%JJ DJJ    A A#c                     U R                   R                  5          U R                  XU5        U R                   R                  5         g! U R                   R                  5         f = f)zPut away an unused connection.N)rZ   r]   rD   r^   )r   r,   r+   r@   s       r   rR   ThreadedConnectionPool.putconn   sF    

	!MM$U+JJ DJJ s   A	 	A%c                     U R                   R                  5          U R                  5         U R                   R                  5         g! U R                   R                  5         f = f)z6Close all connections (even the one currently in use.)N)rZ   r]   rJ   r^   r0   s    r   rS   ThreadedConnectionPool.closeall   sB    

	!NNJJ DJJ r`   )rZ   rL   )NNF)
r   r	   r
   r   rM   r#   rQ   rR   rS   r   r   r   r   rU   rU      s    A&!!!r   rU   )	rM   r&   r   r>   Errorr   r   rO   rU   r   r   r   <module>rf      sI   6  '	 	l l^01 0 !3  !r   