Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/redis/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/redis/__pycache__/sentinel.cpython-37.opt-1.pyc

B

�as-�@s�ddlZddlZddlmZddlmZmZddlmZm	Z	m
Z
mZddlm
Z
Gdd�de�ZGdd	�d	e�ZGd
d�de�ZGdd
�d
e�ZGdd�d�ZdS)�N)�Redis)�ConnectionPool�
Connection)�ConnectionError�
ResponseError�
ReadOnlyError�TimeoutError)�str_if_bytesc@seZdZdS)�MasterNotFoundErrorN)�__name__�
__module__�__qualname__�rr�?/opt/alt/python37/lib/python3.7/site-packages/redis/sentinel.pyr
sr
c@seZdZdS)�SlaveNotFoundErrorN)rrr
rrrrrsrcsDeZdZ�fdd�Zdd�Z�fdd�Zdd�Z�fd	d
�Z�ZS)�SentinelManagedConnectioncs|�d�|_t�jf|�dS)N�connection_pool)�popr�super�__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__csD|\|_|_t���|jjr@|�d�t|���dkr@t	d��dS)NZPINGZPONGzPING failed)
rrr�connectr�check_connectionZsend_commandr	�
read_responser)rZaddress)rrr�
connect_to s

z$SentinelManagedConnection.connect_toc	Csb|jr
dS|jjr$|�|j���n:x4|j��D]&}y
|�|�Stk
rTw0Yq0Xq0Wt�dS)N)Z_sockr�	is_masterr"�get_master_address�
rotate_slavesrr)r�slaverrrr(s
z!SentinelManagedConnection.connectcs>y
t���Stk
r8|jjr2|��td���YnXdS)Nz"The previous master is now a slave)rr!rrr#�
disconnectr)r)rrrr!5s
z'SentinelManagedConnection.read_response)	rrr
rrr"rr!�
__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.
    csZ|�dt�|d<|�dd�|_|�dd�|_t�jf|�t�|�|j	d<||_
||_dS)NZconnection_classr#Tr Fr)�getrrr#r rr�weakref�proxy�connection_kwargsr�sentinel_manager)rrr.r)rrrrLszSentinelConnectionPool.__init__cCs dt|�j|j|jrdpdfS)Nz%s<service=%s(%s)Zmasterr&)rrrr#)rrrrrVszSentinelConnectionPool.__repr__cst���d|_d|_dS)N)r�reset�master_address�slave_rr_counter)r)rrrr/]s
zSentinelConnectionPool.resetcs8|jp|jo|j|j|jfk}tt|�}|o6|�|�S)N)r#r0rrrr)�owns_connection)rZ
connection�check�parent)rrrr2bs

z&SentinelConnectionPool.owns_connectioncCs4|j�|j�}|jr0|j|kr0||_|jdd�|S)NF)Zinuse_connections)r.�discover_masterrr#r0r')rr0rrrr$is
z)SentinelConnectionPool.get_master_addressccs�|j�|j�}|rl|jdkr2t�dt|�d�|_x8tt|��D](}|jdt|�|_||j}|Vq@Wy|��VWnt	k
r�YnXt
d|j��dS)zRound-robin slave balancerNr�zNo slave found for %r)r.�discover_slavesrr1�randomZrandint�len�ranger$r
r)r�slaves�_r&rrrr%ts


z$SentinelConnectionPool.rotate_slaves)rrr
�__doc__rrr/r2r$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�|��D�}|�_�fdd�|D��_|�_|�_dS)NcSs i|]\}}|�d�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>)�itemsrD�	sentinels�min_other_sentinelsr-)rrGrHrDr-r)rrr�s

zSentinel.__init__cCsNg}x0|jD]&}|�d|jjd|jjdf�qWdt|�jd�|�fS)Nz%s:%srrz%s<sentinels=[%s]>�,)rG�appendrr-rr�join)rZsentinel_addresses�sentinelrrrr�s
zSentinel.__repr__cCs2|dr|ds|drdS|d|jkr.dSdS)Nr#�is_sdown�is_odownFznum-other-sentinelsT)rH)r�staterrrr�check_master_state�s
zSentinel.check_master_statec
Cs�x�t|j�D]v\}}y|��}Wnttfk
r:wYnX|�|�}|r|�||�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)�	enumeraterGZsentinel_mastersrrr*rPr
)rrZsentinel_norLZmastersrOrrrr5�s
zSentinel.discover_mastercCs>g}x4|D],}|ds
|dr q
|�|d|df�q
W|S)z1Remove slaves that are in an ODOWN or SDOWN staterNrMrQr)rJ)rr;Zslaves_aliver&rrr�
filter_slaves�s
zSentinel.filter_slavescCsRxL|jD]B}y|�|�}Wntttfk
r6wYnX|�|�}|r|SqWgS)z;Returns a list of alive slaves for service ``service_name``)rGZsentinel_slavesrrrrS)rrrLr;rrrr7�s
zSentinel.discover_slavescKs0d|d<t|j�}|�|�||||f|�d�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#)r)�dictr-�update)rr�redis_class�connection_pool_classrr-rrr�
master_for�s


zSentinel.master_forcKs0d|d<t|j�}|�|�||||f|�d�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)rTr-rU)rrrVrWrr-rrr�	slave_fors


zSentinel.slave_for)rN)rrr
r=rrrPr5rSr7rr)rXrYrrrrr>�s
	r>)r8r+Zredis.clientrZredis.connectionrrZredis.exceptionsrrrrZredis.utilsr	r
rrr)r>rrrr�<module>s1C