
    tUi$                     j   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dlm
Z
  ej                  d      Zd Z G d d	e      Z ed
d      Z G d de      Zd Z G d de      Z G d de      Z G d dee      Z G d de      Z G d dee	      Z G d de      Z G d dee
      Zy)    N)
namedtuple)MySQLDatabase)PostgresqlDatabase)SqliteDatabasezpeewee.poolc                 L    | !t        | t        t        f      st        |       S | S N)
isinstanceintfloat)vals    A/home/agent/.local/lib/python3.12/site-packages/playhouse/pool.pymake_intr      s"    
z#U|<3xJ    c                       e Zd Zy)MaxConnectionsExceededN__name__
__module____qualname__ r   r   r   r      s    r   r   PoolConnection)	timestamp
connectionchecked_outc                       e Zd Zd Zy)	_sentinelc                      yNTr   )selfothers     r   __lt__z_sentinel.__lt__   s    r   N)r   r   r   r!   r   r   r   r   r      s    r   r   c                 B     t        j                          fd       }|S )Nc                 `    | j                   5   | g|i |cd d d        S # 1 sw Y   y xY wr   )
_pool_lock)r   argskwargsfns      r   innerzlocked.<locals>.inner!   s1    __ 	-d,T,V,	- 	- 	-s   $-)	functoolswraps)r'   r(   s   ` r   lockedr+       s%    __R- - Lr   c                        e Zd Z	 	 d fd	Z	 	 d fd	Zd fd	Ze fd       Zd Zd Z	d Z
 fdZedd	       Zed
        Zed        Zedd       Zed        Z xZS )PooledDatabasec                    t        |      | _        t        |      | _        t        |      | _        | j                  dk(  rt	        d      | _        t        j                         | _        t        j                  | j                        | _	        g | _
        d| _        i | _        t        | _        t        t         | F  |fi | y Nr   inf)r   _max_connections_stale_timeout_wait_timeoutr   	threadingRLockr$   	Condition_pool_available_connections_heap_counter_in_useidconn_keysuperr-   __init__)r   databasemax_connectionsstale_timeouttimeoutr&   	__class__s         r   r>   zPooledDatabase.__init__)   s     ( 9&}5%g."!&uD $//+(224??C  
  nd,X@@r   c                     t        t        | 
  |fi | |t        |      | _        |t        |      | _        |1t        |      | _        | j                  dk(  rt        d      | _        y y y r/   )r=   r-   initr   r1   r2   r3   r   )r   r?   r@   rA   rB   connect_kwargsrC   s         r   rE   zPooledDatabase.initJ   st    nd(D^D&$,_$=D!$"*="9D!)'!2D!!Q&%*5\" ' r   c                    | j                   st        t        |   |      S t	        j
                         | j                   z   }	 	 t        t        |   |      S # t        $ rr |t	        j
                         z
  }|dk  rt        d      | j                  5  | j                  j                  t        |d             d d d        n# 1 sw Y   nxY wY nw xY w)Nr   z:Max connections exceeded, timed out attempting to connect.g      ?)rB   )
r3   r=   r-   connecttime	monotonicr   r7   waitmin)r   reuse_if_opendeadline	remainingrC   s       r   rH   zPooledDatabase.connectV   s    !!6}EE>>#d&8&88	K^T:=II) K$t~~'77	>0#$ $ )) K((--c)S6I-JK K KK s*   A <C'C:	CC	CCc                 B   | j                   r	 t        j                  | j                         \  }}}| j	                  |      }| j                  |      rt        j                  d|       i| j                  r9| j                  |      r(t        j                  d|       | j                  |       t        ||t        j                               | j                  |<   |S | j                  r-t        | j                        | j                  k\  rt!        d      t"        t$        | O         }t        j                         }| j	                  |      }t        j                  d|       t        ||t        j                               | j                  |<   |S # t        $ r Y w xY w)Nz%Connection %s was closed, discarding.z!Connection %s was stale, closing.zExceeded maximum connections.zCreated new connection %s.)r8   heapqheappop
IndexErrorr<   
_is_closedloggerdebugr2   	_is_stale
_close_rawr   rI   r:   r1   lenr   r=   r-   _connect)r   ts_counterconnkeyrC   s        r   rZ   zPooledDatabase._connectg   sI   %*]]43D3D%E"Hd --%Ct$DcJ""t~~b'9@#F% !/r4 EDLLK  DLL!T%:%::()HII^T35YY[mmD!137*2tTYY[AS7  s   #F 	FFc                 J    t        j                          |z
  | j                  kD  S r   )rI   r2   )r   r   s     r   rW   zPooledDatabase._is_stale   s      		i'4+>+>>>r   c                      y)NFr   r   r]   s     r   rT   zPooledDatabase._is_closed   s    r   c                      yr   r   ra   s     r   
_can_reusezPooledDatabase._can_reuse   s    r   c                     	 t         t        |   |       y # t        $ r* t        j                  d| j                  |      d       Y y w xY w)NzError closing connection %s.T)exc_info)r=   r-   _close	ExceptionrU   rV   r<   )r   r]   rC   s     r   rX   zPooledDatabase._close_raw   sH    	(.$.t4 	(LL7t9L"&  (	(s    0A
	A
c                    | j                  |      }|r.| j                  j                  |d        | j                  |       y || j                  vrt        j                  d|       y | j                  j                  |      }| j                  rC| j                  |j                        r(t        j                  d|       | j                  |       n| j                  |      s(t        j                  d|       | j                  |       nbt        j                  d|       | xj                  dz  c_
        t        j                  | j                  |j                  | j                  |f       | j                  j                          y )Nz)Connection %s not in use, ignoring close.z(Closing stale connection %s on check-in.z$Connection %s not reusable, closing.zReturning %s to pool.   )r<   r:   poprX   rU   rV   r2   rW   r   rc   r9   rQ   heappushr8   r7   notify)r   r]   
close_connr^   	pool_conns        r   rf   zPooledDatabase._close   s%    mmD!LLS$'OOD!dll"LLDcJLL$$S)	4>>)2E2E#FLLCSIOOD!&LL?EOOD!LL0#6!#NN4,,%//1C1CTJL 	##%r   c                     | j                         ry| j                         }| j                  |      }| j                  j	                  |d       | j                          | j                  |       y)zS
        Close the underlying connection without returning it to the pool.
        FN)	is_closedr   r<   r:   rj   closerX   )r   r]   r^   s      r   manual_closezPooledDatabase.manual_close   sX    
 >>  mmD! 	d#

r   c                 b    | j                   }g | _         |D ]  \  }}}| j                  |        y r   )r8   rX   )r   idle_r]   s       r   
close_idlezPooledDatabase.close_idle   s8        	"JAq$OOD!	"r   c                 <   t        j                          |z
  }d}t        | j                  j                               D ]B  \  }}|j                  |k  s| j                  |j                         | j                  |= |dz  }D | j                  j                          |S )Nr   ri   )	rI   listr:   itemsr   rX   r   r7   
notify_all)r   agecutoffnr^   rn   s         r   close_stalezPooledDatabase.close_stale   s    
 s""4<<#5#5#78 	NC$$v-	 4 45LL%Q		 	'')r   c                     | j                          | j                          | j                  i c}| _        |j                         D ]  }| j	                  |j
                          | j                  j                          y r   )rq   rv   r:   valuesrX   r   r7   rz   )r   in_usern   s      r   	close_allzPooledDatabase.close_all   sd     	

#||R 	2IOOI001	2 	'')r   )   NN)NNNF)iX  )r   r   r   r>   rE   rH   r+   rZ   rW   rT   rc   rX   rf   rr   rv   r~   r   __classcell__)rC   s   @r   r-   r-   (   s    CGAB BF
2K"    D?
( & &<  " " "   	* 	*r   r-   c                       e Zd Zd Zy)_PooledMySQLDatabasec                 `    | j                   d   dk(  rd}nd}	  |j                  |  y#  Y yxY w)Nr      r   r   TF)server_versionping)r   r]   r%   s      r   rT   z_PooledMySQLDatabase._is_closed   sA    q!Q&DD	DIIt 	s   ) -Nr   r   r   rT   r   r   r   r   r      s    	r   r   c                       e Zd Zy)PooledMySQLDatabaseNr   r   r   r   r   r          r   r   c                       e Zd Zd Zd Zy)_PooledPostgresqlDatabasec                 R    |j                   ry| j                  j                  |      S r   )closed_adapteris_connection_closedra   s     r   rT   z$_PooledPostgresqlDatabase._is_closed  s!    ;;}}11$77r   c                 8    | j                   j                  |      S r   )r   is_connection_reusablera   s     r   rc   z$_PooledPostgresqlDatabase._can_reuse	  s    }}33D99r   N)r   r   r   rT   rc   r   r   r   r   r     s    8
:r   r   c                       e Zd Zy)PooledPostgresqlDatabaseNr   r   r   r   r   r     r   r   r   c                       e Zd Zd Zy)_PooledSqliteDatabasec                 ,    	 |j                    y#  Y yxY w)NTF)total_changesra   s     r   rT   z _PooledSqliteDatabase._is_closed  s    	 	s    Nr   r   r   r   r   r     s    r   r   c                       e Zd Zy)PooledSqliteDatabaseNr   r   r   r   r   r     r   r   r   )r)   rQ   loggingr4   rI   collectionsr   peeweer   r   r   	getLoggerrU   r   
ValueErrorr   r   objectr   r+   r-   r   r   r   r   r   r   r   r   r   <module>r      s         "   % !			=	) /Z . , /> ? 
H*V H*V
> 
	. 	: :	8:L 	N 	0. 	r   