HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/redis/__pycache__/sentinel.cpython-310.pyc
o

$we_7�@s�ddlZddlZddlmZddlmZddlmZddlm	Z	m
Z
mZddlm
Z
mZmZmZddlmZGdd	�d	e
�ZGd
d�de
�ZGdd
�d
e	�ZGdd�dee�ZGdd�d�ZGdd�de
�ZGdd�de�ZdS)�N)�Optional)�Redis)�SentinelCommands)�
Connection�ConnectionPool�
SSLConnection)�ConnectionError�
ReadOnlyError�
ResponseError�TimeoutError)�str_if_bytesc@�eZdZdS)�MasterNotFoundErrorN��__name__�
__module__�__qualname__�rr�G/home/arjun/projects/env/lib/python3.10/site-packages/redis/sentinel.pyr�rc@r
)�SlaveNotFoundErrorNrrrrrrrrcsjeZdZ�fdd�Zdd�Z�fdd�Zdd�Zd	d
�Z	dddd�d
ee	dee	f�fdd�Z
�ZS)�SentinelManagedConnectioncs"|�d�|_t�jdi|��dS)N�connection_poolr)�popr�super�__init__)�self�kwargs��	__class__rrrsz"SentinelManagedConnection.__init__cCsD|j}t|�j�d|j�d�}|jr d|j�d|j��}||}|S)N�	<service=z%s>z,host=z,port=)r�typer�service_name�host�port)r�pool�s�	host_inforrr�__repr__sz"SentinelManagedConnection.__repr__csH|\|_|_t���|jjr |�d�t|���dkr"t	d��dSdS)N�PING�PONGzPING failed)
r#r$r�connectr�check_connection�send_commandr�
read_responser)r�addressrrr�
connect_to!s

�z$SentinelManagedConnection.connect_toc	Cs\|jrdS|jjr|�|j���dS|j��D]}z|�|�WSty+Yqwt��N)�_sockr�	is_masterr0�get_master_address�
rotate_slavesrr)r�slaverrr�_connect_retry)s�z(SentinelManagedConnection._connect_retrycCs|j�|jdd��S)NcSsdSr1r)�errorrrr�<lambda>7sz3SentinelManagedConnection.connect.<locals>.<lambda>)�retry�call_with_retryr7�rrrrr+6sz!SentinelManagedConnection.connectF)�disconnect_on_error�push_requestr=r>cs>z
t�j|||d�WSty|jjr|��td���w)N)�disable_decodingr=r>z"The previous master is now a slave)rr.r	rr3�
disconnectr)rr?r=r>rrrr.9s��z'SentinelManagedConnection.read_response)F)rrrrr(r0r7r+r�boolr.�
__classcell__rrrrrs
����rc@r
)�SentinelManagedSSLConnectionNrrrrrrCRrrCc@s,eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�SentinelConnectionPoolProxycCs0t�|�|_||_||_||_||_|��dSr1)�weakref�ref�connection_pool_refr3r,r"�sentinel_manager�reset)rrr3r,r"rHrrrrWsz$SentinelConnectionPoolProxy.__init__cCsd|_d|_dSr1)�master_address�slave_rr_counterr<rrrrIfs
z!SentinelConnectionPoolProxy.resetcCsD|j�|j�}|jr |j|kr ||_|��}|dur |jdd�|S)NF)�inuse_connections)rH�discover_masterr"r3rJrGr@)rrJrrrrr4jsz.SentinelConnectionPoolProxy.get_master_addressccs��|j�|j�}|r5|jdurt�dt|�d�|_tt|��D]}|jdt|�|_||j}|Vq z|��VWn	t	yEYnwt
d|j����)Nr�zNo slave found for )rH�discover_slavesr"rK�random�randint�len�ranger4rr)r�slaves�_r6rrrr5us�

�z)SentinelConnectionPoolProxy.rotate_slavesN)rrrrrIr4r5rrrrrDVs
rDcs\eZdZdZ�fdd�Zdd�Z�fdd�Zedd	��Z�fd
d�Z	dd
�Z
dd�Z�ZS)�SentinelConnectionPoolz�
    Sentinel backed connection pool.

    If ``check_connection`` flag is set to True, SentinelManagedConnection
    sends a PING command right after establishing the connection.
    cs�|�d|�dd�rtnt�|d<|�dd�|_|�dd�|_t||j|j||d�|_t�j	d	i|��|j|j
d<||_||_dS)
N�connection_class�sslFr3Tr,)rr3r,r"rHrr)
�getrrCrr3r,rD�proxyrr�connection_kwargsr"rH)rr"rHrrrrr�s&
���
zSentinelConnectionPool.__init__cCs,|jrdnd}t|�j�d|j�d|�d�S)N�masterr6r �(�))r3r!rr")r�rolerrrr(�szSentinelConnectionPool.__repr__cst���|j��dSr1)rrIrZr<rrrrI�s
zSentinelConnectionPool.resetcCs|jjSr1)rZrJr<rrrrJ�sz%SentinelConnectionPool.master_addresscs4|jp|jo|j|j|jfk}t�}|o|�|�Sr1)r3rJr#r$r�owns_connection)r�
connection�check�parentrrrr`�s
�z&SentinelConnectionPool.owns_connectioncC�
|j��Sr1)rZr4r<rrrr4�s
z)SentinelConnectionPool.get_master_addresscCrd)zRound-robin slave balancer)rZr5r<rrrr5�s
z$SentinelConnectionPool.rotate_slaves)
rrr�__doc__rr(rI�propertyrJr`r4r5rBrrrrrV�s
rVc@sjeZdZdZ		ddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
eefdd�Z
eefdd�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.
    rNcsD|dur
dd�|��D�}|�_�fdd�|D��_|�_|�_dS)NcSs i|]\}}|�d�r||�qS)�socket_)�
startswith)�.0�k�vrrr�
<dictcomp>�s
��z%Sentinel.__init__.<locals>.<dictcomp>cs$g|]\}}t||fi�j���qSr)r�sentinel_kwargs)rj�hostnamer$r<rr�
<listcomp>�s��z%Sentinel.__init__.<locals>.<listcomp>)�itemsrn�	sentinels�min_other_sentinelsr[)rrrrsrnr[rr<rr�s	�
�
zSentinel.__init__cOsft|�dd��}d|��vr|�d�|r#t�|j�j|i|��dS|jD]
}|j|i|��q&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)rArY�keysrrP�choicerr�execute_command)r�argsrrt�sentinelrrrrw�s

�zSentinel.execute_commandcCs@g}|jD]}|�d�|jj��qt|�j�dd�|��d�S)Nz
{host}:{port}z<sentinels=[�,z]>)rr�append�
format_maprr[r!r�join)r�sentinel_addressesryrrrr(s
�zSentinel.__repr__cCs2|dr|ds|drdS|d|jkrdSdS)Nr3�is_sdown�is_odownFznum-other-sentinelsT)rs)r�stater"rrr�check_master_state
s
zSentinel.check_master_statec	Cs�t�}t|j�D]N\}}z|��}Wnttfy0}z|�|�d|���WYd}~qd}~ww|�|�}|rV|�||�rV||jd|jd<|j|<|d|dfSqd}t	|�dkrgdd�
|���}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�	enumeraterr�sentinel_mastersrrr{rYr�rRr}r)	rr"�collected_errors�sentinel_nory�masters�er��
error_inforrrrMs(��
��zSentinel.discover_mastercCs:g}|D]}|ds|drq|�|d|df�q|S)z1Remove slaves that are in an ODOWN or SDOWN stater�rr�r$)r{)rrT�slaves_aliver6rrr�
filter_slaves/szSentinel.filter_slavescCsN|jD]!}z|�|�}WntttfyYqw|�|�}|r$|SqgS)z;Returns a list of alive slaves for service ``service_name``)rr�sentinel_slavesrr
rr�)rr"ryrTrrrrO8s
�
�zSentinel.discover_slavescK�4d|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.
        Tr3��dictr[�update�	from_pool�rr"�redis_class�connection_pool_classrr[rrr�
master_forDs

�zSentinel.master_forcKr�)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.
        Fr3r�r�rrr�	slave_forhs

�zSentinel.slave_for)rN)rrrrerrwr(r�rMr�rOrrVr�r�rrrrrg�s"
�	
�'�rg)rPrE�typingr�redis.clientr�redis.commandsr�redis.connectionrrr�redis.exceptionsrr	r
r�redis.utilsrrrrrCrDrVrgrrrr�<module>s>08