� J�g�.��t�ddlZddlZddlZddlmZmZddlmZm Z ddl m Z m Z ddl mZGd�d��ZdS)�N)�SimpleNamespace� TracebackType)�Optional�Type)� LockError�LockNotOwnedError)�Numberc ��eZdZdZdZdZdZdZdZdZ d%de d e e d e d e d e e d e f d�Zd&d�Zd'd�Zde eede ede eddfd�Z d(d e e d e e d e e de e fd�Zde de fd�Zde fd�Zde fd�Zd&d�Zde ddfd�Zd)ded e de fd!�Zded e de fd"�Zde fd#�Zde fd$�ZdS)*�Lockz� A shared, distributed Lock. Using Redis for locking allows the Lock to be shared across processes and/or machines. It's left to the user to resolve deadlock issues and make sure multiple clients play nicely together. Nz� local token = redis.call('get', KEYS[1]) if not token or token ~= ARGV[1] then return 0 end redis.call('del', KEYS[1]) return 1 a� local token = redis.call('get', KEYS[1]) if not token or token ~= ARGV[1] then return 0 end local expiration = redis.call('pttl', KEYS[1]) if not expiration then expiration = 0 end if expiration < 0 then return 0 end local newttl = ARGV[2] if ARGV[3] == "0" then newttl = ARGV[2] + expiration end redis.call('pexpire', KEYS[1], newttl) return 1 z� local token = redis.call('get', KEYS[1]) if not token or token ~= ARGV[1] then return 0 end redis.call('pexpire', KEYS[1], ARGV[2]) return 1 皙�����?T�name�timeout�sleep�blocking�blocking_timeout� thread_localc��||_||_||_||_||_||_t |��|_|jrtj ��n t��|_ d|j _ |� ��dS)a� Create a new Lock instance named ``name`` using the Redis client supplied by ``redis``. ``timeout`` indicates a maximum life for the lock in seconds. By default, it will remain locked until release() is called. ``timeout`` can be specified as a float or integer, both representing the number of seconds to wait. ``sleep`` indicates the amount of time to sleep in seconds per loop iteration when the lock is in blocking mode and another client is currently holding the lock. ``blocking`` indicates whether calling ``acquire`` should block until the lock has been acquired or to fail immediately, causing ``acquire`` to return False and the lock not being acquired. Defaults to True. Note this value can be overridden by passing a ``blocking`` argument to ``acquire``. ``blocking_timeout`` indicates the maximum amount of time in seconds to spend trying to acquire the lock. A value of ``None`` indicates continue trying forever. ``blocking_timeout`` can be specified as a float or integer, both representing the number of seconds to wait. ``thread_local`` indicates whether the lock token is placed in thread-local storage. By default, the token is placed in thread local storage so that a thread only sees its token, not a token set by another thread. Consider the following timeline: time: 0, thread-1 acquires `my-lock`, with a timeout of 5 seconds. thread-1 sets the token to "abc" time: 1, thread-2 blocks trying to acquire `my-lock` using the Lock instance. time: 5, thread-1 has not yet completed. redis expires the lock key. time: 5, thread-2 acquired `my-lock` now that it's available. thread-2 sets the token to "xyz" time: 6, thread-1 finishes its work and calls release(). if the token is *not* stored in thread local storage, then thread-1 would see the token value as "xyz" and would be able to successfully release the thread-2's lock. In some use cases it's necessary to disable thread local storage. For example, if you have code where one thread acquires a lock and passes that lock instance to a worker thread to release later. If thread local storage isn't disabled in this case, the worker thread won't see the token set by the thread that acquired the lock. Our assumption is that these cases aren't common and as such default to using thread local storage. N) �redisr rrrr�boolr� threading�localr�token�register_scripts)�selfrr rrrrrs �Z/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/redis/lock.py�__init__z Lock.__init__Ls���x�� ��� ��� ��� � �� � 0��� ��.�.���*.�*;�R�Y�_�&�&�&��AR�AR�� ��� �� ���������returnc� �|j}|j}|j�|�|j��|_|j�|�|j��|_|j�!|�|j��|_dSdS�N) � __class__r� lua_release�register_script�LUA_RELEASE_SCRIPT� lua_extend�LUA_EXTEND_SCRIPT� lua_reacquire�LUA_REACQUIRE_SCRIPT)r�cls�clients rrzLock.register_scripts�s����n����� �?� "�$�4�4�S�5K�L�L�C�O� �>� !�#�3�3�C�4I�J�J�C�N� � � $� &� 6� 6�s�7O� P� P�C� � � � %� $rc�Z�|���r|Std|j����)Nz0Unable to acquire lock within the time specified�� lock_name)�acquirerr �rs r� __enter__zLock.__enter__�s6�� �<�<�>�>� ��K�� >��i� � � � r�exc_type� exc_value� tracebackc�.�|���dSr )�release)rr1r2r3s r�__exit__z Lock.__exit__�s�� � � �����rrc���|�|j}|�+tj��j���}n.|j���}|�|��}|�|j}|�|j}d}|�tj ��|z} |� |��r||j _ dS|sdStj ��|z}|�||krdStj|���\)as Use Redis to hold a shared, distributed lock named ``name``. Returns True once the lock is acquired. If ``blocking`` is False, always return immediately. If the lock was acquired, return True, otherwise return False. ``blocking_timeout`` specifies the maximum number of seconds to wait trying to acquire the lock. ``token`` specifies the token value to be used. If provided, token must be a bytes object or a string that can be encoded to a bytes object with the default encoding. If a token isn't specified, a UUID will be generated. NTF)r�uuid�uuid1�hex�encoder� get_encoderrr�mod_time� monotonic� do_acquirerr)rrrrr�encoder�stop_trying_at� next_try_ats rr.z Lock.acquire�s��, �=��J�E� �=��J�L�L�$�+�+�-�-�E�E��j�,�,�.�.�G��N�N�5�)�)�E� � ��}�H� � #�#�4� ��� � '�%�/�1�1�4D�D�N� "����u�%�%� �#(�� � ��t�� ��u�"�,�.�.��6�K��)�k�N�.J�.J��u� �N�5� !� !� !� "rc��|jrt|jdz��}nd}|j�|j|d|���rdSdS)N��T)�nx�pxF)r�intr�setr )rrrs rr?zLock.do_acquire�sQ�� �<� ��$�,��-�.�.�G�G��G� �:�>�>�$�)�U�t��>� @� @� ��4��urc�D�|j�|j��duS)zU Returns True if this key is locked by any process, otherwise False. N)r�getr r/s r�lockedz Lock.locked�s���z�~�~�d�i�(�(��4�4rc��|j�|j��}|rCt|t��s.|j���}|�|��}|jjduo||jjkS)zS Returns True if this key is locked by this lock, otherwise False. N) rrJr � isinstance�bytesr<r;rr)r� stored_tokenr@s r�ownedz Lock.owned�sw���z�~�~�d�i�0�0� � � 8� �<�� ?� ?� 8��j�,�,�.�.�G�"�>�>�,�7�7�L��z��t�+�P� �� �@P�0P�Prc��|jj}|�td|j����d|j_|�|��dS)z4 Releases the already acquired lock NzCannot release an unlocked lockr,)rrrr � do_release�r�expected_tokens rr5z Lock.release�sL����)�� � !��=���S�S�S� S��� �� ����'�'�'�'�'rrTc��t|�|jg|g|j�����st d|j����dS)N��keys�argsr*z,Cannot release a lock that's no longer ownedr,)rr"r rrrSs rrRzLock.do_releases`��� � � �4�9�+�^�4D�T�Z� � X� X� � � �$�>��)���� � � rF�additional_time� replace_ttlc��|jj�td|j����|j�td|j����|�||��S)ah Adds more time to an already acquired lock. ``additional_time`` can be specified as an integer or a float, both representing the number of seconds to add. ``replace_ttl`` if False (the default), add `additional_time` to the lock's existing ttl. If True, replace the lock's ttl with `additional_time`. NzCannot extend an unlocked lockr,z$Cannot extend a lock with no timeout)rrrr r� do_extend�rrYrZs r�extendz Lock.extend sW�� �:� � #��<�� �R�R�R� R� �<� ��B�d�i�X�X�X� X��~�~�o�{�;�;�;rc���t|dz��}t|�|jg|jj||rdndg|j�����std|j����dS)NrD�1�0rVz+Cannot extend a lock that's no longer ownedr,T)rGrr%r rrrrr]s rr\zLock.do_extends����o��4�5�5��� �O�O��i�[��j�&�� �9T���QT�U��z� � � � � � �$�=��)���� ��trc��|jj�td|j����|j�td|j����|���S)zS Resets a TTL of an already acquired lock back to a timeout value. Nz!Cannot reacquire an unlocked lockr,z'Cannot reacquire a lock with no timeout)rrrr r� do_reacquirer/s r� reacquirezLock.reacquire+s`�� �:� � #��?�4�9�U�U�U� U� �<� ��9��)���� �� � �"�"�"rc���t|jdz��}t|�|jg|jj|g|j�����std|j����dS)NrDrVz.Cannot reacquire a lock that's no longer ownedr,T) rGrrr'r rrrr)rrs rrczLock.do_reacquire8s���d�l�T�)�*�*��� � � ��i�[�� �(8�'�'B�4�:� � � � � � � $�@��)���� ��tr)Nr TNT)rN)rr )NNNN)F) �__name__� __module__� __qualname__�__doc__r"r%r'r$r&r(�strrr rrrr0r� BaseExceptionrr6r.r?rKrPr5rRrGr^r\rdrc�rrr r s����������K��J��M� ����2��%)���-1�!�E �E ��E ��&�!� E � � E � � E �#�6�*�E ��E �E �E �E �NQ�Q�Q�Q� � � � ���4� �.�/���M�*���M�*� � � ����#'�#'�-1�#� -"�-"����-"��4�.�-"�#�6�*� -"� ��}� -"�-"�-"�-"�^��������5��5�5�5�5� Q�t� Q� Q� Q� Q�(�(�(�(���������<�<�c�<��<��<�<�<�<�" �� �4� �D� � � � � #�4� #� #� #� #� �d� � � � � � rr )r�timer=r8�typesrr�typingrr�redis.exceptionsrr� redis.typingr r rlrr�<module>rrs����������� � � � �0�0�0�0�0�0�0�0�!�!�!�!�!�!�!�!�9�9�9�9�9�9�9�9�������x�x�x�x�x�x�x�x�x�xr
Memory