
    iu                         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	rdd	lmZ  G d
 d      Z G d d      Zy)z9
psycopg connection pool base class and functionalities.
    )	monotonicrandom)AnyDictOptionalTupleTYPE_CHECKING)errors   )
PoolClosed)CounterDeque)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ed   ed<   	 d1d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Zdefd Zedefd!       Zedefd"       Zede fd#       Z!dedee   de"eef   fd$Z#d2d&Z$d2d'Z%d3d(Z&deeef   fd)Z'deeef   fd*Z(deeef   fd+Z)e*d,ed-ed.edefd/       Z+d3d0Z,y%)4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       
            | j                  ||      \  }}|s#t        j                  dz   x}t        _        d| }|dk  rt        d      || _        |xs i | _        || _        || _        || _        || _	        || _
        |
| _        || _        |	| _        || _        || _        t!               | _        t%        t&                  | _        || _        d| _        d| _        d| _        d| _        y )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-   nums                q/var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/psycopg_pool/base.py__init__zBasePool.__init__+   s     "--hA('/'9'9A'==C($3%=D?=>> &,l	!!&!2( &W
cln $ #    returnc           
          d| j                   j                   d| j                   j                   d| j                  dt	        |       dd	S )N<. z at 0xx>)	__class__
__module____name__r'   idr<   s    r>   __repr__zBasePool.__repr__d   sH    ))*!DNN,C,C+D		}F2d8A,a1	
r@   c                     | j                   S N)r2   rL   s    r>   r%   zBasePool.min_sizej       ~~r@   c                     | j                   S rO   )r3   rL   s    r>   r&   zBasePool.max_sizen   rP   r@   c                     | j                   S )z`!True` if the pool is closed.)r:   rL   s    r>   closedzBasePool.closedr   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)r1   )r<   r%   r&   s      r>   r/   zBasePool._check_sizew   si    Ha<:;;hNOOx$1$RSS % !!x!!r@   Nc                 `    | j                   r"| j                  rt        j                  d      y y )Nz8pool has already been opened/closed and cannot be reused)r:   r9   eOperationalErrorrL   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)r:   r9   r   r'   rL   s    r>   _check_open_getconnzBasePool._check_open_getconn   sG    <<|| 9TYYM9K!LMM 9TYYM9I!JKK	 r@   c                     t        |dd       }|| 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'   r1   )r<   connpoolmsgs       r>   _check_pool_putconnzBasePool._check_pool_putconn   sY    tWd+4<'		}5C1C.tyym2cU"TFK
 	
r@   c                 n    t        | j                        }|j                  | j                                |S )z<
        Return current stats about the pool usage.
        )dictr6   update_get_measures)r<   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.
        )r6   r   rb   rc   rd   )r<   statsre   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_MINr2   	_POOL_MAXr3   
_POOL_SIZEr4   _POOL_AVAILABLElenr"   rL   s    r>   rd   zBasePool._get_measures   sH    
 NNDNNNNDNNOOT\\  #djj/	
 	
r@   valuemin_pcmax_pcc                 4    |d||z
  t               z  z   |z   z  S )zV
        Add a random value to *value* between *min_pc* and *max_pc* percent.
              ?r   )clsrp   rq   rr   s       r>   _jitterzBasePool._jitter   s$    
 68;<vEFFr@   c                 ^    t               | j                  | j                  dd      z   |_        y)zeSet an expiry date on a connection.

        Add some randomness to avoid mass reconnection.
        g        N)r   rv   r*   
_expire_at)r<   r]   s     r>   _set_connection_expiry_datez$BasePool._set_connection_expiry_date   s%    
 $+T5F5Fs(SSr@   ) )rA   N)r]   zBaseConnection[Any]rA   N)-rJ   rI   __qualname__r0   rk   rl   rm   rn   _REQUESTS_WAITING_REQUESTS_NUM_REQUESTS_QUEUED_REQUESTS_WAIT_MS_REQUESTS_ERRORS	_USAGE_MS_RETURNS_BAD_CONNECTIONS_NUM_CONNECTIONS_MS_CONNECTIONS_ERRORS_CONNECTIONS_LOSTr   __annotations__r5   r   r   r   intfloatr?   rM   propertyr%   r&   boolrS   r	   r/   rX   rZ   r`   rf   ri   rd   classmethodrv   rz    r@   r>   r   r      s   I IIJ&O*"M(*(I L(&O.*< 7$7$ c3h(	7$
 7$ 3-7$ sm7$ 7$ 7$ 7$ 7$ !7$ 7$r
# 
 #   #     "C "8C= "U3PS8_ "L
4S> 	4S> 		
tCH~ 	
 GE G5 G% GE G GTr@   r   c                   H    e Zd ZdZdZdZdZdefdZdedd	fd
Z	dede
fdZy	)AttemptWithBackoffzR
    Keep the state of a repeated operation attempt with exponential backoff.
    rt   g?g       @r(   c                .    || _         d| _        d| _        y )Nrx   )r(   delay
give_up_at)r<   r(   s     r>   r?   zAttemptWithBackoff.__init__   s    
r@   nowrA   Nc                 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 attemptrx   N)	r   r(   r   r   rv   INITIAL_DELAYDELAY_JITTERDELAY_BACKOFFmaxr<   r   s     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.rx   )r   r   s     r>   time_to_give_upz"AttemptWithBackoff.time_to_give_up   s    $?)??r@   )rJ   rI   r|   __doc__r   r   r   r   r?   r   r   r   r   r@   r>   r   r      sM     MLM5 
9 9$ 9@5 @T @r@   r   N)r   timer   r   typingr   r   r   r	   r
   psycopgr   rV   r   _compatr   r   psycopg._connection_baser   r   r   r   r@   r>   <module>r      sB      < <   #7tT tTn@ @r@   