HEX
Server: LiteSpeed
System: Linux linux31.centraldnserver.com 4.18.0-553.83.1.lve.el8.x86_64 #1 SMP Wed Nov 12 10:04:12 UTC 2025 x86_64
User: salamatk (1501)
PHP: 8.1.33
Disabled: show_source, system, shell_exec, passthru, exec, popen, proc_open
Upload Files
File: //lib/python3.6/site-packages/redis/__pycache__/sentinel.cpython-36.opt-1.pyc
3

be�^�-�@s�ddlZddlZddlmZddlmZmZddlmZm	Z	m
Z
mZddlm
Z
mZmZGdd�de�ZGdd	�d	e�ZGd
d�de�ZGdd
�d
e�ZGdd�de�ZdS)�N)�Redis)�ConnectionPool�
Connection)�ConnectionError�
ResponseError�
ReadOnlyError�TimeoutError)�	iteritems�	nativestr�xrangec@seZdZdS)�MasterNotFoundErrorN)�__name__�
__module__�__qualname__�rr�/usr/lib/python3.6/sentinel.pyrsrc@seZdZdS)�SlaveNotFoundErrorN)r
rrrrrrrsrcsDeZdZ�fdd�Zdd�Z�fdd�Zdd�Z�fd	d
�Z�ZS)�SentinelManagedConnectioncs"|jd�|_tt|�jf|�dS)N�connection_pool)�popr�superr�__init__)�self�kwargs)�	__class__rrrsz"SentinelManagedConnection.__init__cCs<|j}dt|�j|jf}|jr8d|j|jf}||}|S)Nz%s<service=%s%%s>z,host=%s,port=%s)r�typer
�service_name�host�port)rZpool�sZ	host_inforrr�__repr__sz"SentinelManagedConnection.__repr__csH|\|_|_tt|�j�|jjrD|jd�t|j	��dkrDt
d��dS)NZPINGZPONGzPING failed)rrrr�connectr�check_connectionZsend_commandr
�
read_responser)rZaddress)rrr�
connect_to s
z$SentinelManagedConnection.connect_tocCsb|jr
dS|jjr$|j|jj��n:x4|jj�D]&}y
|j|�Stk
rTw0Yq0Xq0Wt�dS)N)Z_sockr�	is_masterr$�get_master_address�
rotate_slavesrr)r�slaverrrr!(s
z!SentinelManagedConnection.connectcsBytt|�j�Stk
r<|jjr6|j�td���YnXdS)Nz"The previous master is now a slave)rrr#rrr%�
disconnectr)r)rrrr#5sz'SentinelManagedConnection.read_response)	r
rrrr r$r!r#�
__classcell__rr)rrrs

rcsPeZdZdZ�fdd�Zdd�Z�fdd�Z�fdd	�Zd
d�Zdd
�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^|jdt�|d<|jdd�|_|jdd�|_tt|�jf|�tj	|�|j
d<||_||_dS)NZconnection_classr%Tr"Fr)
�getrrr%r"rr+r�weakref�proxy�connection_kwargsr�sentinel_manager)rrr0r)rrrrLszSentinelConnectionPool.__init__cCs dt|�j|j|jrdpdfS)Nz%s<service=%s(%s)Zmasterr()rr
rr%)rrrrr VszSentinelConnectionPool.__repr__cstt|�j�d|_d|_dS)N)rr+�reset�master_address�slave_rr_counter)r)rrrr1]szSentinelConnectionPool.resetcs8|jp|jo|j|j|jfk}tt|�}|o6|j|�S)N)r%r2rrrr+�owns_connection)rZ
connectionZcheck�parent)rrrr4bs

z&SentinelConnectionPool.owns_connectioncCs4|jj|j�}|jr0|j|kr0||_|jdd�|S)NF)Zinuse_connections)r0�discover_masterrr%r2r))rr2rrrr&is
z)SentinelConnectionPool.get_master_addressccs�|jj|j�}|rl|jdkr2tjdt|�d�|_x8tt|��D](}|jdt|�|_||j}|Vq@Wy|j�VWnt	k
r�YnXt
d|j��dS)zRound-robin slave balancerNr�zNo slave found for %r)r0�discover_slavesrr3�randomZrandint�lenrr&rr)r�slaves�_r(rrrr'ts


z$SentinelConnectionPool.rotate_slaves)r
rr�__doc__rr r1r4r&r'r*rr)rrr+Ds
r+c@s^eZdZdZddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Z	e
efdd�Ze
efdd�Z
dS)�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|dkrdd�t|�D�}|�_�fdd�|D��_|�_|�_dS)NcSs i|]\}}|jd�r||�qS)Zsocket_)�
startswith)�.0�k�vrrr�
<dictcomp>�sz%Sentinel.__init__.<locals>.<dictcomp>cs g|]\}}t||f�j��qSr)r�sentinel_kwargs)r@Zhostnamer)rrr�
<listcomp>�sz%Sentinel.__init__.<locals>.<listcomp>)r	rD�	sentinels�min_other_sentinelsr/)rrFrGrDr/r)rrr�s

zSentinel.__init__cCsNg}x0|jD]&}|jd|jjd|jjdf�qWdt|�jdj|�fS)Nz%s:%srrz%s<sentinels=[%s]>�,)rF�appendrr/rr
�join)rZsentinel_addresses�sentinelrrrr �s
zSentinel.__repr__cCs4|ds|ds|drdS|d|jkr0dSdS)Nr%�is_sdown�is_odownFznum-other-sentinelsT)rG)r�staterrrr�check_master_state�s
zSentinel.check_master_statec
Cs�x�t|j�D]v\}}y|j�}Wnttfk
r:wYnX|j|�}|r|j||�r||jd|jd<|j|<|d|dfSqWtd|f��dS)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.
        r�iprzNo master found for %rN)�	enumeraterFZsentinel_mastersrrr,rOr)rrZsentinel_norKZmastersrNrrrr6�s
zSentinel.discover_mastercCs>g}x4|D],}|ds
|dr q
|j|d|df�q
W|S)z1Remove slaves that are in an ODOWN or SDOWN staterMrLrPr)rI)rr;Zslaves_aliver(rrr�
filter_slaves�s
zSentinel.filter_slavescCsRxL|jD]B}y|j|�}Wntttfk
r6wYnX|j|�}|r|SqWgS)z;Returns a list of alive slaves for service ``service_name``)rFZsentinel_slavesrrrrR)rrrKr;rrrr8�s
zSentinel.discover_slavescKs0d|d<t|j�}|j|�||||f|�d�S)aF
        Returns a redis client instance for the ``service_name`` master.

        A SentinelConnectionPool class is used to retrive 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 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.
        Tr%)r)�dictr/�update)rr�redis_class�connection_pool_classrr/rrr�
master_for�s


zSentinel.master_forcKs0d|d<t|j�}|j|�||||f|�d�S)a�
        Returns redis client instance for the ``service_name`` slave(s).

        A SentinelConnectionPool class is used to retrive the slave's
        address before establishing a new connection.

        By default clients will be a 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)rSr/rT)rrrUrVrr/rrr�	slave_fors


zSentinel.slave_for)rN)r
rrr=rr rOr6rRr8rr+rWrXrrrrr>�s
	r>)r9r-Zredis.clientrZredis.connectionrrZredis.exceptionsrrrrZ
redis._compatr	r
rrrrr+�objectr>rrrr�<module>s1C