
    3fiZ                        d Z ddlmZ ddlmZ ddlmZ ddlmZmZ ddl	m
Z
mZ ddlmZ dd	lmZ erdd
lmZ  G d d      Z G d d      Zy)z9
psycopg connection pool base class and functionalities.
    )annotations)	monotonicrandom)TYPE_CHECKINGAny)Counterdeque)errors   )
PoolClosed)BaseConnectionc                     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ded<   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d"dZd#dZed$d       Zed$d       Zed%d       Zd&dZd'dZd'dZd(dZd)dZd)dZd)dZ e!d*d       Z"d(d Z#y!)+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_lostz
deque[Any]_poolc       
           | j                  ||      \  }}|s#t        j                  dz   x}t        _        d| }|
dk  rt        d      || _        || _        || _        || _        || _        || _	        |	| _
        || _        || _        |
| _        || _        t               | _        t#        t$                  | _        d| _        || _        d| _        d| _        d| _        d| _        y )Nr   zpool-znum_workers must be at least 1        FT)_check_sizer   	_num_pool
ValueErrornameclose_returns	_min_size	_max_sizetimeoutmax_waitingreconnect_timeoutmax_lifetimemax_idlenum_workers_nconnsr
   r    r	   str_stats_drained_at_nconns_min_growing_opened_closed_open_implicit)selfmin_sizemax_sizer&   r'   r*   r+   r-   r.   r,   r/   nums               O/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/psycopg_pool/base.py__init__zBasePool.__init__-   s     "--hA('/'9'9A'==C($3%=D?=>>	*!!&!2( &W
cln $ #    c           
         d| j                   j                   d| j                   j                   d| j                  dt	        |       dd	S )N<. z at 0xx>)	__class__
__module____name__r&   idr9   s    r=   __repr__zBasePool.__repr__e   sH    ))*!DNN,C,C+D		}F2d8A,a1	
r?   c                    | j                   S N)r(   rJ   s    r=   r:   zBasePool.min_sizek       ~~r?   c                    | j                   S rM   )r)   rJ   s    r=   r;   zBasePool.max_sizeo   rN   r?   c                    | j                   S )z`!True` if the pool is closed.)r7   rJ   s    r=   closedzBasePool.closeds   s     ||r?   c                    ||}|dk  rt        d      ||k  rt        d      ||cx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%   )r9   r:   r;   s      r=   r#   zBasePool._check_sizex   si    Ha<:;;hNOOx$1$RSS % !!x!!r?   c                `    | j                   r"| j                  rt        j                  d      y y )Nz8pool has already been opened/closed and cannot be reused)r7   r6   eOperationalErrorrJ   s    r=   _check_openzBasePool._check_open   s,    <<DLL$$J  )<r?   c                    | j                   r>| j                  rt        d| j                  d      t        d| j                  d      y )Nz	the pool z is already closedz is not open yet)r7   r6   r   r&   rJ   s    r=   _check_open_getconnzBasePool._check_open_getconn   sG    <<|| 9TYYM9K!LMM 9TYYM9I!JKK	 r?   c                    t        |dd       x}| u ry |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%   )r9   connpoolmsgs       r=   _check_pool_putconnzBasePool._check_pool_putconn   sZ    D'400DT9'		}5C1C.tyym2cU"TFK
 	
r?   c                n    t        | j                        }|j                  | j                                |S )z<
        Return current stats about the pool usage.
        )dictr2   update_get_measures)r9   rvs     r=   	get_statszBasePool.get_stats   s,     $++
		$$$&'	r?   c                    | j                   t               c}| _         t        |      }|j                  | j	                                |S )zy
        Return current stats about the pool usage.

        After the call, all the counters are reset to zero.
        )r2   r	   r`   ra   rb   )r9   statsrc   s      r=   	pop_statszBasePool.pop_stats   s;     "[[')t{%[
		$$$&'	r?   c           
         | j                   | j                  | j                  | j                  | j                  | j
                  | j                  t        | j                        iS )zG
        Return immediate measures of the pool (not counters).
        )		_POOL_MINr(   	_POOL_MAXr)   
_POOL_SIZEr0   _POOL_AVAILABLElenr    rJ   s    r=   rb   zBasePool._get_measures   sH    
 NNDNNNNDNNOOT\\  #djj/	
 	
r?   c                4    |d||z
  t               z  z   |z   z  S )zV
        Add a random value to *value* between *min_pc* and *max_pc* percent.
              ?r   )clsvaluemin_pcmax_pcs       r=   _jitterzBasePool._jitter   s$    
 68;<vEFFr?   c                p    t               x|_        }|| j                  | j                  dd      z   |_        y)zeSet an expiry date on a connection.

        Add some randomness to avoid mass reconnection.
        gr"   N)r   _created_atrt   r-   
_expire_at)r9   r[   ts      r=   _set_connection_expiry_datez$BasePool._set_connection_expiry_date   s2    
  ){*1dll4+<+<eSIIr?   N)r:   intr;   
int | Noner&   z
str | Noner'   boolr*   floatr+   rz   r-   r}   r.   r}   r,   r}   r/   rz   )returnr1   )r~   rz   )r~   r|   )r:   rz   r;   r{   r~   ztuple[int, int])r~   None)r[   zBaseConnection[Any]r~   r   )r~   zdict[str, int])rq   r}   rr   r}   rs   r}   r~   r}   )$rH   rG   __qualname__r$   ri   rj   rk   rl   _REQUESTS_WAITING_REQUESTS_NUM_REQUESTS_QUEUED_REQUESTS_WAIT_MS_REQUESTS_ERRORS	_USAGE_MS_RETURNS_BAD_CONNECTIONS_NUM_CONNECTIONS_MS_CONNECTIONS_ERRORS_CONNECTIONS_LOST__annotations__r>   rK   propertyr:   r;   rQ   r#   rV   rX   r^   rd   rg   rb   classmethodrt   ry    r?   r=   r   r      sL   I IIJ&O*"M(*(I L(&O.*6$ 6$ 	6$
 6$ 6$ 6$ 6$ 6$ 6$ !6$ 6$p
      "L

		
 G GJr?   r   c                  4    e Zd ZdZdZdZdZd	dZd
dZddZ	y)AttemptWithBackoffzR
    Keep the state of a repeated operation attempt with exponential backoff.
    ro   g?g       @c               .    || _         d| _        d| _        y )Nr"   )r*   delay
give_up_at)r9   r*   s     r=   r>   zAttemptWithBackoff.__init__   s    
r?   c                v   | j                   dk(  rP|| j                  z   | _        t        j	                  | j
                  | j                   | j                        | _         n| xj                   | j                  z  c_         | j                   |z   | j                  kD  rt        d| j                  |z
        | _         yy)z7Calculate how long to wait for a new connection attemptr"   N)	r   r*   r   r   rt   INITIAL_DELAYDELAY_JITTERDELAY_BACKOFFmaxr9   nows     r=   update_delayzAttemptWithBackoff.update_delay   s    ::!DLL0DO!))""T%6%6$68I8IDJ JJ$,,,J::doo-S$//C"78DJ .r?   c                B    | j                   dkD  xr || j                   k\  S )z8Return True if we are tired of trying this attempt. Meh.r"   )r   r   s     r=   time_to_give_upz"AttemptWithBackoff.time_to_give_up   s    $?)??r?   N)r*   r}   )r   r}   r~   r   )r   r}   r~   r|   )
rH   rG   r   __doc__r   r   r   r>   r   r   r   r?   r=   r   r      s(     MLM
9@r?   r   N)r   
__future__r   timer   r   typingr   r   collectionsr	   r
   psycopgr   rT   r   psycopg._connection_baser   r   r   r   r?   r=   <module>r      sB    #   % &  7sJ sJl@ @r?   