
     hR1                        d dl Z d dl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e          Z G d	 d
e          Z G d de          Z G d dee	          Z G d de          Z G d de          ZdS )    N)Redis)SentinelCommands)
ConnectionConnectionPoolSSLConnection)ConnectionErrorReadOnlyErrorResponseErrorTimeoutError)str_if_bytesc                       e Zd ZdS )MasterNotFoundErrorN__name__
__module____qualname__     J/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/redis/sentinel.pyr   r              Dr   r   c                       e Zd ZdS )SlaveNotFoundErrorNr   r   r   r   r   r      r   r   r   c                   F     e Zd Z fdZd Z fdZd Zd Zd fd	Z xZ	S )	SentinelManagedConnectionc                 n    |                     d          | _         t                      j        di | d S )Nconnection_poolr   )popr   super__init__)selfkwargs	__class__s     r   r   z"SentinelManagedConnection.__init__   s;    %zz*;<<""6"""""r   c                     | j         }t          |           j         d|j         d}| j        rd| j         d| j         }||z  }|S )N	<service=z%s>z,host=z,port=)r   typer   service_namehostport)r    pools	host_infos       r   __repr__z"SentinelManagedConnection.__repr__   s]    #Dzz"CCT->CCC9 	===$)==IIAr   c                    |\  | _         | _        t                                                       | j        j        rI|                     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"   s     r   
connect_toz$SentinelManagedConnection.connect_to    s    &	490 	5f%%%D..0011V;;%m444	5 	5;;r   c                    | j         rd S | j        j        r.|                     | j                                                   d S | j                                        D ])}	 |                     |          c S # t          $ r Y &w xY wt          N)_sockr   	is_masterr5   get_master_addressrotate_slavesr   r   )r    slaves     r   _connect_retryz(SentinelManagedConnection._connect_retry(   s    : 	F) 	%OOD0CCEEFFFFF-;;==  ??511111&   H$$s    A77
BBc                 D    | j                             | j        d           S )Nc                     d S r7   r   )errors    r   <lambda>z3SentinelManagedConnection.connect.<locals>.<lambda>6   s    T r   )retrycall_with_retryr=   )r    s    r   r0   z!SentinelManagedConnection.connect5   s!    z))$*=?Q?QRRRr   Fc                     	 t                                          |          S # t          $ r1 | j        j        r#|                                  t          d           w xY w)N)disable_decodingz"The previous master is now a slave)r   r3   r	   r   r9   
disconnectr   )r    rE   r"   s     r   r3   z'SentinelManagedConnection.read_response8   sr    	77((:J(KKK 		 		 		#- L !!!%&JKKK		s	   !% ;A )F)
r   r   r   r   r,   r5   r=   r0   r3   __classcell__r"   s   @r   r   r      s        # # # # #  5 5 5 5 5% % %S S S         r   r   c                       e Zd ZdS )SentinelManagedSSLConnectionNr   r   r   r   rJ   rJ   G   r   r   rJ   c                   H     e Zd ZdZ fdZd Z fdZ fdZd Zd Z	 x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                 p   |                     d|                    dd          rt          nt                    |d<   |                    dd          | _        |                    dd          | _         t                      j        di | t          j	        |           | j
        d<   || _        || _        d S )	Nconnection_classsslFr9   Tr1   r   r   )getr   rJ   r   r9   r1   r   r   weakrefproxyconnection_kwargsr&   sentinel_manager)r    r&   rT   r!   r"   s       r   r   zSentinelConnectionPool.__init__S   s    %+ZZzz%''+((*	&
 &
!"  K66 &

+=u E E""6"""4;M$4G4G01( 0r   c                 \    | j         rdnd}t          |           j         d| j         d| dS )Nmasterr<   r$   ())r9   r%   r   r&   )r    roles     r   r,   zSentinelConnectionPool.__repr__a   s=    >6xxwt**%KK0AKKDKKKKr   c                 d    t                                                       d | _        d | _        d S r7   )r   resetmaster_addressslave_rr_counter)r    r"   s    r   r[   zSentinelConnectionPool.resete   s*    " $r   c                     | j          p| j         o| j        |j        |j        fk    }t	                      }|o|                    |          S r7   )r9   r\   r'   r(   r   owns_connection)r    
connectioncheckparentr"   s       r   r_   z&SentinelConnectionPool.owns_connectionj   sT    N" 
NXt2z
6XX 	 ;//
;;;r   c                     | j                             | j                  }| j        r(| j        |k    r|| _        |                     d           |S )NF)inuse_connections)rT   discover_masterr&   r9   r\   rF   )r    r\   s     r   r:   z)SentinelConnectionPool.get_master_addressq   sW    .>>t?PQQ> 	9"n44&4# %888r   c              #     K   | j                             | j                  }|r| j        *t	          j        dt          |          dz
            | _        t          t          |                    D ]2}| j        dz   t          |          z  | _        || j                 }|V  3	 |                                 V  n# t          $ r Y nw xY wt          d| j                  )zRound-robin slave balancerNr      zNo slave found for )rT   discover_slavesr&   r]   randomrandintlenranger:   r   r   )r    slaves_r<   s       r   r;   z$SentinelConnectionPool.rotate_slaves{   s      &66t7HII 	$,(.q#f++/(J(J%3v;;''  )-)>)Bc&kk(Q%t45	))++++++" 	 	 	D	 !Lt7H!L!LMMMs   %B< <
C	C	)r   r   r   __doc__r   r,   r[   r_   r:   r;   rG   rH   s   @r   rL   rL   K   s         1 1 1 1 1L L L% % % % %
< < < < <  N N N N N N Nr   rL   c                   Z    e Zd ZdZ	 	 ddZd Zd Zd Zd Zd	 Z	d
 Z
eefdZeef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                      |d |                                 D             }| _         fd|D              _        | _        | _        d S )Nc                 D    i | ]\  }}|                     d           ||S )socket_)
startswith).0kvs      r   
<dictcomp>z%Sentinel.__init__.<locals>.<dictcomp>   s?       Aall9>U>U1  r   c                 <    g | ]\  }}t          ||fi j        S r   )r   sentinel_kwargs)rv   hostnamer(   r    s      r   
<listcomp>z%Sentinel.__init__.<locals>.<listcomp>   sB     
 
 
$ (D99D$899
 
 
r   )itemsr{   	sentinelsmin_other_sentinelsrS   )r    r   r   r{   rS   s   `    r   r   zSentinel.__init__   s     " !2!8!8!:!:  O  /
 
 
 
"+
 
 
 $7 !2r   c                    t          |                    dd                    }d|                                v r|                    d           |r% t	          j        | j                  j        |i | n| j        D ]} |j        |i | 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)boolrP   keysr   ri   choicer   execute_command)r    argsr!   r   sentinels        r   r   zSentinel.execute_command   s     FJJvu--..V[[]]""JJv 	:9FM$.))94J6JJJJ N : :(($9&9999tr   c                     g }| j         D ]4}|                    d                    |j        j                             5t          |           j         dd                    |           dS )Nz{host}:{port}z<sentinels=[,z]>)r   append
format_mapr   rS   r%   r   join)r    sentinel_addressesr   s      r   r,   zSentinel.__repr__   s|     	 	H%%**8+C+UVV    t**%SS388<N3O3OSSSSr   c                 `    |d         r|d         s|d         rdS |d         | j         k     rdS dS )Nr9   is_sdownis_odownFznum-other-sentinelsT)r   )r    stater&   s      r   check_master_statezSentinel.check_master_state   sJ    [! 	U:%6 	%
:K 	5&'$*BBB5tr   c                 r   t          | j                  D ]\  }}	 |                                }n# t          t          f$ r Y .w xY w|                    |          }|rH|                     ||          r2|| j        d         c| j        d<   | j        |<   |d         |d         fc S 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.
        r   ipr(   zNo master found for )	enumerater   sentinel_mastersr   r   rP   r   r   )r    r&   sentinel_nor   mastersr   s         r   re   zSentinel.discover_master   s     &/t~%>%> 	2 	2!K"3355#\2   KK--E 200EE 2 N1% ?q!4>+#> T{E&M1111!"I"I"IJJJs   0AAc                 |    g }|D ]6}|d         s|d         r|                     |d         |d         f           7|S )z1Remove slaves that are in an ODOWN or SDOWN stater   r   r   r(   )r   )r    rm   slaves_aliver<   s       r   filter_slaveszSentinel.filter_slaves   s\     	> 	>EZ  E*$5 teFm <====r   c                     | j         D ]Q}	 |                    |          }n# t          t          t          f$ r Y 2w xY w|                     |          }|r|c S Rg S )z;Returns a list of alive slaves for service ``service_name``)r   sentinel_slavesr   r
   r   r   )r    r&   r   rm   s       r   rh   zSentinel.discover_slaves  s     	 	H!11,??#]LA   ''//F 	s   !;;c                     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.
        Tr9   r   dictrS   updater    r&   redis_classconnection_pool_classr!   rS   s         r   
master_forzSentinel.master_for  sn    : #{ !788  ((({11d &7 
 
 
 	
r   c                     d|d<   t          | j                  }|                    |            | ||| fi |          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.
        Fr9   r   r   r   s         r   	slave_forzSentinel.slave_for3  sn    0 ${ !788  ((({11d &7 
 
 
 	
r   )r   N)r   r   r   ro   r   r   r,   r   re   r   rh   r   rL   r   r   r   r   r   rq   rq      s         > 	3 3 3 3,  "T T T  K K K.  
 
 
 4	$
 $
 $
 $
R 4	
 
 
 
 
 
r   rq   )ri   rQ   redis.clientr   redis.commandsr   redis.connectionr   r   r   redis.exceptionsr   r	   r
   r   redis.utilsr   r   r   r   rJ   rL   rq   r   r   r   <module>r      s           + + + + + + F F F F F F F F F F X X X X X X X X X X X X $ $ $ $ $ $	 	 	 	 	/ 	 	 		 	 	 	 	 	 	 	1 1 1 1 1
 1 1 1h	 	 	 	 	#<m 	 	 	?N ?N ?N ?N ?N^ ?N ?N ?NDE
 E
 E
 E
 E
 E
 E
 E
 E
 E
r   