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.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)r�pool�sZ	host_inforrr�__repr__sz"SentinelManagedConnection.__repr__csD|\|_|_t���|jjr@|�d�t|���dkr@t	d��dS)N�PING�PONGzPING failed)
rrr�connectr�check_connection�send_commandr	�
read_responser)r�address)rrr�
connect_to s

z$SentinelManagedConnection.connect_toc	Csb|jr
dS|jjr$|�|j���n:x4|j��D]&}y
|�|�Stk
rTw0Yq0Xq0Wt�dS)N)�_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'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.
    csZ|�dt�|d<|�dd�|_|�dd�|_t�jf|�t�|�|j	d<||_
||_dS)N�connection_classr)Tr#Fr)�getrrr)r#rr�weakref�proxy�connection_kwargsr�sentinel_manager)rrr5r)rrrrLszSentinelConnectionPool.__init__cCs dt|�j|j|jrdpdfS)Nz%s<service=%s(%s)�masterr,)rrrr))rrrrrVszSentinelConnectionPool.__repr__cst���d|_d|_dS)N)r�reset�master_address�slave_rr_counter)r)rrrr7]s
zSentinelConnectionPool.resetcs8|jp|jo|j|j|jfk}tt|�}|o6|�|�S)N)r)r8rrrr/�owns_connection)r�
connection�check�parent)rrrr:bs

z&SentinelConnectionPool.owns_connectioncCs4|j�|j�}|jr0|j|kr0||_|jdd�|S)NF)�inuse_connections)r5�discover_masterrr)r8r-)rr8rrrr*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)r5�discover_slavesrr9�random�randint�len�ranger*r
r)r�slaves�_r,rrrr+ts


z$SentinelConnectionPool.rotate_slaves)rrr
�__doc__rrr7r:r*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)rK�hostnamer)rrr�
<listcomp>�sz%Sentinel.__init__.<locals>.<listcomp>)�itemsrO�	sentinels�min_other_sentinelsr4)rrSrTrOr4r)rrr�s

zSentinel.__init__cCsNg}x0|jD]&}|�d|jjd|jjdf�qWdt|�jd�|�fS)Nz%s:%srrz%s<sentinels=[%s]>�,)rS�appendrr4rr�join)rZsentinel_addresses�sentinelrrrr�s
zSentinel.__repr__cCs2|dr|ds|drdS|d|jkr.dSdS)Nr)�is_sdown�is_odownFznum-other-sentinelsT)rT)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)�	enumeraterS�sentinel_mastersrrr1r\r
)rrZsentinel_norXZmastersr[rrrr?�s
zSentinel.discover_mastercCs>g}x4|D],}|ds
|dr q
|�|d|df�q
W|S)z1Remove slaves that are in an ODOWN or SDOWN staterZrYr]r)rV)rrFZ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``)rS�sentinel_slavesrrrr`)rrrXrFrrrrA�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)�dictr4�update)rr�redis_class�connection_pool_classrr4rrr�
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)rbr4rc)rrrdrerr4rrr�	slave_fors


zSentinel.slave_for)rN)rrr
rHrrr\r?r`rArr/rfrgrrrrrI�s
	rI)rBr2�redis.clientr�redis.connectionrr�redis.exceptionsrrrr�redis.utilsr	r
rrr/rIrrrr�<module>s1C