
    B>qj)                     T   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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    M/opt/rentech/trading_bot/.venv/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                 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)self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d
 Zed        Zedd       Z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   r8   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)   )r7   r'   initr   r+   r,   r-   r   )r   r9   r:   r;   r<   connect_kwargsr=   s         r   r?   zPooledDatabase.initE   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      ?)r<   )
r-   r7   r'   connecttime	monotonicr   r1   waitmin)r   reuse_if_opendeadline	remainingr=   s       r   rB   zPooledDatabase.connectQ   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                 "   | j                   rt        j                  | j                         \  }}}| j                  |      }| j	                  |      rt
        j                  d|       h| j                  r9| j                  |      r(t
        j                  d|       | j                  |       t        ||t        j                               | j                  |<   |S | j                  r-t        | j                        | j                  k\  rt        d      t         t"        | K         }t        j                         }| j                  |      }t
        j                  d|       t        ||t        j                               | j                  |<   |S )Nz%Connection %s was closed, discarding.z!Connection %s was stale, closing.zExceeded maximum connections.zCreated new connection %s.)r2   heapqheappopr6   
_is_closedloggerdebugr,   	_is_stale
_close_rawr   rC   r4   r+   lenr   r7   r'   _connect)r   ts_counterconnkeyr=   s        r   rS   zPooledDatabase._connectb   s.   !&t/@/@!AB$--%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r   c                 J    t        j                          |z
  | j                  kD  S r   )rC   r,   )r   r   s     r   rP   zPooledDatabase._is_stale   s      		i'4+>+>>>r   c                      y)NFr   r   rV   s     r   rM   zPooledDatabase._is_closed   s    r   c                      yNTr   rZ   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)r7   r'   _close	ExceptionrN   rO   r6   )r   rV   r=   s     r   rQ   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.   )r6   r4   poprQ   rN   rO   r,   rP   r   r]   r3   rK   heappushr2   r1   notify)r   rV   
close_connrW   	pool_conns        r   r`   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                  5  | j                  j                  |d       ddd       | j                          | j                  |       y# 1 sw Y   +xY w)zS
        Close the underlying connection without returning it to the pool.
        FN)	is_closedr   r6   r   r4   rd   closerQ   )r   rV   rW   s      r   manual_closezPooledDatabase.manual_close   sv     >>  mmD!
 __ 	(LLS$'	( 	

		( 	(s   BBc                 b    | j                   }g | _         |D ]  \  }}}| j                  |        y r   )r2   rQ   )r   idle_rV   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   rc   )	rC   listr4   itemsr   rQ   r   r1   
notify_all)r   agecutoffnrW   rh   s         r   close_stalezPooledDatabase.close_stale   s    
 s""4<<#5#5#78 	NC$$v-	 4 45LL%Q		 	'')r   c                 B   | j                          | j                  5  | j                          | j                  i c}| _        |j	                         D ]  }| j                  |j                          | j                  j                          d d d        y # 1 sw Y   y xY wr   )	rk   r   rp   r4   valuesrQ   r   r1   rt   )r   in_userh   s      r   	close_allzPooledDatabase.close_all   s     	

__ 	.OO#'<< FDL#]]_ 6		 4 456   ++-	. 	. 	.s   A/BB)   NN)NNN)F)iX  )r   r   r   r8   r?   rB   r%   rS   rP   rM   r]   rQ   r`   rl   rp   rx   r|   __classcell__)r=   s   @r   r'   r'   #   s    CGAB BF
2K"  <?
( & &<( " "  .r   r'   c                       e Zd Zd Zd Zy)_PooledMySQLDatabasec                     	 |j                  d       y# t        $ r$ 	 |j                          Y y# t        $ r Y Y yw xY wt        $ r Y yw xY wNFT)ping	TypeErrorra   rZ   s     r   rM   z_PooledMySQLDatabase._is_closed   s[    
	IIe   			
 	   		s%    	A0	=A=A
Ac                     	 t        |dd       }| |       }nt        |dd      }|r|j                          y# t        $ r Y yw xY w)Nget_transaction_statusin_transactionTF)getattrrollbackra   )r   rV   statusdirtys       r   r]   z_PooledMySQLDatabase._can_reuse  sW    		T#;TBF!&6=   		s   69 	AANr   r   r   rM   r]   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_closedrZ   s     r   rM   z$_PooledPostgresqlDatabase._is_closed  s!    ;;}}11$77r   c                 8    | j                   j                  |      S r   )r   is_connection_reusablerZ   s     r   r]   z$_PooledPostgresqlDatabase._can_reuse  s    }}33D99r   Nr   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d Zy)_PooledSqliteDatabasec                 ,    	 |j                    y#  Y yxY w)NTF)total_changesrZ   s     r   rM   z _PooledSqliteDatabase._is_closed!  s    	 	s    c                 \    	 |j                   r|j                          y# t        $ r Y yw xY wr   )r   r   ra   rZ   s     r   r]   z _PooledSqliteDatabase._can_reuse(  s2    	""   		s    	++Nr   r   r   r   r   r      s    r   r   c                       e Zd Zy)PooledSqliteDatabaseNr   r   r   r   r   r   1  r   r   r   )r#   rK   loggingr.   rC   collectionsr   peeweer   r   r   	getLoggerrN   r   
ValueErrorr   r   r%   objectr'   r   r   r   r   r   r   r   r   r   <module>r      s         "   % !			=	) /Z . , /> ?J.V J.Z> >	. 	: :	8:L 	N "	0. 	r   