o
    ƒ¡g¬  ã                   @   s\   d Z ddlZddlmZ G dd„ dejƒZG dd„ dƒZG dd	„ d	eƒZG d
d„ deƒZdS )z`Connection pooling for psycopg2

This module implements thread-safe (and not) connection pools.
é    N)Ú
extensionsc                   @   s   e Zd ZdS )Ú	PoolErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   úb/var/www/primordialfields.com/dashboard_backend/venv/lib/python3.10/site-packages/psycopg2/pool.pyr       s    r   c                   @   sF   e Zd ZdZdd„ Zddd„Zdd„ Zdd	d
„Zddd„Zdd„ Z	dS )ÚAbstractConnectionPoolzGeneric key-based pooling code.c                 O   sZ   t |ƒ| _t |ƒ| _d| _|| _|| _g | _i | _i | _d| _	t
| jƒD ]}|  ¡  q$dS )zèInitialize 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Úir   r   r   Ú__init__'   s   


ÿzAbstractConnectionPool.__init__Nc                 C   sH   t j| ji | j¤Ž}|dur|| j|< || jt|ƒ< |S | j |¡ |S )z;Create a new connection and assign it to 'key' if not None.N)	Úpsycopg2Úconnectr   r   r   r   Úidr   Úappend©r   ÚkeyÚconnr   r   r   r   =   s   
ÿzAbstractConnectionPool._connectc                 C   s   |  j d7  _ | j S )zReturn a new unique key.é   )r   ©r   r   r   r   Ú_getkeyG   s   zAbstractConnectionPool._getkeyc                 C   s€   | j rtdƒ‚|du r|  ¡ }|| jv r| j| S | jr/| j ¡  | j|< }|| jt|ƒ< |S t| jƒ| j	kr;tdƒ‚|  
|¡S )ú9Get a free connection and assign it to 'key' if not None.úconnection pool is closedNzconnection pool exhausted)r   r   r$   r   r   Úpopr   r   Úlenr   r   r   r   r   r   Ú_getconnL   s   


zAbstractConnectionPool._getconnFc                 C   sÎ   | j rtdƒ‚|du r| j t|ƒ¡}|du rtdƒ‚t| jƒ| jk rM|sM|j sL|jj	}|t
jkr6| ¡  n|t
jkrF| ¡  | j |¡ n| j |¡ n| ¡  | j rY|| jv re| j|= | jt|ƒ= dS dS )zPut away a connection.r&   Nz trying to put unkeyed connection)r   r   r   Úgetr   r(   r   r   ÚinfoÚtransaction_statusÚ_extÚTRANSACTION_STATUS_UNKNOWNÚcloseÚTRANSACTION_STATUS_IDLEÚrollbackr   r   )r   r!   r    r/   Ústatusr   r   r   Ú_putconn_   s*   


€þzAbstractConnectionPool._putconnc              	   C   sP   | j rtdƒ‚| jt| j ¡ ƒ D ]}z| ¡  W q ty"   Y qw d| _ dS )z×Close 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   Úvaluesr/   Ú	Exception)r   r!   r   r   r   Ú	_closeall‚   s   ÿ
z AbstractConnectionPool._closeall©N)NF)
r   r   r   Ú__doc__r   r   r$   r)   r3   r7   r   r   r   r   r	   $   s    



#r	   c                   @   s"   e Zd ZdZejZejZej	Z
dS )ÚSimpleConnectionPoolz@A connection pool that can't be shared across different threads.N)r   r   r   r9   r	   r)   Úgetconnr3   Úputconnr7   Úcloseallr   r   r   r   r:   “   s
    
r:   c                   @   s4   e Zd ZdZdd„ Zddd„Zddd	„Zd
d„ ZdS )ÚThreadedConnectionPoolz7A connection pool that works with the threading module.c                 O   s2   ddl }tj| ||g|¢R i |¤Ž | ¡ | _dS )zInitialize the threading lock.r   N)Ú	threadingr	   r   ÚLockÚ_lock)r   r   r   r   r   r?   r   r   r   r   ž   s   ÿÿÿzThreadedConnectionPool.__init__Nc                 C   s.   | j  ¡  z|  |¡W | j  ¡  S | j  ¡  w )r%   )rA   Úacquirer)   Úrelease)r   r    r   r   r   r;   ¥   s   

zThreadedConnectionPool.getconnFc                 C   s6   | j  ¡  z|  |||¡ W | j  ¡  dS | j  ¡  w )zPut away an unused connection.N)rA   rB   r3   rC   )r   r!   r    r/   r   r   r   r<   ­   s   
zThreadedConnectionPool.putconnc                 C   s0   | j  ¡  z|  ¡  W | j  ¡  dS | j  ¡  w )z6Close all connections (even the one currently in use.)N)rA   rB   r7   rC   r#   r   r   r   r=   µ   s   

zThreadedConnectionPool.closeallr8   )NNF)r   r   r   r9   r   r;   r<   r=   r   r   r   r   r>   ›   s    

r>   )	r9   r   r   r-   ÚErrorr   r	   r:   r>   r   r   r   r   Ú<module>   s    o