� l\�gJ���dZddlmZddlZddlZddlZddlmZddlmZddlmZddlm Z ddlm Z dd lm Z dd lm Z dd lm Z ddlZd d lmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZd dlmZddlmZddlmZddlmZddlmZddlmZej rddl m!Z!Gd�de��Z"Gd�de"��Z#Gd �d!e#��Z$Gd"�d#e��Z%Gd$�d%e��Z&Gd&�d'e��Z'Gd(�d)e��Z(dS)*zPool implementation classes. �)� annotationsN)�Any)�cast)�List)�Optional)�Set)�Type)� TYPE_CHECKING)�Union�)�_AsyncConnDialect)�_ConnectionFairy��_ConnectionRecord)�_CreatorFnType)�_CreatorWRecFnType)�ConnectionPoolEntry)�Pool)�PoolProxiedConnection�)�exc)�util)�chop_traceback)�queue)�Literal)�DBAPIConnectionc��eZdZUdZdZejZded<ded< d)d*d�Z d+d�Z d,d�Z d-d�Z d.d�Z d/d�Zd0d �Zd1d"�Zd2d#�Zd3d$�Zd2d%�Zd2d&�Zd2d'�Zd(S)4� QueuePoolaOA :class:`_pool.Pool` that imposes a limit on the number of open connections. :class:`.QueuePool` is the default pooling implementation used for all :class:`_engine.Engine` objects other than SQLite with a ``:memory:`` database. The :class:`.QueuePool` class **is not compatible** with asyncio and :func:`_asyncio.create_async_engine`. The :class:`.AsyncAdaptedQueuePool` class is used automatically when using :func:`_asyncio.create_async_engine`, if no other kind of pool is specified. .. seealso:: :class:`.AsyncAdaptedQueuePool` F�1Type[sqla_queue.QueueCommon[ConnectionPoolEntry]]� _queue_classz+sqla_queue.QueueCommon[ConnectionPoolEntry]�_pool�� �>@�creator�)Union[_CreatorFnType, _CreatorWRecFnType]� pool_size�int� max_overflow�timeout�float�use_lifo�bool�kwrc ���tj||fi|��|�||���|_d|z |_|dkrdn||_||_tj��|_ dS)a� Construct a QueuePool. :param creator: a callable function that returns a DB-API connection object, same as that of :paramref:`_pool.Pool.creator`. :param pool_size: The size of the pool to be maintained, defaults to 5. This is the largest number of connections that will be kept persistently in the pool. Note that the pool begins with no connections; once this number of connections is requested, that number of connections will remain. ``pool_size`` can be set to 0 to indicate no size limit; to disable pooling, use a :class:`~sqlalchemy.pool.NullPool` instead. :param max_overflow: The maximum overflow size of the pool. When the number of checked-out connections reaches the size set in pool_size, additional connections will be returned up to this limit. When those additional connections are returned to the pool, they are disconnected and discarded. It follows then that the total number of simultaneous connections the pool will allow is pool_size + `max_overflow`, and the total number of "sleeping" connections the pool will allow is pool_size. `max_overflow` can be set to -1 to indicate no overflow limit; no limit will be placed on the total number of concurrent connections. Defaults to 10. :param timeout: The number of seconds to wait before giving up on returning a connection. Defaults to 30.0. This can be a float but is subject to the limitations of Python time functions which may not be reliable in the tens of milliseconds. :param use_lifo: use LIFO (last-in-first-out) when retrieving connections instead of FIFO (first-in-first-out). Using LIFO, a server-side timeout scheme can reduce the number of connections used during non-peak periods of use. When planning for server-side timeouts, ensure that a recycle or pre-ping strategy is in use to gracefully handle stale connections. .. versionadded:: 1.3 .. seealso:: :ref:`pool_use_lifo` :ref:`pool_disconnects` :param \**kw: Other keyword arguments including :paramref:`_pool.Pool.recycle`, :paramref:`_pool.Pool.echo`, :paramref:`_pool.Pool.reset_on_return` and others are passed to the :class:`_pool.Pool` constructor. )r,r�����N) r�__init__r r!� _overflow� _max_overflow�_timeout� threading�Lock�_overflow_lock)�selfr%r'r)r*r,r.s �d/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/sqlalchemy/pool/impl.pyr1zQueuePool.__init__Isu��@ � �d�G�*�*�r�*�*�*��&�&�y�8�&�D�D�� ��Y����#,��>�>�R�R�|����� �'�n�.�.������recordr�return�Nonec��� |j�|d��dS#tj$rF |���|���YdS#|���wxYwwxYw�NF)r!�put� sqla_queue�Full�close� _dec_overflow�r8r;s r9�_do_return_connzQueuePool._do_return_conn�s��� %� �J�N�N�6�5� )� )� )� )� )���� %� %� %� %�� � �����"�"�$�$�$�$�$�$���"�"�$�$�$�$����  %���s ��A4�A�A4�A0�0A4c��|jdk}|o|j|jk} |j�||j��S#t j$rYnwxYw|rk|j|jkr[|s|���Stj d|� ��|� ��|jfzd����|� ��rZ |� ��S#tj��5|���ddd��n #1swxYwY�xYw|���S)Nr0zSQueuePool limit of size %d overflow %d reached, connection timed out, timeout %0.2f�3o7r)�code)r3r2r!�getr4rA�Empty�_do_getr� TimeoutError�size�overflow� _inc_overflow�_create_connectionr� safe_reraiserD)r8� use_overflow�waits r9rLzQueuePool._do_get�s����)�B�.� ��D���$�2D� D�� ��:�>�>�$�� �6�6� 6���� � � � �D�  ���� � �D�N�d�.@�@�@�� ��|�|�~�~�%��&�:��y�y�{�{�D�M�M�O�O�T�]�C�D� � ���� � � � � � "� ��.�.�0�0�0�� ��&�(�(�)�)��&�&�(�(�(�)�)�)�)�)�)�)�)�)�)�)����)�)�)�)������<�<�>�>� !s?�?�A�A�C*�*D,�?D � D,� D$ �$D,�'D$ �(D,c���|jdkr|xjdz c_dS|j5|j|jkr|xjdz c_ ddd��dS ddd��dS#1swxYwYdS)Nr0r TF�r3r2r7�r8s r9rPzQueuePool._inc_overflow�s��� � �� #� #� �N�N�a� �N�N��4� � � � ��~�� 2�2�2����!�#���� � � � � � � � � �  � � � � � � � � � � � ���� � � � � � s�"A"�A"�"A&�)A&� Literal[True]c��|jdkr|xjdzc_dS|j5|xjdzc_ ddd��dS#1swxYwYdS)Nr0r TrVrWs r9rDzQueuePool._dec_overflow�s��� � �� #� #� �N�N�a� �N�N��4� � � � � �N�N�a� �N�N�� � � � � � � � � � � � ���� � � � � � s�A�A� Ac��|j�d��|�|j|jj|j|j|jj|j |j |j |j |j |j|j�� � S)N�Pool recreating) r'r)�pre_pingr,r*�recycle�echo� logging_name�reset_on_return� _dispatch�dialect)�logger�info� __class__�_creatorr!�maxsizer3� _pre_pingr,r4�_recycler^�_orig_logging_name�_reset_on_return�dispatch�_dialectrWs r9�recreatezQueuePool.recreate�sy�� � ���*�+�+�+��~�~� �M��j�(��+��^��Z�(��M��M����0� �1��m��M��  �  � r:c�&� |j�d��}|���n#tj$rYnwxYw�Fd|���z |_|j�d|� ����dS)NTFrzPool disposed. %s) r!rJrCrArKrNr2rcrd�status�r8�conns r9�disposezQueuePool.dispose�s��� � ��z�~�~�e�,�,��� � � � � � ���#� � � ��� ����  ��T�Y�Y�[�[���� � ���,�d�k�k�m�m�<�<�<�<�<s�.2�A�A�strc��d|���|���|���|���fzS)Nz_Pool size: %d Connections in pool: %d Current Overflow: %d Current Checked out connections: %d)rN� checkedinrO� checkedoutrWs r9rpzQueuePool.status�sH�� �� � � � ���� � �� � ������!�!� � � r:c��|jjS�N)r!rgrWs r9rNzQueuePool.size�s ���z�!�!r:c��|jSry)r4rWs r9r*zQueuePool.timeout�s ���}�r:c�4�|j���Sry)r!�qsizerWs r9rvzQueuePool.checkedin�s���z���!�!�!r:c�,�|jjr|jndS)Nr)r!rgr2rWs r9rOzQueuePool.overflow�s��!%��!3�:�t�~�~��:r:c�^�|jj|j���z |jzSry)r!rgr|r2rWs r9rwzQueuePool.checkedouts'���z�!�D�J�$4�$4�$6�$6�6���G�Gr:N)r"r#r$F) r%r&r'r(r)r(r*r+r,r-r.r�r;rr<r=�r<r)r<r-)r<rX)r<r�r<r=�r<rt)r<r()r<r+)�__name__� __module__� __qualname__�__doc__� _is_asynciorA�Queuer �__annotations__r1rFrLrPrDrnrsrprNr*rvrOrw�r:r9rr-sz���������&�K� �������7�6�6�6� ���� E/�E/�E/�E/�E/�N%�%�%�%�"�"�"�"�@ � � � ����� � � � �" =� =� =� =�  �  �  �  �"�"�"�"�����"�"�"�"�;�;�;�;�H�H�H�H�H�Hr:rc�D�eZdZUdZdZejZded<e ��Z dS)�AsyncAdaptedQueuePoola�An asyncio-compatible version of :class:`.QueuePool`. This pool is used by default when using :class:`.AsyncEngine` engines that were generated from :func:`_asyncio.create_async_engine`. It uses an asyncio-compatible queue implementation that does not use ``threading.Lock``. The arguments and operation of :class:`.AsyncAdaptedQueuePool` are otherwise identical to that of :class:`.QueuePool`. Trr N) r�r�r�r�r�rA�AsyncAdaptedQueuer r�r rmr�r:r9r�r�sP������� � ��K��$������!� �"�"�H�H�Hr:r�c��eZdZejZdS)�FallbackAsyncAdaptedQueuePoolN)r�r�r�rA�FallbackAsyncAdaptedQueuer r�r:r9r�r�s�������7�L�L�Lr:r�c�:�eZdZdZd d�Zdd�Zdd �Zdd �Zdd �Zd S)�NullPoola�A Pool which does not pool connections. Instead it literally opens and closes the underlying DB-API connection per each connection open/close. Reconnect-related functions such as ``recycle`` and connection invalidation are not supported by this Pool implementation, since no connections are held persistently. The :class:`.NullPool` class **is compatible** with asyncio and :func:`_asyncio.create_async_engine`. r<rtc��dS)Nr�r�rWs r9rpzNullPool.status-s���zr:r;rr=c�.�|���dSry)rCrEs r9rFzNullPool._do_return_conn0s��� � �����r:c�*�|���Sry)rQrWs r9rLzNullPool._do_get3s���&�&�(�(�(r:c ���|j�d��|�|j|j|j|j|j|j|j |j ���S)Nr[)r]r^r_r`r\rarb) rcrdrerfrir^rjrkrhrlrmrWs r9rnzNullPool.recreate6s^�� � ���*�+�+�+��~�~� �M��M����0� �1��^��m��M��  �  � r:c��dSryr�rWs r9rszNullPool.disposeD��� �r:Nr�rr�)r<r�r�) r�r�r�r�rprFrLrnrsr�r:r9r�r�s������� � ���������)�)�)�)�  �  �  �  � � � � � � r:r�c�Z�eZdZdZdZ ddd �Zdd �Zdd�Zdd�Zdd�Z dd�Z dd�Z d d�Z dS)!�SingletonThreadPoola'A Pool that maintains one connection per thread. Maintains one connection per each thread, never moving a connection to a thread other than the one which it was created in. .. warning:: the :class:`.SingletonThreadPool` will call ``.close()`` on arbitrary connections that exist beyond the size setting of ``pool_size``, e.g. if more unique **thread identities** than what ``pool_size`` states are used. This cleanup is non-deterministic and not sensitive to whether or not the connections linked to those thread identities are currently in use. :class:`.SingletonThreadPool` may be improved in a future release, however in its current status it is generally used only for test scenarios using a SQLite ``:memory:`` database and is not recommended for production use. The :class:`.SingletonThreadPool` class **is not compatible** with asyncio and :func:`_asyncio.create_async_engine`. Options are the same as those of :class:`_pool.Pool`, as well as: :param pool_size: The number of threads in which to maintain connections at once. Defaults to five. :class:`.SingletonThreadPool` is used by the SQLite dialect automatically when a memory-based database is used. See :ref:`sqlite_toplevel`. Fr"r%r&r'r(r.rc ���tj||fi|��tj��|_tj��|_t ��|_||_dSry) rr1r5�local�_conn�_fairy�set� _all_connsrN)r8r%r'r.s r9r1zSingletonThreadPool.__init__ksR�� � �d�G�*�*�r�*�*�*��_�&�&�� ��o�'�'�� �47�E�E����� � � r:r<c ���|j�d��|�|j|j|j|j|j|j|j |j |j �� � S)Nr[)r'r]r^r\r_r`rarb) rcrdrerfrNrir^rhrjrkrlrmrWs r9rnzSingletonThreadPool.recreatewsc�� � ���*�+�+�+��~�~� �M��i��M����^��0� �1��m��M��  �  � r:r=c��|jD]'} |����#t$rY�$wxYw|j���dS)zDispose of this pool.N)r�rC� Exception�clearrqs r9rszSingletonThreadPool.dispose�sg���O� � �D� �� � � � � � ��� � � ��� ���� ��������s � � -�-c���t|j��|jkrL|j���}|���t|j��|jk�JdSdSry)�lenr�rN�poprC�r8�cs r9�_cleanupzSingletonThreadPool._cleanup�s^���$�/�"�"�d�i�/�/���#�#�%�%�A� �G�G�I�I�I��$�/�"�"�d�i�/�/�/�/�/�/r:rtc�N�dt|��t|j��fzS)Nz"SingletonThreadPool id:%d size: %d)�idr�r�rWs r9rpzSingletonThreadPool.status�s*��3� �t�H�H� ��� � �7 � � r:r;rc�8� |j`dS#t$rYdSwxYwry)r��current�AttributeErrorrEs r9rFz#SingletonThreadPool._do_return_conn�s7�� �� �#�#�#��� � � � �D�D� ���s � � �c��� tr-tt|j�����}n|j���}|r|Sn#t $rYnwxYw|���}tj|��|j_t|j ��|j kr|� ��|j � |��|Sry)r rrr�r�r�rQ�weakref�refr�r�rNr��addr�s r9rLzSingletonThreadPool._do_get�s��� �� )��,�d�j�.@�.@�.B�.B�C�C����J�&�&�(�(��� ��� ��� � � � �D� ���� � #� #� %� %��$�[��^�^�� �� �t�� � �4�9� ,� ,� �M�M�O�O�O� ����A�����s�AA� A!� A!rc��� tt|j�����}|�|���Sn#t $rYnwxYwtj||j��Sry)rrr�r��_checkout_existingr�� _checkout�r8�recs r9�connectzSingletonThreadPool.connect�s|�� 0��'���)<�)<�)>�)>�?�?�C����-�-�/�/�/���� � � � �D� ����  �)�$�� �<�<�<s�,A� A�AN)r")r%r&r'r(r.r)r<r�r�r�rr�)r<r) r�r�r�r�r�r1rnrsr�rprFrLr�r�r:r9r�r�Hs���������@�K� � � � � � �  �  �  �  �  �  �  �  �����  � � � � ���� ����" =� =� =� =� =� =r:r�c�l�eZdZdZejdd���Zdd�Zdd�Zdd �Z dd �Z dd �Z dd�Z dd�Z dS)� StaticPoola~A Pool of exactly one connection, used for all requests. Reconnect-related functions such as ``recycle`` and connection invalidation (which is also used to support auto-reconnect) are only partially supported right now and may not yield good results. The :class:`.StaticPool` class **is compatible** with asyncio and :func:`_asyncio.create_async_engine`. r<rc� �t|��SryrrWs r9� connectionzStaticPool.connection�s�� ��&�&�&r:rtc��dS)Nr�r�rWs r9rpzStaticPool.status�s���|r:r=c�z�d|jvr/|jj�%|j���|jd=dSdSdS�Nr�)�__dict__r��dbapi_connectionrCrWs r9rszStaticPool.dispose�sL�� �D�M� )� )���0�<� �O� !� !� #� #� #�� �l�+�+�+� *� )�<�<r:c ���|j�d��|�|j|j|j|j|j|j|j |j ���S)Nr[)r%r]r`r\r^r_rarb) rcrdrerfrirkrhr^rjrlrmrWs r9rnzStaticPool.recreate�s^�� � ���*�+�+�+��~�~��M��M� �1��^����0��m��M��  �  � r:�other_static_poolc�"��d�fd� }||_dS)Nr�rr<rc�(���jj}|�J�|Sry)r�r�)r�rrr�s �r9r%z*StaticPool._transfer_from.<locals>.creator�s���$�/�@�D��#�#�#��Kr:)r�rr<r)�_invoke_creator)r8r�r%s ` r9�_transfer_fromzStaticPool._transfer_from�s1��� � � � � � � '����r:rc��t���ry)�NotImplementedErrorrWs r9rQzStaticPool._create_connection�s��!�#�#�#r:r;c��dSryr�rEs r9rFzStaticPool._do_return_conn�r�r:c�Z�|j}|���r|jd=|j}|Sr�)r��_is_hard_or_soft_invalidatedr�r�s r9rLzStaticPool._do_get�s3���o�� � +� +� -� -� "�� �l�+��/�C�� r:N)r<rr�r�)r<r�)r�r�r<r=r�r)r�r�r�r�r�memoized_propertyr�rprsrnr�rQrFrLr�r:r9r�r��s������� � � ��'�'�'���'�����,�,�,�,�  �  �  �  �'�'�'�'�$�$�$�$� � � � ������r:r�c�X�eZdZUdZded<ded<dd �Zdd �Zdd�Zdd�Zdd�Z dd�Z dS)� AssertionPoola}A :class:`_pool.Pool` that allows at most one checked out connection at any given time. This will raise an exception if more than one connection is checked out at a time. Useful for debugging code that is using more connections than desired. The :class:`.AssertionPool` class **is compatible** with asyncio and :func:`_asyncio.create_async_engine`. zOptional[ConnectionPoolEntry]r�zOptional[List[str]]�_checkout_traceback�argsrr.c��d|_d|_|�dd��|_d|_t j|g|�Ri|��dS)NF�store_tracebackT)r�� _checked_outr��_store_tracebackr�rr1)r8r�r.s r9r1zAssertionPool.__init__sV���� �!��� "���'8�$� ?� ?���#'�� � � �d�(�T�(�(�(�R�(�(�(�(�(r:r<rtc��dS)Nr�r�rWs r9rpzAssertionPool.statuss���r:r;rr=c�V�|jstd���d|_||jusJ�dS)Nzconnection is not checked outF)r��AssertionErrorr�rEs r9rFzAssertionPool._do_return_conns>��� � B� �!@�A�A� A�!������#�#�#�#�#�#r:c�X�d|_|jr|j���dSdSr?)r�r�rCrWs r9rszAssertionPool.dispose#s8��!��� �:� � �J� � � � � � � � � r:c ���|j�d��|�|j|j|j|j|j|j|j |j ���S)Nr[)r^r\r]r`r_rarb) rcrdrerfr^rhrirkrjrlrmrWs r9rnzAssertionPool.recreate(s^�� � ���*�+�+�+��~�~� �M����^��M� �1��0��m��M��  �  � r:c�6�|jrF|jr+dd�t|j����z}nd}t d|z���|js|���|_d|_|jrtj ��|_|jS)Nz at: %s�z!connection is already checked outT) r�r��joinrr�r�rQr�� traceback� format_stack)r8�suffixs r9rLzAssertionPool._do_get5s��� � � O��'� �#�b�g�g�"�4�#;�<�<�'�'������ �!D�v�!M�N�N� N��z� 3��0�0�2�2�D�J� ��� � � @�'0�'=�'?�'?�D� $��z�r:N)r�rr.rr�rr�)r<r�r�) r�r�r�r�r�r1rprFrsrnrLr�r:r9r�r�s�������� � �)�(�(�(�,�,�,�,�)�)�)�)�����$�$�$�$� ����  �  �  �  ������r:r�))r�� __future__rr5r��typingrrrrrr r r r��baser rrrrrrrr�rrrrrA� util.typingr�engine.interfacesrrr�r�r�r�r�r�r�r:r9�<module>r�s;����#�"�"�"�"�"��������� � � � ������������������������������������� � � � � � �����������#�#�#�#�#�#�"�"�"�"�"�"�#�#�#�#�#�#� � � � � � �$�$�$�$�$�$�%�%�%�%�%�%�������'�'�'�'�'�'�������������!�!�!�!�!�!�&�&�&�&�&�&�!�!�!�!�!�!� ��4�3�3�3�3�3�3�UH�UH�UH�UH�UH��UH�UH�UH�p#�#�#�#�#�I�#�#�#�*8�8�8�8�8�$9�8�8�8�' �' �' �' �' �t�' �' �' �Tw=�w=�w=�w=�w=�$�w=�w=�w=�t>�>�>�>�>��>�>�>�BB�B�B�B�B�D�B�B�B�B�Br:
Memory