
    (ph-                         S SK r S SKrS SKJr  S SKJrJr  S SKJrJ	r	J
r
Jr  S SKJrJrJr   " S S\5      r " S S	\5      r " S
 S\5      r " S S\5      r " S S\5      rg)    N)Redis)ConnectionPool
Connection)ConnectionErrorResponseErrorReadOnlyErrorTimeoutError)	iteritems	nativestrxrangec                       \ rS rSrSrg)MasterNotFoundError    N__name__
__module____qualname____firstlineno____static_attributes__r       A/var/www/html/venv/lib/python3.13/site-packages/redis/sentinel.pyr   r          r   r   c                       \ rS rSrSrg)SlaveNotFoundError   r   Nr   r   r   r   r   r      r   r   r   c                   L   ^  \ rS rSrU 4S jrS rU 4S jrS rU 4S jrSr	U =r
$ )SentinelManagedConnection   c                 Z   > UR                  S5      U l        [        [        U ]  " S0 UD6  g )Nconnection_poolr   )popr!   superr   __init__)selfkwargs	__class__s     r   r$   "SentinelManagedConnection.__init__   s(    %zz*;<'7A&Ar   c                     U R                   n[        U 5      R                  < SUR                  < S3nU R                  (       a"  SU R                  < SU R
                  < 3nX#-  nU$ )N	<service=z%s>z,host=z,port=)r!   typer   service_namehostport)r%   pools	host_infos       r   __repr__"SentinelManagedConnection.__repr__   sN    ###':#6#68I8IJ999.2iiCIAr   c                    > Uu  U l         U l        [        [        U ]  5         U R
                  R                  (       a:  U R                  S5        [        U R                  5       5      S:w  a  [        S5      eg g )NPINGPONGzPING failed)r-   r.   r#   r   connectr!   check_connectionsend_commandr   read_responser   )r%   addressr'   s     r   
connect_to$SentinelManagedConnection.connect_to    sg    &	49'6800f%++-.&8%m44 9 1r   c                 D   U R                   (       a  g U R                  R                  (       a*  U R                  U R                  R	                  5       5        g U R                  R                  5        H  n U R                  U5      s  $    [        e! [         a     M,  f = fN)_sockr!   	is_masterr<   get_master_addressrotate_slavesr   r   )r%   slaves     r   r7   !SentinelManagedConnection.connect(   s    ::))OOD00CCEF--;;=??511 >
 %$ ' s   6B
BBc                    >  [         [        U ]  5       $ ! [         a8    U R                  R
                  (       a  U R                  5         [        S5      ee f = f)Nz"The previous master is now a slave)r#   r   r:   r   r!   rA   
disconnectr   r%   r'   s    r   r:   'SentinelManagedConnection.read_response5   sR    	2DGII 		##-- !%&JKK		s
    AA)r!   r-   r.   )r   r   r   r   r$   r2   r<   r7   r:   r   __classcell__r'   s   @r   r   r      s"    B5% r   r   c                   V   ^  \ rS rSrSrU 4S jrS rU 4S jrU 4S jrS r	S r
S	rU =r$ )
SentinelConnectionPoolD   z
Sentinel backed connection pool.

If ``check_connection`` flag is set to True, SentinelManagedConnection
sends a PING command right after establishing the connection.
c                   > UR                  S[        5      US'   UR                  SS5      U l        UR                  SS5      U l        [
        [        U ]  " S0 UD6  [        R                  " U 5      U R                  S'   Xl        X l        g )Nconnection_classrA   Tr8   Fr!   r   )getr   r"   rA   r8   r#   rM   r$   weakrefproxyconnection_kwargsr,   sentinel_manager)r%   r,   rU   r&   r'   s       r   r$   SentinelConnectionPool.__init__L   s|    %+ZZ 9&;!"K6 &

+=u E$d4>v>4;MM$4G01( 0r   c                     [        U 5      R                  < SU R                  < SU R                  =(       a    S=(       d    S< S3$ )Nr*   (masterrD   ))r+   r   r,   rA   )r%   s    r   r2   SentinelConnectionPool.__repr__V   s6    JNN'x272
 	
r   c                 F   > [         [        U ]  5         S U l        S U l        g r?   )r#   rM   resetmaster_addressslave_rr_counterrH   s    r   r]   SentinelConnectionPool.reset]   s!    $d13" $r   c                    > U R                   (       + =(       d8    U R                   =(       a%    U R                  UR                  UR                  4:H  n[	        [
        U 5      nU=(       a    UR                  U5      $ r?   )rA   r^   r-   r.   r#   rM   owns_connection)r%   
connectioncheckparentr'   s       r   rb   &SentinelConnectionPool.owns_connectionb   se    NN" L K$$*//(JJ 	 -t4;//
;;r   c                     U R                   R                  U R                  5      nU R                  (       a%  U R                  U:w  a  Xl        U R                  SS9  U$ )NF)inuse_connections)rU   discover_masterr,   rA   r^   rG   )r%   r^   s     r   rB   )SentinelConnectionPool.get_master_addressi   sR    ..>>>>""n4&4# %8r   c              #     #    U R                   R                  U R                  5      nU(       a  U R                  c(  [        R
                  " S[        U5      S-
  5      U l        [        [        U5      5       H5  nU R                  S-   [        U5      -  U l        XR                     nUv   M7      U R                  5       v   [        SU R                  -  5      e! [         a     N$f = f7f)zRound-robin slave balancerr      zNo slave found for %r)rU   discover_slavesr,   r_   randomrandintlenr   rB   r   r   )r%   slaves_rD   s       r   rC   $SentinelConnectionPool.rotate_slavest   s     &&66t7H7HI$$,(.q#f+/(J%CK())A-V)=%445	 )	))++ !!8D<M<M!NOO # 		s*   B/C,2C C,
C)&C,(C))C,)r8   rA   r^   rU   r,   r_   )r   r   r   r   __doc__r$   r2   r]   rb   rB   rC   r   rJ   rK   s   @r   rM   rM   D   s-    1
%
<	P Pr   rM   c                   \    \ rS rSrSrSS jrS rS rS rS r	S	 r
\\4S
 jr\\4S jrSrg)Sentinel   a  
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.
Nc           	         Uc7  [        U5       VVs0 s H  u  pVUR                  S5      (       d  M  XV_M!     nnnX0l        U VVs/ s H  u  px[        Xx40 U R                  D6PM     snnU l        X l        X@l        g s  snnf s  snnf )Nsocket_)r
   
startswithsentinel_kwargsr   	sentinelsmin_other_sentinelsrT   )	r%   r|   r}   r{   rT   kvhostnamer.   s	            r   r$   Sentinel.__init__   s     " &&788DA<<	* 8  
  / 1:;09nh  G$2F2FG09;#6 !2;s   A>A>"Bc                    / nU R                    HI  nUR                  UR                  R                  S   < SUR                  R                  S   < 35        MK     [	        U 5      R
                  < SSR                  U5      < S3$ )Nr-   :r.   z<sentinels=[,z]>)r|   appendr!   rT   r+   r   join)r%   sentinel_addressessentinels      r   r2   Sentinel.__repr__   sw    H%%((::6B((::6B'  ' JHH'(* 	*r   c                 j    US   (       a  US   (       d
  US   (       a  gUS   U R                   :  a  gg)NrA   is_sdownis_odownFznum-other-sentinelsT)r}   )r%   stater,   s      r   check_master_stateSentinel.check_master_state   s5    [!U:%6%
:K&'$*B*BBr   c                 z   [        U R                  5       H}  u  p# UR                  5       nUR                  U5      nU(       d  M0  U R                  XQ5      (       d  MH  X0R                  S   sU R                  S'   U R                  U'   US   US   4s  $    [        SU< 35      e! [        [        4 a     M  f = f)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	   rQ   r   r   )r%   r,   sentinel_nor   mastersr   s         r   ri   Sentinel.discover_master   s     &/t~~%>!K"335 KK-Eu00EE nnQ/ ?q!4>>+#>T{E&M11 &? "l"LMM $\2 s   B&&B:9B:c                 z    / nU H2  nUS   (       d
  US   (       a  M  UR                  US   US   45        M4     U$ )z1Remove slaves that are in an ODOWN or SDOWN stater   r   r   r.   )r   )r%   rq   slaves_aliverD   s       r   filter_slavesSentinel.filter_slaves   sF    EZ E*$5teFm <=  r   c                     U R                    H1  n UR                  U5      nU R                  U5      nU(       d  M/  Us  $    / $ ! [        [        [        4 a     MO  f = f)z;Returns a list of alive slaves for service ``service_name``)r|   sentinel_slavesr   r   r	   r   )r%   r,   r   rq   s       r   rm   Sentinel.discover_slaves   sc    H!11,? ''/Fv ' 	 $]LA s   AAAc                 r    SUS'   [        U R                  5      nUR                  U5        U" U" X40 UD6S9$ )a  
Returns a redis client instance for the ``service_name`` master.

A SentinelConnectionPool class is used to retrive 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 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.
TrA   r!   dictrT   updater%   r,   redis_classconnection_pool_classr&   rT   s         r   
master_forSentinel.master_for   sM    , #{ !7!78  (+@,5"3,5 6 	6r   c                 r    SUS'   [        U R                  5      nUR                  U5        U" U" X40 UD6S9$ )at  
Returns redis client instance for the ``service_name`` slave(s).

A SentinelConnectionPool class is used to retrive the slave's
address before establishing a new connection.

By default clients will be a 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.
FrA   r   r   r   s         r   	slave_forSentinel.slave_for  sM    & ${ !7!78  (+@,5"3,5 6 	6r   )rT   r}   r{   r|   )r   N)r   r   r   r   rt   r$   r2   r   ri   r   rm   r   rM   r   r   r   r   r   r   rv   rv      s@    83"	*N*
 49)?68 38(>6r   rv   )rn   rR   redis.clientr   redis.connectionr   r   redis.exceptionsr   r   r   r	   redis._compatr
   r   r   r   r   r   rM   objectrv   r   r   r   <module>r      se       7, , 6 6	/ 		 	.
 .b@P^ @PF^6v ^6r   