o
    ^fu                     @   s   d Z ddlmZ ddlmZ ddlmZmZmZmZm	Z	 ddl
mZ ddlmZ ddlmZmZ e	r8dd	lmZ G d
d dZG dd dZdS )z9
psycopg connection pool base class and functionalities.
    )	monotonicrandom)AnyDictOptionalTupleTYPE_CHECKING)errors   )
PoolClosed)CounterDeque)BaseConnectionc                   @   s  e Zd ZU dZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZed ed< 	dBdedeeeef  dedee dee dedededededefdd Zd!efd"d#Zed!efd$d%Zed!efd&d'Zed!e fd(d)Z!dedee d!e"eef fd*d+Z#dCd-d.Z$dCd/d0Z%dDd3d4Z&d!eeef fd5d6Z'd!eeef fd7d8Z(d!eeef fd9d:Z)e*d;ed<ed=ed!efd>d?Z+dDd@dAZ,d,S )EBasePoolr   pool_minpool_max	pool_sizepool_availablerequests_waitingrequests_numrequests_queuedrequests_wait_msrequests_errorsusage_msreturns_badconnections_numconnections_msconnections_errorsconnections_lostr   _pool conninfokwargsmin_sizemax_sizenametimeoutmax_waitingmax_lifetimemax_idlereconnect_timeoutnum_workersc       
         C   s   |  ||\}}|stjd  }t_d| }|dk rtd|| _|p%i | _|| _|| _|| _|| _	|| _
|
| _|| _|	| _|| _|| _t | _tt  | _|| _d| _d| _d| _d| _d S )Nr   zpool-znum_workers must be at least 1FT)_check_sizer   	_num_pool
ValueErrorr"   r#   r&   	_min_size	_max_sizer'   r(   r+   r)   r*   r,   _nconnsr   r    r   str_stats_nconns_min_growing_opened_closed_open_implicit)selfr"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   num r<   J/home/ertert/spirit/venv/lib/python3.10/site-packages/psycopg_pool/base.py__init__+   s2   


zBasePool.__init__returnc              	   C   s.   d| j j d| j j d| jdt| dd	S )N<. z at 0xx>)	__class__
__module____name__r&   idr:   r<   r<   r=   __repr__d   s   zBasePool.__repr__c                 C      | j S N)r0   rI   r<   r<   r=   r$   j      zBasePool.min_sizec                 C   rK   rL   )r1   rI   r<   r<   r=   r%   n   rM   zBasePool.max_sizec                 C   rK   )z`!True` if the pool is closed.)r8   rI   r<   r<   r=   closedr   s   zBasePool.closedc                 C   sX   |d u r|}|dk rt d||k rt d||  kr#dkr(t d ||fS ||fS )Nr   zmin_size cannot be negativez/max_size must be greater or equal than min_sizez3if min_size is 0 max_size must be greater or than 0)r/   )r:   r$   r%   r<   r<   r=   r-   w   s   zBasePool._check_sizeNc                 C   s   | j r| jrtdd S d S )Nz8pool has already been opened/closed and cannot be reused)r8   r7   eOperationalErrorrI   r<   r<   r=   _check_open   s
   zBasePool._check_openc                 C   s4   | j r| jrtd| jdtd| jdd S )Nz	the pool z is already closedz is not open yet)r8   r7   r   r&   rI   r<   r<   r=   _check_open_getconn   s
   zBasePool._check_open_getconnconnBaseConnection[Any]c                 C   sJ   t |dd }|| u rd S |rd|j}nd}td| jd| d| )Nr    zit comes from pool zit doesn't come from any poolz can't return connection to pool z, z: )getattrr&   r/   )r:   rS   poolmsgr<   r<   r=   _check_pool_putconn   s   zBasePool._check_pool_putconnc                 C   s   t | j}||   |S )z<
        Return current stats about the pool usage.
        )dictr4   update_get_measures)r:   rvr<   r<   r=   	get_stats   s   
zBasePool.get_statsc                 C   s*   | j t }| _ t|}||   |S )zy
        Return current stats about the pool usage.

        After the call, all the counters are reset to zero.
        )r4   r   rY   rZ   r[   )r:   statsr\   r<   r<   r=   	pop_stats   s   zBasePool.pop_statsc              	   C   s(   | j | j| j| j| j| j| jt| jiS )zG
        Return immediate measures of the pool (not counters).
        )		_POOL_MINr0   	_POOL_MAXr1   
_POOL_SIZEr2   _POOL_AVAILABLElenr    rI   r<   r<   r=   r[      s
   zBasePool._get_measuresvaluemin_pcmax_pcc                 C   s   |d|| t    |  S )zV
        Add a random value to *value* between *min_pc* and *max_pc* percent.
              ?r   )clsre   rf   rg   r<   r<   r=   _jitter   s   zBasePool._jitterc                 C   s   t  | | jdd |_dS )zeSet an expiry date on a connection.

        Add some randomness to avoid mass reconnection.
        g        N)r   rj   r)   
_expire_at)r:   rS   r<   r<   r=   _set_connection_expiry_date   s   z$BasePool._set_connection_expiry_date)r!   )r?   N)rS   rT   r?   N)-rG   rF   __qualname__r.   r`   ra   rb   rc   _REQUESTS_WAITING_REQUESTS_NUM_REQUESTS_QUEUED_REQUESTS_WAIT_MS_REQUESTS_ERRORS	_USAGE_MS_RETURNS_BAD_CONNECTIONS_NUM_CONNECTIONS_MS_CONNECTIONS_ERRORS_CONNECTIONS_LOSTr   __annotations__r3   r   r   r   intfloatr>   rJ   propertyr$   r%   boolrN   r   r-   rQ   rR   rX   r]   r_   r[   classmethodrj   rm   r<   r<   r<   r=   r      sv   
 	

9"


r   c                   @   sN   e Zd ZdZdZdZdZdefddZded	d
fddZ	ded	e
fddZd
S )AttemptWithBackoffzR
    Keep the state of a repeated operation attempt with exponential backoff.
    rh   g?g       @r'   c                C   s   || _ d| _d| _d S )Nrk   )r'   delay
give_up_at)r:   r'   r<   r<   r=   r>      s   
zAttemptWithBackoff.__init__nowr?   Nc                 C   sj   | j dkr|| j | _t| j| j | j| _ n|  j | j9  _ | j | | jkr3td| j| | _ dS dS )z7Calculate how long to wait for a new connection attemptrk   N)	r   r'   r   r   rj   INITIAL_DELAYDELAY_JITTERDELAY_BACKOFFmaxr:   r   r<   r<   r=   update_delay   s   
zAttemptWithBackoff.update_delayc                 C   s   | j dko	|| j kS )z8Return True if we are tired of trying this attempt. Meh.rk   )r   r   r<   r<   r=   time_to_give_up   s   z"AttemptWithBackoff.time_to_give_up)rG   rF   rn   __doc__r   r   r   r|   r>   r   r~   r   r<   r<   r<   r=   r      s    r   N)r   timer   r   typingr   r   r   r   r	   psycopgr
   rO   r   _compatr   r   psycopg._connection_baser   r   r   r<   r<   r<   r=   <module>   s     8