AlkantarClanX12

Your IP : 3.144.89.42


Current Path : /opt/imunify360/venv/lib/python3.11/site-packages/playhouse/__pycache__/
Upload File :
Current File : //opt/imunify360/venv/lib/python3.11/site-packages/playhouse/__pycache__/pool.cpython-311.pyc

�

m��f�,���dZddlZddlZddlZddlZddlmZddlmZ	ddl	m
Z
ddl	mZddl	mZn#e
$r	dxZ
xZZYnwxYwddlmZdd	lmZdd
lmZejd��Zd�ZGd
�de��Zedd��ZGd�de��ZGd�dee��ZGd�de��ZGd�dee��Z	ddlmZGd�dee��Zn
#e
$rdZYnwxYwGd�de��Z Gd�de e��Z!	dd l"m#Z#Gd!�d"e e#��Z$n
#e
$rdZ$YnwxYw	dd#l"m%Z%Gd$�d%e e%��Z&dS#e
$rdZ&YdSwxYw)&aG
Lightweight connection pooling for peewee.

In a multi-threaded application, up to `max_connections` will be opened. Each
thread (or, if using gevent, greenlet) will have it's own connection.

In a single-threaded application, only one connection will be created. It will
be continually recycled until either it exceeds the stale timeout or is closed
explicitly (using `.manual_close()`).

By default, all your application needs to do is ensure that connections are
closed when you are finished with them, and they will be returned to the pool.
For web applications, this typically means that at the beginning of a request,
you will open a connection, and when you return a response, you will close the
connection.

Simple Postgres pool example code:

    # Use the special postgresql extensions.
    from playhouse.pool import PooledPostgresqlExtDatabase

    db = PooledPostgresqlExtDatabase(
        'my_app',
        max_connections=32,
        stale_timeout=300,  # 5 minutes.
        user='postgres')

    class BaseModel(Model):
        class Meta:
            database = db

That's it!
�N)�
namedtuple)�chain)�TRANSACTION_STATUS_IDLE)�TRANSACTION_STATUS_INERROR)�TRANSACTION_STATUS_UNKNOWN)�
MySQLDatabase)�PostgresqlDatabase)�SqliteDatabasezpeewee.poolc�`�|�+t|ttf��st|��S|S�N)�
isinstance�int�float)�vals �E/opt/imunify360/venv/lib64/python3.11/site-packages/playhouse/pool.py�make_intr9s)��
��z�#��U�|�<�<���3�x�x���J�c��eZdZdS)�MaxConnectionsExceededN��__name__�
__module__�__qualname__�rrrr?s�������rr�PoolConnection)�	timestamp�
connection�checked_outc���eZdZ		d�fd�	Z		d�fd�	Zd�fd�	Z�fd�Zd�Zd	�Zd
�Z	d�fd�	Z
d�Zd
�Zdd�Z
d�Z�xZS)�PooledDatabase�Nc�4��t|��|_t|��|_t|��|_|jdkrt	d��|_g|_i|_t|_tt|��j|fi|��dS�Nr�inf)r�_max_connections�_stale_timeout�
_wait_timeoutr�_connections�_in_use�id�conn_key�superr �__init__)�self�database�max_connections�
stale_timeout�timeout�kwargs�	__class__s      �rr-zPooledDatabase.__init__Gs���� (�� 9� 9���&�}�5�5���%�g�.�.������"�"�!&�u���D�����
�����
�,��n�d�#�#�,�X�@�@��@�@�@�@�@rc���tt|��j|fi|��|�t|��|_|�t|��|_|�5t|��|_|jdkrtd��|_dSdSdSr#)r,r �initrr%r&r'r)r.r/r0r1r2�connect_kwargsr4s      �rr6zPooledDatabase.init_s����(��n�d�#�#�(��D�D�^�D�D�D��&�$,�_�$=�$=�D�!��$�"*�=�"9�"9�D����!)�'�!2�!2�D���!�Q�&�&�%*�5�\�\��"�"�"���&�&rFc���|js(tt|���|��St	j��|jz}|t	j��krf	tt|���|��}|S#t
$rt	jd��YnwxYw|t	j��k�ftd���)Ng�������?z:Max connections exceeded, timed out attempting to connect.)r'r,r �connect�timer�sleep)r.�
reuse_if_open�expires�retr4s    �rr9zPooledDatabase.connectks�����!�	F����.�.�6�6�}�E�E�E��)�+�+�� 2�2����	���#�#�
��N�D�1�1�9�9�-�H�H���
��*�
 �
 �
 ��
�3������
 ������	���#�#�%�&>�?�?�	?s�$(B�B/�.B/c���		tj|j��\}}|�|��}|�|��r t
�d|��dx}}n�|jrK|�|��r6t
�d|��|�	|d��dx}}n/n/#t$r!dx}}t
�d��YnwxYw��|��|jr,t|j
��|jkrtd���tt |�����}t%j��t'j��dzz
}|�|��}t
�d|��t)||t%j����|j
|<|S)NTzConnection %s was closed.z!Connection %s was stale, closing.z No connection available in pool.zExceeded maximum connections.i�zCreated new connection %s.)�heapq�heappopr(r+�
_is_closed�logger�debugr&�	_is_stale�_close�
IndexErrorr%�lenr)rr,r �_connectr:�randomr)r.�ts�conn�keyr4s    �rrIzPooledDatabase._connectzs����	�
� �=��):�;�;���D��m�m�D�)�)���?�?�4�(�(���L�L�!<�c�B�B�B� $�$�B����(�	�T�^�^�B�-?�-?�	�
�L�L�!D�c�J�J�J��K�K��d�+�+�+� $�$�B�����-�
�
�
� � ��T����?�@�@�@���
����	�:�<��$�
N����%�%��)>�>�>�,�-L�M�M�M����.�.�7�7�9�9�D�����v�}����5�5�B��-�-��%�%�C��L�L�5�s�;�;�;�*�2�t�T�Y�[�[�A�A���S���s�1B=�=(C(�'C(c�@�tj��|z
|jkSr)r:r&)r.rs  rrEzPooledDatabase._is_stale�s���	���i�'�4�+>�>�>rc��dS)NFr�r.rLs  rrBzPooledDatabase._is_closed�s���urc��dS)NTrrPs  r�
_can_reusezPooledDatabase._can_reuse�s���trc�|��|�|��}|r*tt|���|��dS||jvr�|j�|��}|jr_|�|j��rEt�
d|��tt|���|��dS|�|��r>t�
d|��tj
|j|j|f��dSt�
d|��dSdS)NzClosing stale connection %s.zReturning %s to pool.z
Closed %s.)r+r,r rFr)�popr&rErrCrDrRr@�heappushr()r.rL�
close_connrM�	pool_connr4s     �rrFzPooledDatabase._close�s*����m�m�D�!�!���	0��.�$�'�'�.�.�t�4�4�4�4�4�
�D�L�
 �
 ���(�(��-�-�I��"�
0�t�~�~�i�6I�'J�'J�
0����;�S�A�A�A��n�d�+�+�2�2�4�8�8�8�8�8�����&�&�
0����4�c�:�:�:���t�0�9�3F��2M�N�N�N�N�N����\�3�/�/�/�/�/�!�
 rc��|���rdS|���}|j�|�|��d��|���|�|d���dS)zS
        Close the underlying connection without returning it to the pool.
        FNT�rV)�	is_closedrr)rTr+�closerFrPs  r�manual_closezPooledDatabase.manual_close�s{���>�>���	��5���� � ��	
�������t�,�,�d�3�3�3��
�
�������D�T��*�*�*�*�*rc��|j5|jD]\}}|�|d����g|_ddd��dS#1swxYwYdS�NTrY)�_lockr(rF)r.�_rLs   r�
close_idlezPooledDatabase.close_idle�s���
�Z�	#�	#��,�
3�
3���4����D�T��2�2�2�2� "�D��	#�	#�	#�	#�	#�	#�	#�	#�	#�	#�	#�	#����	#�	#�	#�	#�	#�	#s�,A�A�A�Xc�(�|j5i}tj��|z
}d}|j���D]7\}}|j|kr"|�|jd���|dz
}�2|||<�8||_ddd��n#1swxYwY|S)NrTrY�)r_r:r)�itemsrrFr)r.�age�in_use�cutoff�nrMrWs       r�close_stalezPooledDatabase.close_stale�s����Z�
	"�
	"��F��Y�[�[�3�&�F��A�"&�,�"4�"4�"6�"6�
,�
,���Y��(�6�1�1��K�K�	� 4��K�F�F�F���F�A�A�"+�F�3�K�K�!�D�L�
	"�
	"�
	"�
	"�
	"�
	"�
	"�
	"�
	"�
	"�
	"����
	"�
	"�
	"�
	"��s�A3B�B�Bc�B�|���|j5|jD]\}}|�|d����|j���D]}|�|jd����g|_i|_ddd��dS#1swxYwYdSr^)r[r_r(rFr)�valuesr)r.r`rLrWs    r�	close_allzPooledDatabase.close_all�s���	
�
�
����
�Z�	�	��,�
3�
3���4����D�T��2�2�2�2�!�\�0�0�2�2�
C�
C�	����I�0�T��B�B�B�B� "�D���D�L�
	�	�	�	�	�	�	�	�	�	�	�	����	�	�	�	�	�	s�A+B�B�B)r!NN)NNN)F)rb)rrrr-r6r9rIrErBrRrFr\rarjrm�
__classcell__)r4s@rr r Fs5�������CG��A�A�A�A�A�A�0BF��
2�
2�
2�
2�
2�
2�
?�
?�
?�
?�
?�
?�(�(�(�(�(�T?�?�?�
������
0�
0�
0�
0�
0�
0�+�+�+�$#�#�#����� 
�
�
�
�
�
�
rr c��eZdZd�ZdS)�PooledMySQLDatabasec�B�	|�d��dS#YdSxYw�NFT)�pingrPs  rrBzPooledMySQLDatabase._is_closed�s0��	��I�I�e�����5��	��4�4���s��N�rrrrBrrrrprp��#����������rrpc��eZdZd�Zd�ZdS)�_PooledPostgresqlDatabasec��|jrdS|���}|tkrdS|tkr|���dS)NTF)�closed�get_transaction_statusrr�rollback�r.rL�
txn_statuss   rrBz$_PooledPostgresqlDatabase._is_closedsQ���;�	��4��0�0�2�2�
��3�3�3��4�
�2�
2�
2��M�M�O�O�O��urc���|���}|tkrdS|tkr|���n|tkr|���dSrr)rzrr�resetrr{r|s   rrRz$_PooledPostgresqlDatabase._can_reuses_���0�0�2�2�
��3�3�3��5�
�5�
5�
5��J�J�L�L�L�L�
�2�
2�
2��M�M�O�O�O��trN)rrrrBrRrrrrwrw�s2������	�	�	�����rrwc��eZdZdS)�PooledPostgresqlDatabaseNrrrrr�r���������Drr�)�PostgresqlExtDatabasec��eZdZdS)�PooledPostgresqlExtDatabaseNrrrrr�r���������rr�c��eZdZd�ZdS)�_PooledSqliteDatabasec�&�	|jdS#YdSxYwrr)�
total_changesrPs  rrBz _PooledSqliteDatabase._is_closed%s'��	�����5��	��4�4���s��Nrtrrrr�r�$rurr�c��eZdZdS)�PooledSqliteDatabaseNrrrrr�r�-r�rr�)�SqliteExtDatabasec��eZdZdS)�PooledSqliteExtDatabaseNrrrrr�r�3r�rr�)�CSqliteExtDatabasec��eZdZdS)�PooledCSqliteExtDatabaseNrrrrr�r�;r�rr�)'�__doc__r@�loggingrJr:�collectionsr�	itertoolsr�psycopg2.extensionsrrr�ImportError�peeweerr	r
�	getLoggerrCr�
ValueErrorrr�objectr rprwr��playhouse.postgres_extr�r�r�r��playhouse.sqlite_extr�r�r�r�rrr�<module>r�s��� � �B
��������
�
�
�
�����"�"�"�"�"�"�������.�;�;�;�;�;�;�>�>�>�>�>�>�>�>�>�>�>�>�>���.�.�.�*.�.��.�&�&�&�&�.����
!� � � � � �%�%�%�%�%�%�!�!�!�!�!�!�	��	�=�	)�	)�����/�.�.�.�.�Z�.�.�.���,�/>�?�?��l�l�l�l�l�V�l�l�l�^�����.�-�������������2	�	�	�	�	�8�:L�	�	�	�'�<�<�<�<�<�<�
�
�
�
�
�&?�AV�
�
�
�
���'�'�'�"&����'���������N����	�	�	�	�	�0�.�	�	�	�#�6�6�6�6�6�6�
�
�
�
�
�"7�9J�
�
�
�
���#�#�#�"����#����$�7�7�7�7�7�7�
�
�
�
�
�#8�:L�
�
�
�
�
���$�$�$�#�����$���sE�3�A�A�>C�C�C�?D�D�D�#D:�:E�E