� J�gE9��6�ddlZddlZddlmZddlmZddlmZddlm Z m Z m Z ddl m Z mZmZmZddlmZGd�d e ��ZGd �d e ��ZGd �d e ��ZGd�dee ��ZGd�d��ZGd�de ��ZGd�de��ZdS)�N)�Optional)�Redis)�SentinelCommands)� Connection�ConnectionPool� SSLConnection)�ConnectionError� ReadOnlyError� ResponseError� TimeoutError)� str_if_bytesc��eZdZdS)�MasterNotFoundErrorN��__name__� __module__� __qualname__���^/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/redis/sentinel.pyrr ��������Drrc��eZdZdS)�SlaveNotFoundErrorNrrrrrrrrrc�r��eZdZ�fd�Zd�Z�fd�Zd�Zd�Z d ddd�dee d ee f�fd �Z �xZ S) �SentinelManagedConnectionc �n��|�d��|_t��jdi|��dS)N�connection_poolr)�popr�super�__init__)�self�kwargs� __class__s �rr z"SentinelManagedConnection.__init__s;���%�z�z�*;�<�<��������"�"�6�"�"�"�"�"rc��|j}dt|��j�dt|��j�d|j�d�}|jrd|j�d|j��}||z}|S)N�<�.� (service=z%s)>z,host=z,port=)r�typerr� service_name�host�port)r!�pool�s� host_infos r�__repr__z"SentinelManagedConnection.__repr__s����#�� 0��T� � �%� 0� 0��T� � �(;� 0� 0��)� 0� 0� 0� � �9� �=���=�=�$�)�=�=�I��I� �A��rc���|\|_|_t�����|jjrI|�d��t|�����dkrtd���dSdS)N�PING�PONGz PING failed) r*r+r�connectr�check_connection� send_commandr � read_responser )r!�addressr#s �r� connect_toz$SentinelManagedConnection.connect_to$s����&��� �4�9� �������� � � 0� 5� � � �f� %� %� %��D�.�.�0�0�1�1�V�;�;�%�m�4�4�4� 5� 5�;�;rc��|jrdS|jjr.|�|j�����dS|j���D])} |�|��cS#t $rY�&wxYwt��N)�_sockr� is_masterr8�get_master_address� rotate_slavesr r)r!�slaves r�_connect_retryz(SentinelManagedConnection._connect_retry,s��� �:� � �F� � � )� %� �O�O�D�0�C�C�E�E� F� F� F� F� F��-�;�;�=�=� � ����?�?�5�1�1�1�1�1��&�����H�����$� $s� A7�7 B�Bc�D�|j�|jd���S)Nc��dSr:r)�errors r�<lambda>z3SentinelManagedConnection.connect.<locals>.<lambda>:s��T�r)�retry�call_with_retryr@�r!s rr3z!SentinelManagedConnection.connect9s!���z�)�)�$�*=�?Q�?Q�R�R�RrF)�disconnect_on_error� push_requestrHrIc���� t���|||���S#t$r1|jjr#|���t d����wxYw)N)�disable_decodingrHrIz"The previous master is now a slave)rr6r rr<� disconnectr )r!rKrHrIr#s �rr6z'SentinelManagedConnection.read_response<s���� ��7�7�(�(�!1�$7�)�)��� �� � � � ��#�-� L� ���!�!�!�%�&J�K�K�K� � ���s �#'�;A")F) rrrr r/r8r@r3r�boolr6� __classcell__�r#s@rrrs��������#�#�#�#�#� � � �5�5�5�5�5� %� %� %�S�S�S� ��/4�',� ���&�d�^� � �t�n� ���������rrc��eZdZdS)�SentinelManagedSSLConnectionNrrrrrQrQUrrrQc�&�eZdZd�Zd�Zd�Zd�ZdS)�SentinelConnectionPoolProxyc��tj|��|_||_||_||_||_|���dSr:)�weakref�ref�connection_pool_refr<r4r)�sentinel_manager�reset)r!rr<r4r)rXs rr z$SentinelConnectionPoolProxy.__init__ZsF��$+�;��#?�#?�� �"��� 0���(��� 0��� � � � � � � � rc�"�d|_d|_dSr:)�master_address�slave_rr_counterrGs rrYz!SentinelConnectionPoolProxy.resetis��"��� $����rc���|j�|j��}|jr>|j|kr3||_|���}|�|�d���|S)NF)�inuse_connections)rX�discover_masterr)r<r[rWrL)r!r[rs rr=z.SentinelConnectionPoolProxy.get_master_addressmsq���.�>�>�t�?P�Q�Q�� �>� D�d�1�^�C�C�"0�D� �#�6�6�8�8�O��*��*�*�U�*�C�C�C��rc#��K�|j�|j��}|r�|j�*t jdt |��dz ��|_tt |����D]2}|jdzt |��z|_||j}|V��3 |���V�n#t$rYnwxYwtd|j�����)Nr�zNo slave found for ) rX�discover_slavesr)r\�random�randint�len�ranger=rr)r!�slaves�_r?s rr>z)SentinelConnectionPoolProxy.rotate_slavesxs������&�6�6�t�7H�I�I�� � ��$�,�(.��q�#�f�+�+��/�(J�(J��%��3�v�;�;�'�'� � ��)-�)>��)B�c�&�k�k�(Q��%��t�4�5��� � � � � ��)�)�+�+� +� +� +� +��"� � � � �D� ���� �!L�t�7H�!L�!L�M�M�Ms�%B<�< C �C N)rrrr rYr=r>rrrrSrSYsU������ � � �%�%�%� � � �N�N�N�N�NrrSc�^��eZdZdZ�fd�Zd�Z�fd�Zed���Z�fd�Z d�Z d�Z �xZ S) �SentinelConnectionPoolz� Sentinel backed connection pool. If ``check_connection`` flag is set to True, SentinelManagedConnection sends a PING command right after establishing the connection. c ���|�d|�dd��rtnt��|d<|�dd��|_|�dd��|_t ||j|j||���|_t��j d i|��|j|j d<||_ ||_ dS) N�connection_class�sslFr<Tr4)rr<r4r)rXrr) �getrrQrr<r4rS�proxyrr �connection_kwargsr)rX)r!r)rXr"r#s �rr zSentinelConnectionPool.__init__�s����%+�Z�Z� ��:�:�e�U�+�+�/�,�,�.� & �& ��!�"� ���K��6�6��� &� � �+=�u� E� E���0� ��n�!�2�%�-�  � � �� � �����"�"�6�"�"�"�48�J���0�1�(��� 0����rc ��|jrdnd}dt|��j�dt|��j�d|j�d|�d� S)N�masterr?r%r&r'�(z))>)r<r(rrr))r!�roles rr/zSentinelConnectionPool.__repr__�sh���>�6�x�x�w�� 6��T� � �%� 6� 6��T� � �(;� 6� 6��)� 6� 6�,0� 6� 6� 6� rc�z��t�����|j���dSr:)rrYro)r!r#s �rrYzSentinelConnectionPool.reset�s.��� ��� � ���� � �������rc��|jjSr:)ror[rGs rr[z%SentinelConnectionPool.master_address�s ���z�(�(rc���|j p|jo|j|j|jfk}t ��}|o|�|��Sr:)r<r[r*r+r�owns_connection)r!� connection�check�parentr#s �rrxz&SentinelConnectionPool.owns_connection�sT����N�"� � �N� X�t�2�z�� ��6X�X� ������;��/�/� �;�;�;rc�4�|j���Sr:)ror=rGs rr=z)SentinelConnectionPool.get_master_address�s���z�,�,�.�.�.rc�4�|j���S)zRound-robin slave balancer)ror>rGs rr>z$SentinelConnectionPool.rotate_slaves�s���z�'�'�)�)�)r) rrr�__doc__r r/rY�propertyr[rxr=r>rNrOs@rrjrj�s����������1�1�1�1�1�. � � �������)�)��X�)�<�<�<�<�<�/�/�/�*�*�*�*�*�*�*rrjc�\�eZdZdZ d d�Zd�Zd�Zd�Zd�Zd �Z d �Z e e fd �Z e e fd �ZdS)�Sentinelar Redis Sentinel cluster client >>> from redis.sentinel import Sentinel >>> sentinel = Sentinel([('localhost', 26379)], socket_timeout=0.1) >>> master = sentinel.master_for('mymaster', socket_timeout=0.1) >>> master.set('foo', 'bar') >>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1) >>> slave.get('foo') b'bar' ``sentinels`` is a list of sentinel nodes. Each node is represented by a pair (hostname, port). ``min_other_sentinels`` defined a minimum number of peers for a sentinel. When querying a sentinel, if it doesn't meet this threshold, responses from that sentinel won't be considered valid. ``sentinel_kwargs`` is a dictionary of connection arguments used when connecting to sentinel instances. Any argument that can be passed to a normal Redis connection can be specified here. If ``sentinel_kwargs`` is not specified, any socket_timeout and socket_keepalive options specified in ``connection_kwargs`` will be used. ``connection_kwargs`` are keyword arguments that will be used when establishing a connection to a Redis server. rNc ���|�d�|���D��}|�_�fd�|D���_|�_|�_|�_dS)Nc�D�i|]\}}|�d���||��S)�socket_)� startswith)�.0�k�vs r� <dictcomp>z%Sentinel.__init__.<locals>.<dictcomp>�s?�������A�a�l�l�9�>U�>U���1���rc�<��g|]\}}t||fi�j����Sr)r�sentinel_kwargs)r��hostnamer+r!s �r� <listcomp>z%Sentinel.__init__.<locals>.<listcomp>�sB��� � � ���$� �(�D� 9� 9�D�$8� 9� 9� � � r)�itemsr�� sentinels�min_other_sentinelsrp�_force_master_ip)r!r�r�r��force_master_iprps` rr zSentinel.__init__�s���� � "���!2�!8�!8�!:�!:����O� /��� � � � �"+� � � ���$7�� �!2��� /����rc��t|�dd����}d|���vr|�d��|r%t j|j��j|i|��n|jD]}|j|i|���dS)z� Execute Sentinel command in sentinel nodes. once - If set to True, then execute the resulting command on a single node at random, rather than across the entire sentinel cluster. �onceFT)rMrn�keysrrc�choicer��execute_command)r!�argsr"r��sentinels rr�zSentinel.execute_command�s��� �F�J�J�v�u�-�-�.�.�� �V�[�[�]�]� "� "� �J�J�v� � � � � :� 9�F�M�$�.� )� )� 9�4� J�6� J� J� J� J� �N� :� :��(��(�$�9�&�9�9�9�9��trc��g}|jD]4}|�d�|jj�����5dt |��j�dt |��j�dd�|���d�S)Nz {host}:{port}r%r&z (sentinels=[�,z])>) r��append� format_maprrpr(rr�join)r!�sentinel_addressesr�s rr/zSentinel.__repr__ s������� � �H� � %� %��*�*�8�+C�+U�V�V� � � � � =��T� � �%� =� =��T� � �(;� =� =��8�8�$6�7�7� =� =� =� rc�`�|dr|ds|drdS|d|jkrdSdS)Nr<�is_sdown�is_odownFznum-other-sentinelsT)r�)r!�stater)s r�check_master_statezSentinel.check_master_statesJ���[�!� �U�:�%6� �%� �:K� ��5� �&� '�$�*B� B� B��5��trc�N�t��}t|j��D]�\}} |���}n8#tt f$r$}|�|�d|����Yd}~�Kd}~wwxYw|�|��}|rX|�||��rB||jdc|jd<|j|<|j �|j n|d}||dfcS��d} t|��dkrdd� |����} td |�| �����) z� Asks sentinel servers for the Redis master's address corresponding to the service labeled ``service_name``. Returns a pair (address, port) or raises MasterNotFoundError if no master is found. z - Nr�ipr+�z : z, zNo master found for ) �list� enumerater��sentinel_mastersr r r�rnr�r�rer�r) r!r)�collected_errors� sentinel_nor��masters�er�r�� error_infos rr_zSentinel.discover_mastersv�� �6�6��%.�t�~�%>�%>� )� )� !�K�� �"�3�3�5�5����#�\�2� � � � �'�'�8�(=�(=��(=�(=�>�>�>���������� �����K�K� �-�-�E�� )��0�0�� �E�E� )���N�1�%�?���q�!�4�>�+�#>��,�8��)�)��t��� �5��=�(�(�(�(��� � �� � �1� $� $�<�t�y�y�)9�:�:�<�<�J�!�"U��"U��"U�"U�V�V�Vs�>�A3�A.�.A3c�|�g}|D]6}|ds|dr�|�|d|df���7|S)z1Remove slaves that are in an ODOWN or SDOWN stater�r�r�r+)r�)r!rg� slaves_aliver?s r� filter_slaveszSentinel.filter_slavesBs\��� �� >� >�E��Z� � �E�*�$5� �� � � ��t��e�F�m� <� =� =� =� =��rc��|jD]Q} |�|��}n#tttf$rY�2wxYw|�|��}|r|cS�RgS)z;Returns a list of alive slaves for service ``service_name``)r��sentinel_slavesr r r r�)r!r)r�rgs rrbzSentinel.discover_slavesKs����� � �H� �!�1�1�,�?�?����#�]�L�A� � � ��� �����'�'��/�/�F�� �� � � � �� s �!�;�;c ��d|d<t|j��}|�|��|�|||fi|����S)a� Returns a redis client instance for the ``service_name`` master. A :py:class:`~redis.sentinel.SentinelConnectionPool` class is used to retrieve the master's address before establishing a new connection. NOTE: If the master's address has changed, any cached connections to the old master are closed. By default clients will be a :py:class:`~redis.Redis` instance. Specify a different class to the ``redis_class`` argument if you desire something different. The ``connection_pool_class`` specifies the connection pool to use. The :py:class:`~redis.sentinel.SentinelConnectionPool` will be used by default. All other keyword arguments are merged with any connection_kwargs passed to this class and passed to the connection pool as keyword arguments to be used to initialize Redis connections. Tr<��dictrp�update� from_pool�r!r)� redis_class�connection_pool_classr"rps r� master_forzSentinel.master_forWsd��:#��{�� ��!7�8�8��� � ��(�(�(��$�$� !� !�,�� J� J�8I� J� J� � � rc ��d|d<t|j��}|�|��|�|||fi|����S)a� Returns redis client instance for the ``service_name`` slave(s). A SentinelConnectionPool class is used to retrieve the slave's address before establishing a new connection. By default clients will be a :py:class:`~redis.Redis` instance. Specify a different class to the ``redis_class`` argument if you desire something different. The ``connection_pool_class`` specifies the connection pool to use. The SentinelConnectionPool will be used by default. All other keyword arguments are merged with any connection_kwargs passed to this class and passed to the connection pool as keyword arguments to be used to initialize Redis connections. Fr<r�r�s r� slave_forzSentinel.slave_for{sd��0$��{�� ��!7�8�8��� � ��(�(�(��$�$� !� !�,�� J� J�8I� J� J� � � r)rNN)rrrr~r r�r/r�r_r�rbrrjr�r�rrrr�r��s���������>��� 0�0�0�0�0���"  �  �  ����!W�!W�!W�F��� � � ��4� " �" �" �" �N�4�  � � � � � rr�)rcrU�typingr� redis.clientr�redis.commandsr�redis.connectionrrr�redis.exceptionsr r r r � redis.utilsr rrrrQrSrjr�rrr�<module>r�s��� � � � �����������������+�+�+�+�+�+�F�F�F�F�F�F�F�F�F�F�X�X�X�X�X�X�X�X�X�X�X�X�$�$�$�$�$�$� � � � � �/� � � � � � � � �� � � �>�>�>�>�>� �>�>�>�B � � � � �#<�m� � � �-N�-N�-N�-N�-N�-N�-N�-N�`:*�:*�:*�:*�:*�^�:*�:*�:*�zR �R �R �R �R ��R �R �R �R �R r
Memory