o
    Š¨ÊhE9  ã                   @   sà   d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
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ƒZG dd„ de
ƒZG dd„ deƒZdS )é    N)ÚOptional)ÚRedis)ÚSentinelCommands)Ú
ConnectionÚConnectionPoolÚSSLConnection)ÚConnectionErrorÚReadOnlyErrorÚResponseErrorÚTimeoutError)Ústr_if_bytesc                   @   ó   e Zd ZdS )ÚMasterNotFoundErrorN©Ú__name__Ú
__module__Ú__qualname__© r   r   ú@/var/www/html/env/lib/python3.10/site-packages/redis/sentinel.pyr      ó    r   c                   @   r   )ÚSlaveNotFoundErrorNr   r   r   r   r   r      r   r   c                       sj   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œdee	 dee	 f‡ fdd„Z
‡  ZS )ÚSentinelManagedConnectionc                    s"   |  d¡| _tƒ jdi |¤Ž d S )NÚconnection_poolr   )Úpopr   ÚsuperÚ__init__)ÚselfÚkwargs©Ú	__class__r   r   r      s   z"SentinelManagedConnection.__init__c                 C   sR   | j }dt| ƒj› dt| ƒj› d|j› d�}| jr'd| j› d| j› �}|| }|S )Nú<Ú.ú	(service=z%s)>z,host=z,port=)r   Útyper   r   Úservice_nameÚhostÚport)r   ÚpoolÚsÚ	host_infor   r   r   Ú__repr__   s   ÿÿz"SentinelManagedConnection.__repr__c                    sH   |\| _ | _tƒ  ¡  | jjr |  d¡ t|  ¡ ƒdkr"t	dƒ‚d S d S )NÚPINGÚPONGzPING failed)
r%   r&   r   Úconnectr   Úcheck_connectionÚsend_commandr   Úread_responser   )r   Úaddressr   r   r   Ú
connect_to$   s   

ýz$SentinelManagedConnection.connect_toc              	   C   s\   | j rd S | jjr|  | j ¡ ¡ d S | j ¡ D ]}z|  |¡W   S  ty+   Y qw t‚©N)Ú_sockr   Ú	is_masterr2   Úget_master_addressÚrotate_slavesr   r   )r   Úslaver   r   r   Ú_connect_retry,   s   ÿz(SentinelManagedConnection._connect_retryc                 C   s   | j  | jdd„ ¡S )Nc                 S   s   d S r3   r   )Úerrorr   r   r   Ú<lambda>:   s    z3SentinelManagedConnection.connect.<locals>.<lambda>)ÚretryÚcall_with_retryr9   ©r   r   r   r   r-   9   s   z!SentinelManagedConnection.connectF)Údisconnect_on_errorÚpush_requestr?   r@   c                   s>   z
t ƒ j|||d�W S  ty   | jjr|  ¡  tdƒ‚‚ w )N)Údisable_decodingr?   r@   z"The previous master is now a slave)r   r0   r	   r   r5   Ú
disconnectr   )r   rA   r?   r@   r   r   r   r0   <   s   ý÷z'SentinelManagedConnection.read_response)F)r   r   r   r   r*   r2   r9   r-   r   Úboolr0   Ú__classcell__r   r   r   r   r      s    þûüûr   c                   @   r   )ÚSentinelManagedSSLConnectionNr   r   r   r   r   rE   U   r   rE   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚSentinelConnectionPoolProxyc                 C   s0   t  |¡| _|| _|| _|| _|| _|  ¡  d S r3   )ÚweakrefÚrefÚconnection_pool_refr5   r.   r$   Úsentinel_managerÚreset)r   r   r5   r.   r$   rJ   r   r   r   r   Z   s   z$SentinelConnectionPoolProxy.__init__c                 C   s   d | _ d | _d S r3   )Úmaster_addressÚslave_rr_counterr>   r   r   r   rK   i   s   
z!SentinelConnectionPoolProxy.resetc                 C   sD   | j  | j¡}| jr | j|kr || _|  ¡ }|d ur |jdd� |S )NF)Úinuse_connections)rJ   Údiscover_masterr$   r5   rL   rI   rB   )r   rL   r   r   r   r   r6   m   s   z.SentinelConnectionPoolProxy.get_master_addressc                 c   sœ   � | j  | j¡}|r5| jd u rt dt|ƒd ¡| _tt|ƒƒD ]}| jd t|ƒ | _|| j }|V  q z|  ¡ V  W n	 t	yE   Y nw t
d| j›�ƒ‚)Nr   é   zNo slave found for )rJ   Údiscover_slavesr$   rM   ÚrandomÚrandintÚlenÚranger6   r   r   )r   ÚslavesÚ_r8   r   r   r   r7   x   s   €

ÿz)SentinelConnectionPoolProxy.rotate_slavesN)r   r   r   r   rK   r6   r7   r   r   r   r   rF   Y   s
    rF   c                       s\   e Zd ZdZ‡ fdd„Zdd„ Z‡ fdd„Zedd	„ ƒZ‡ fd
d„Z	dd„ Z
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                    s‚   |  d| dd¡rtnt¡|d< | dd¡| _| dd¡| _t| | j| j||d�| _tƒ j	d	i |¤Ž | j| j
d< || _|| _d S )
NÚconnection_classÚsslFr5   Tr.   )r   r5   r.   r$   rJ   r   r   )Úgetr   rE   r   r5   r.   rF   Úproxyr   r   Úconnection_kwargsr$   rJ   )r   r$   rJ   r   r   r   r   r   ‘   s&   
ÿûû
zSentinelConnectionPool.__init__c              	   C   s:   | j rdnd}dt| ƒj› dt| ƒj› d| j› d|› d�	S )NÚmasterr8   r    r!   r"   ú(z))>)r5   r#   r   r   r$   )r   Úroler   r   r   r*   ¨   s   ÿÿÿzSentinelConnectionPool.__repr__c                    s   t ƒ  ¡  | j ¡  d S r3   )r   rK   r\   r>   r   r   r   rK   ¯   s   
zSentinelConnectionPool.resetc                 C   s   | j jS r3   )r\   rL   r>   r   r   r   rL   ³   s   z%SentinelConnectionPool.master_addressc                    s4   | j  p| j o| j|j|jfk}tƒ }|o| |¡S r3   )r5   rL   r%   r&   r   Úowns_connection)r   Ú
connectionÚcheckÚparentr   r   r   ra   ·   s
   ÿz&SentinelConnectionPool.owns_connectionc                 C   ó
   | j  ¡ S r3   )r\   r6   r>   r   r   r   r6   ¾   s   
z)SentinelConnectionPool.get_master_addressc                 C   re   )zRound-robin slave balancer)r\   r7   r>   r   r   r   r7   Á   s   
z$SentinelConnectionPool.rotate_slaves)r   r   r   Ú__doc__r   r*   rK   ÚpropertyrL   ra   r6   r7   rD   r   r   r   r   rX   ‰   s    
rX   c                   @   sl   e Zd ZdZ			ddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
eefdd„Zeefdd„ZdS )Ú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.
    r   Nc                    sJ   |d u rdd„ |  ¡ D ƒ}|ˆ _‡ fdd„|D ƒˆ _|ˆ _|ˆ _|ˆ _d S )Nc                 S   s    i | ]\}}|  d ¡r||“qS )Úsocket_)Ú
startswith)Ú.0ÚkÚvr   r   r   Ú
<dictcomp>î   s
    ÿÿz%Sentinel.__init__.<locals>.<dictcomp>c                    s$   g | ]\}}t ||fi ˆ j¤Ž‘qS r   )r   Úsentinel_kwargs)rk   Úhostnamer&   r>   r   r   Ú
<listcomp>ó   s    ÿÿz%Sentinel.__init__.<locals>.<listcomp>)Úitemsro   Ú	sentinelsÚmin_other_sentinelsr]   Ú_force_master_ip)r   rs   rt   ro   Úforce_master_ipr]   r   r>   r   r   ã   s   
ÿ
þ
zSentinel.__init__c                 O   sf   t | dd¡ƒ}d| ¡ v r| d¡ |r#t | j¡j|i |¤Ž dS | jD ]
}|j|i |¤Ž q&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.
        ÚonceFT)rC   r[   Úkeysr   rR   Úchoicers   Úexecute_command)r   Úargsr   rw   Úsentinelr   r   r   rz   û   s   

þzSentinel.execute_commandc                 C   sN   g }| j D ]}| d |jj¡¡ qdt| ƒj› dt| ƒj› dd |¡› d�S )Nz{host}:{port}r    r!   z(sentinels=[ú,z])>)	rs   ÚappendÚ
format_mapr   r]   r#   r   r   Újoin)r   Úsentinel_addressesr|   r   r   r   r*     s   
ÿÿÿzSentinel.__repr__c                 C   s2   |d r|d s|d rdS |d | j k rdS dS )Nr5   Úis_sdownÚis_odownFznum-other-sentinelsT)rt   )r   Ústater$   r   r   r   Úcheck_master_state  s
   zSentinel.check_master_statec           
      C   sô   t ƒ }t| jƒD ]X\}}z| ¡ }W n ttfy0 } z| |› d|›�¡ W Y d}~qd}~ww | |¡}|r`|  ||¡r`|| jd | jd< | j|< | j	durT| j	n|d }||d f  S qd}	t
|ƒdkrqdd |¡› �}	td	|›|	› �ƒ‚)
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, zNo master found for )ÚlistÚ	enumeraters   Úsentinel_mastersr   r   r~   r[   r…   ru   rT   r€   r   )
r   r$   Úcollected_errorsÚsentinel_nor|   ÚmastersÚer„   r†   Ú
error_infor   r   r   rO     s0   €þ
þ
ÿý€zSentinel.discover_masterc                 C   s:   g }|D ]}|d s|d rq|  |d |d f¡ q|S )z1Remove slaves that are in an ODOWN or SDOWN staterƒ   r‚   r†   r&   )r~   )r   rV   Úslaves_aliver8   r   r   r   Úfilter_slavesB  s   zSentinel.filter_slavesc                 C   sN   | j D ]!}z| |¡}W n tttfy   Y qw |  |¡}|r$|  S qg S )z;Returns a list of alive slaves for service ``service_name``)rs   Úsentinel_slavesr   r
   r   r‘   )r   r$   r|   rV   r   r   r   rQ   K  s   
ÿ
ÿzSentinel.discover_slavesc                 K   ó4   d|d< t | jƒ}| |¡ | ||| fi |¤Ž¡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.
        Tr5   ©Údictr]   ÚupdateÚ	from_pool©r   r$   Úredis_classÚconnection_pool_classr   r]   r   r   r   Ú
master_forW  s   

ÿzSentinel.master_forc                 K   r“   )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.
        Fr5   r”   r˜   r   r   r   Ú	slave_for{  s   

ÿzSentinel.slave_for)r   NN)r   r   r   rf   r   rz   r*   r…   rO   r‘   rQ   r   rX   r›   rœ   r   r   r   r   rh   Æ   s$    
û#	
ü'ürh   )rR   rG   Útypingr   Úredis.clientr   Úredis.commandsr   Úredis.connectionr   r   r   Úredis.exceptionsr   r	   r
   r   Úredis.utilsr   r   r   r   rE   rF   rX   rh   r   r   r   r   Ú<module>   s    A0=