
    keJi:                     4   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  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)AsyncIteratorIterableMappingOptionalSequenceTupleType)Redis)
ConnectionConnectionPool
EncodableTSSLConnection)AsyncSentinelCommands)ConnectionErrorReadOnlyErrorResponseErrorTimeoutErrorc                       e Zd ZdS )MasterNotFoundErrorN__name__
__module____qualname__     \/var/www/html/movieo_spanner_bot/venv/lib/python3.11/site-packages/redis/asyncio/sentinel.pyr   r              Dr   r   c                       e Zd ZdS )SlaveNotFoundErrorNr   r   r   r   r   r      r   r   r   c                        e Zd Z fdZd Zd Zd Zd Z	 	 dddd	d
ede	e
         de	e
         de	e         f 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                     d| j         j         d| j         j         }| j        rd| j         d| j         }||z  }|dz   S )N<.z,host=z,port=z)>)r)   r   r   hostport)r'   s	host_infos      r   __repr__z"SentinelManagedConnection.__repr__#   sV    E)EEDN,CEE9 	===$)==INA4xr   c                 v   K   |\  | _         | _        |                     | j        j        d           d {V  d S )NF)check_healthretry_socket_connect)r-   r.   connect_check_healthr#   check_connection)r'   addresss     r   
connect_toz$SentinelManagedConnection.connect_to*   se      &	49''->!& ( 
 
 	
 	
 	
 	
 	
 	
 	
 	
 	
r   c                 P  K   | j         rd S | j        j        r:|                     | j                                         d {V            d {V  d S | j                                        2 3 d {V }	 |                     |           d {V c S # t          $ r Y 1w xY w6 t          N)_readerr#   	is_masterr8   get_master_addressrotate_slavesr   r   )r'   slaves     r   _connect_retryz(SentinelManagedConnection._connect_retry1   s     < 	F) 	%//(<(O(O(Q(Q"Q"Q"Q"Q"Q"QRRRRRRRRRRR#3AACC       e!%!7!7777777777&   H  D
 %$s   +B 3B
BBc                 T   K   | j                             | j        d            d {V S )Nc                 *    t          j        d          S )Nr   )asynciosleep)errors    r   <lambda>z3SentinelManagedConnection.connect.<locals>.<lambda>A   s    '-** r   )retrycall_with_retryr@   r'   s    r   connectz!SentinelManagedConnection.connect>   sJ      Z//**
 
 
 
 
 
 
 
 	
r   FNT)disconnect_on_errorpush_requestdisable_decodingtimeoutrK   rL   c                   K   	 t                                          ||||           d {V S # t          $ r7 | j        j        r)|                                  d {V  t          d           w xY w)N)rM   rN   rK   rL   z"The previous master is now a slave)r%   read_responser   r#   r<   
disconnectr   )r'   rM   rN   rK   rL   r)   s        r   rP   z'SentinelManagedConnection.read_responseD   s      	..!1$7)	 /           		 		 		#- L oo'''''''''%&JKKK		s
   *0 AA1)FN)r   r   r   r&   r1   r8   r@   rJ   boolr   floatrP   __classcell__r)   s   @r   r!   r!      s        # # # # #  
 
 
% % %
 
 
 "'#'
 04',   %
 &e_ tn         r   r!   c                       e Zd ZdS )SentinelManagedSSLConnectionNr   r   r   r   rW   rW   _   r   r   rW   c                   T     e Zd ZdZ fdZd Z fdZdef fdZd Z	de
f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                    |                     d|                    dd          rt          nt                    |d<   |                    dd          | _        |                    dd          | _         t                      j        di | t          j	        |           | j
        d<   || _        || _        d | _        d | _        d S )	Nconnection_classsslFr<   Tr6   r#   r   )getr$   rW   r!   r<   r6   r%   r&   weakrefproxyconnection_kwargsservice_namesentinel_managermaster_addressslave_rr_counter)r'   ra   rb   r(   r)   s       r   r&   zSentinelConnectionPool.__init__k   s    %+ZZ ::eU++/,,.&
 &
!"  K66 &

+=u E E""6"""4;M$4G4G01( 0" $r   c           	      d    d| j         j         d| j         j         d| j         d| j        rdpd d	S )Nr+   r,   z	(service=(masterr?   z))>)r)   r   r   ra   r<   rI   s    r   r1   zSentinelConnectionPool.__repr__}   sf    X) X XDN,C X X)X X,0N,Gx,R7X X X	
r   c                 d    t                                                       d | _        d | _        d S r:   )r%   resetrc   rd   )r'   r)   s    r   ri   zSentinelConnectionPool.reset   s*    " $r   
connectionc                     | j          p| j         o| j        |j        |j        fk    }|o t	                                          |          S r:   )r<   rc   r-   r.   r%   owns_connection)r'   rj   checkr)   s      r   rl   z&SentinelConnectionPool.owns_connection   sO    N" 
NXt2z
6XX 	 <00<<<r   c                    K   | j                             | j                   d {V }| j        r.| j        |k    r#|| _        |                     d           d {V  |S )NF)inuse_connections)rb   discover_masterra   r<   rc   rQ   )r'   rc   s     r   r=   z)SentinelConnectionPool.get_master_address   s      #4DDTEVWWWWWWWW> 	?"n44&4# ooo>>>>>>>>>r   returnc                  K   | j                             | j                   d{V }|r| j        *t	          j        dt          |          dz
            | _        t          t          |                    D ]3}| j        dz   t          |          z  | _        || j                 }|W V  4	 |                                  d{V W V  n# t          $ r Y nw xY wt          d| j                  )zRound-robin slave balancerNr      zNo slave found for )rb   discover_slavesra   rd   randomrandintlenranger=   r   r   )r'   slaves_r?   s       r   r>   z$SentinelConnectionPool.rotate_slaves   s!     ,<<T=NOOOOOOOO 	$,(.q#f++/(J(J%3v;;''  )-)>)Bc&kk(Q%t45	//1111111111111" 	 	 	D	 !Lt7H!L!LMMMs   ,C
 

CC)r   r   r   __doc__r&   r1   ri   r   rl   r=   r   r>   rT   rU   s   @r   rY   rY   c   s         % % % % %$
 
 
% % % % %
=* = = = = = =  N] N N N N N N N Nr   rY   c                      e Zd ZdZ	 	 	 ddZd Zd Zdeded	e	fd
Z
defdZdee         d	eeeef                  fdZded	eeeef                  fdZeefdedee         dee         fdZeefdedee         dee         f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                      |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          d||d j        S ))r-   r.   r   )r
   sentinel_kwargs)r   hostnamer.   r'   s      r   
<listcomp>z%Sentinel.__init__.<locals>.<listcomp>   sE     
 
 
$ CxdCCd.BCC
 
 
r   )itemsr   	sentinelsmin_other_sentinelsr`   _force_master_ip)r'   r   r   r   force_master_ipr`   s   `     r   r&   zSentinel.__init__   s     " !2!8!8!:!:  O  /
 
 
 
"+
 
 
 $7 !2 /r   c                 z  K   t                              dd                    }t                              dd                    }|r5 t          j        | j                  j        i  d{V }|r|gS |rdndS fd| j        D             }t          j        |  d{V }|r|S t          |          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.
        onceFreturn_responsesNTc           	      N    g | ]!}t          j         |j        i           "S r   )rC   Taskexecute_command)r   sentinelargsr(   s     r   r   z,Sentinel.execute_command.<locals>.<listcomp>   sF     
 
 
 L114B6BBCC
 
 
r   )	rR   r$   ru   choicer   r   rC   gatherall)r'   r   r(   r   r   responsetasks	responsess    ``     r   r   zSentinel.execute_command   s      FJJvu--..  

+=u E EFF 	3JV]4>::J       H   3 z!'2ttU2
 
 
 
 
 N
 
 
 ".%0000000	 	9~~r   c                     g }| j         D ]<}|                    |j        j        d          d|j        j        d                     =d| j         d| j        j         dd                    |           dS )	Nr-   :r.   r+   r,   z(sentinels=[,z])>)r   appendr#   r`   r)   r   join)r'   sentinel_addressesr   s      r   r1   zSentinel.__repr__   s     	 	H%%+=fE H H+=fEH H   
= = =!8 = =88$677= = =	
r   statera   rq   c                 `    |d         r|d         s|d         rdS |d         | j         k     rdS dS )Nr<   is_sdownis_odownFznum-other-sentinelsT)r   )r'   r   ra   s      r   check_master_statezSentinel.check_master_state  sJ    [! 	U:%6 	%
:K 	5&'$*BBB5tr   c                 ^  K   t                      }t          | j                  D ]\  }}	 |                                 d{V }n8# t          t
          f$ r$}|                    | d|           Y d}~Qd}~ww xY w|                    |          }|rX|                     ||          rB|| j        d         c| j        d<   | j        |<   | j	        | j	        n|d         }||d         fc S d}	t          |          dk    rd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.
        Nz - r   ipr.    z : z, zNo master found for )list	enumerater   sentinel_mastersr   r   r   r]   r   r   rw   r   r   )
r'   ra   collected_errorssentinel_nor   masterser   r   
error_infos
             r   rp   zSentinel.discover_master  s       66%.t~%>%> 	) 	)!K ( 9 9 ; ;;;;;;;#\2    ''8(=(=(=(=>>> KK--E )00EE ) N1% ?q!4>+#> ,8 ))t 
 5=((((
  1$$<tyy)9::<<J!"U"U"U"UVVVs   AA;A66A;ry   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'   ry   slaves_aliver?   s       r   filter_slaveszSentinel.filter_slaves6  s^      	> 	>EZ  E*$5 teFm <====r   c                    K   | j         D ]W}	 |                    |           d{V }n# t          t          t          f$ r Y 8w xY w|                     |          }|r|c S Xg S )z;Returns a list of alive slaves for service ``service_name``N)r   sentinel_slavesr   r   r   r   )r'   ra   r   ry   s       r   rt   zSentinel.discover_slavesA  s        	 	H'77EEEEEEEE#]LA   ''//F 	s   )AAredis_classconnection_pool_classc                     d|d<   t          | j                  }|                    |            ||| fi |}|                    |          S )a  
        Returns a redis client instance for the ``service_name`` master.
        Sentinel client will detect failover and reconnect Redis clients
        automatically.

        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<   dictr`   update	from_poolr'   ra   r   r   r(   r`   r#   s          r   
master_forzSentinel.master_forO  sb    > #{ !788  (((//dXXFWXX$$_555r   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.
        Fr<   r   r   s          r   	slave_forzSentinel.slave_forv  sb    0 ${ !788  (((//dXXFWXX$$_555r   )r   NN)r   r   r   r{   r&   r   r1   r   strrR   r   rp   r   r   r   r   r   r   rt   r
   rY   r	   r   r   r   r   r   r}   r}      s        > 0 0 0 00  @

 

 

 C D    !W# !W !W !W !WF	w'		%
J./	0	 	 	 		%
J./	0   " $)>T	%6 %6%6 %[%6  $$:;	%6 %6 %6 %6T $)>T	6 66 %[6  $$:;	6 6 6 6 6 6r   r}   )rC   ru   r^   typingr   r   r   r   r   r   r	   redis.asyncio.clientr
   redis.asyncio.connectionr   r   r   r   redis.commandsr   redis.exceptionsr   r   r   r   r   r   r!   rW   rY   r}   r   r   r   <module>r      s      T T T T T T T T T T T T T T T T T T & & & & & &            1 0 0 0 0 0           	 	 	 	 	/ 	 	 		 	 	 	 	 	 	 	> > > > >
 > > >B	 	 	 	 	#<m 	 	 	DN DN DN DN DN^ DN DN DNNj6 j6 j6 j6 j6$ j6 j6 j6 j6 j6r   