U
    g7                     @   s   d dl Z d dlZd dlZd dlmZmZmZmZmZm	Z	m
Z
 d dlmZ d dlmZmZmZmZ d dlmZ d dlmZmZmZmZ d dlmZ G dd	 d	eZG d
d deZG dd deZG dd deeZG dd deZG dd deZ dS )    N)AsyncIteratorIterableMappingOptionalSequenceTupleType)Redis)
ConnectionConnectionPool
EncodableTSSLConnection)AsyncSentinelCommands)ConnectionErrorReadOnlyErrorResponseErrorTimeoutError)str_if_bytesc                   @   s   e Zd ZdS )MasterNotFoundErrorN__name__
__module____qualname__ r   r   :/tmp/pip-unpacked-wheel-f3sx1i9r/redis/asyncio/sentinel.pyr      s   r   c                   @   s   e Zd ZdS )SlaveNotFoundErrorNr   r   r   r   r   r      s   r   c                       sn   e Zd Z fddZdd Z fddZdd Zd	d
 Zddddee	e
 e	e
 e	e d fddZ  ZS )SentinelManagedConnectionc                    s   | d| _t jf | d S )Nconnection_pool)popr   super__init__)selfkwargs	__class__r   r   r       s    z"SentinelManagedConnection.__init__c                 C   sB   d| j j d| j j }| jr:d| j d| j }||7 }|d S )N<.z,host=z,port=z)>)r$   r   r   hostport)r!   sZ	host_infor   r   r   __repr__   s
    z"SentinelManagedConnection.__repr__c                    sV   |\| _ | _t  I d H  | jjrR| dI d H  t|  I d H dkrRt	dd S )NZPINGZPONGzPING failed)
r'   r(   r   connectr   check_connectionZsend_commandr   read_responser   )r!   addressr#   r   r   
connect_to&   s    z$SentinelManagedConnection.connect_toc              	      s   | j r
d S | jjr0| | j I d H I d H  nN| j 2 z<3 d H W }z| |I d H W   S  tk
rt   Y q:Y q:X q:6 td S N)Z_readerr   	is_masterr/   get_master_addressrotate_slavesr   r   )r!   slaver   r   r   _connect_retry.   s    z(SentinelManagedConnection._connect_retryc                    s   | j | jdd I d H S )Nc                 S   s
   t dS )Nr   )asynciosleep)errorr   r   r   <lambda>>       z3SentinelManagedConnection.connect.<locals>.<lambda>)retryZcall_with_retryr5   r!   r   r   r   r+   ;   s    z!SentinelManagedConnection.connectFNT)disconnect_on_errorpush_requestdisable_decodingtimeoutr=   r>   c                   sV   zt  j||||dI d H W S  tk
rP   | jjrJ|  I d H  td Y nX d S )Nr?   z"The previous master is now a slave)r   r-   r   r   r1   
disconnectr   )r!   r@   rA   r=   r>   r#   r   r   r-   A   s    z'SentinelManagedConnection.read_response)FN)r   r   r   r    r*   r/   r5   r+   boolr   floatr-   __classcell__r   r   r#   r   r      s      r   c                   @   s   e Zd ZdS )SentinelManagedSSLConnectionNr   r   r   r   r   rF   \   s   rF   c                       s\   e Zd ZdZ fddZdd Z fddZed fd	d
Zdd Z	e
dd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.
    c                    sv   | d|ddrtnt|d< |dd| _|dd| _t jf | t	| | j
d< || _|| _d | _d | _d S )NZconnection_classsslFr1   Tr,   r   )getr   rF   r   r1   r,   r   r    weakrefproxyconnection_kwargsservice_namesentinel_managermaster_addressslave_rr_counter)r!   rM   rN   r"   r#   r   r   r    h   s    
zSentinelConnectionPool.__init__c              	   C   s2   d| j j d| j j d| j d| jr(dp*d d	S )Nr%   r&   z	(service=(Zmasterr4   z))>)r$   r   r   rM   r1   r<   r   r   r   r*   z   s    0zSentinelConnectionPool.__repr__c                    s   t    d | _d | _d S r0   )r   resetrO   rP   r<   r#   r   r   rR      s    
zSentinelConnectionPool.reset)
connectionc                    s0   | j  p| j o| j|j|jfk}|o.t |S r0   )r1   rO   r'   r(   r   owns_connection)r!   rS   checkr#   r   r   rT      s    z&SentinelConnectionPool.owns_connectionc                    s@   | j | jI d H }| jr<| j|kr<|| _| jddI d H  |S )NF)Zinuse_connections)rN   discover_masterrM   r1   rO   rB   )r!   rO   r   r   r   r2      s    
z)SentinelConnectionPool.get_master_address)returnc                 C  s   | j | jI dH }|rn| jdkr8tdt|d | _tt|D ](}| jd t| | _|| j }|V  qDz|  I dH V  W n t	k
r   Y nX t
d| jdS )zRound-robin slave balancerNr      zNo slave found for )rN   discover_slavesrM   rP   randomrandintlenranger2   r   r   )r!   slaves_r4   r   r   r   r3      s    

z$SentinelConnectionPool.rotate_slaves)r   r   r   __doc__r    r*   rR   r
   rT   r2   r   r3   rE   r   r   r#   r   rG   `   s   
rG   c                   @   s   e Zd ZdZdddZdd Zdd	 Zeee	d
ddZ
edddZee eeeef  dddZeeeeef  dddZeefeee ee dddZeefeee ee dddZdS )Sentinela~  
    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)
    >>> await master.set('foo', 'bar')
    >>> slave = sentinel.slave_for('mymaster', socket_timeout=0.1)
    >>> await 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.
    r   Nc                    sD   |d krdd |  D }| _ fdd|D  _| _| _d S )Nc                 S   s    i | ]\}}| d r||qS )Zsocket_)
startswith).0kvr   r   r   
<dictcomp>   s    
  z%Sentinel.__init__.<locals>.<dictcomp>c                    s&   g | ]\}}t f ||d  jqS ))r'   r(   )r	   sentinel_kwargs)rc   hostnamer(   r<   r   r   
<listcomp>   s   z%Sentinel.__init__.<locals>.<listcomp>)itemsrg   	sentinelsmin_other_sentinelsrL   )r!   rk   rl   rg   rL   r   r<   r   r       s    	
zSentinel.__init__c                    sp   t dd}d kr&d |rFt| jj I dH  n& fdd| jD }tj	| I dH  dS )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.
        onceFNc                    s   g | ]}t |j qS r   )r6   ZTaskexecute_command)rc   sentinelargsr"   r   r   ri      s   z,Sentinel.execute_command.<locals>.<listcomp>T)
rC   rI   keysr   rZ   choicerk   rn   r6   Zgather)r!   rq   r"   rm   Ztasksr   rp   r   rn      s    
zSentinel.execute_commandc                 C   sX   g }| j D ](}||jjd  d|jjd   q
d| j d| jj dd| dS )	Nr'   :r(   r%   r&   z(sentinels=[,z])>)rk   appendr   rL   r$   r   join)r!   Zsentinel_addressesro   r   r   r   r*      s    
"zSentinel.__repr__)staterM   rW   c                 C   s2   |d r|d s|d rdS |d | j k r.dS dS )Nr1   is_sdownis_odownFznum-other-sentinelsT)rl   )r!   rx   rM   r   r   r   check_master_state   s
    zSentinel.check_master_state)rM   c           	         s   t  }t| jD ]\}}z| I dH }W nB ttfk
rl } z || d| W Y qW 5 d}~X Y nX ||}|r| ||r|| jd  | jd< | j|< |d |d f  S qd}t	|dkrdd
| }td	|| dS )
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.
        Nz - r   ipr(    z : z, zNo master found for )list	enumeraterk   Zsentinel_mastersr   r   rv   rI   r{   r\   rw   r   )	r!   rM   Zcollected_errorsZsentinel_noro   Zmasterserx   
error_infor   r   r   rV     s"    
zSentinel.discover_master)r^   rW   c                 C   s:   g }|D ],}|d s|d rq| |d |d f q|S )z1Remove slaves that are in an ODOWN or SDOWN staterz   ry   r|   r(   )rv   )r!   r^   Zslaves_aliver4   r   r   r   filter_slaves  s    zSentinel.filter_slaves)rM   rW   c                    sZ   | j D ]N}z||I dH }W n tttfk
r<   Y qY nX | |}|r|  S qg S )z;Returns a list of alive slaves for service ``service_name``N)rk   Zsentinel_slavesr   r   r   r   )r!   rM   ro   r^   r   r   r   rY   *  s    



zSentinel.discover_slaves)rM   redis_classconnection_pool_classc                 K   s4   d|d< t | j}|| ||| f|}||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.
        Tr1   dictrL   updateZ	from_poolr!   rM   r   r   r"   rL   r   r   r   r   
master_for8  s
    

zSentinel.master_forc                 K   s4   d|d< t | j}|| ||| f|}||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.
        Fr1   r   r   r   r   r   	slave_for]  s
    

zSentinel.slave_for)r   N)r   r   r   r`   r    rn   r*   r   strrC   r{   rV   r   r   r   r   r   r   rY   r	   rG   r   r   r   r   r   r   r   ra      s8     
(ra   )!r6   rZ   rJ   typingr   r   r   r   r   r   r   Zredis.asyncio.clientr	   Zredis.asyncio.connectionr
   r   r   r   Zredis.commandsr   Zredis.exceptionsr   r   r   r   Zredis.utilsr   r   r   r   rF   rG   ra   r   r   r   r   <module>   s   $BG