
    AHjC                     t   d dl Z d dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZmZ d dlmZ d dlmZmZ d dlmZmZ d dlmZmZ  G d	 d
e      Z G d de      Z G d de      Z G d de      Z G d de      Z G d d      Z G d de      Z G d d      Z  G d de       Z! G d de      Z" G d d      Z# G d d       Z$ G d! d"      Z% G d# d$      Z& G d% d&      Z' G d' d(e'      Z( G d) d*e      Z) G d+ d,e      Z* G d- d.e      Z+ G d/ d0e      Z, G d1 d2e      Z- G d3 d4e      Z. G d5 d6e      Z/y)7    N)ABCabstractmethod)Enum)DictListOptionalTypeUnion)TokenInterface)CredentialProviderStreamingCredentialProvider)init_connection_countregister_pools_connection_count)check_protocol_versiondeprecated_functionc                   &    e Zd ZdZedefd       Zy)EventListenerInterfacez7
    Represents a listener for given event object.
    eventc                      y N selfr   s     C/root/tools/cai/cai_env/lib/python3.12/site-packages/redis/event.pylistenzEventListenerInterface.listen           N__name__
__module____qualname____doc__r   objectr   r   r   r   r   r      s"     F  r   r   c                   &    e Zd ZdZedefd       Zy)AsyncEventListenerInterfacez>
    Represents an async listener for given event object.
    r   c                    K   y wr   r   r   s     r   r   z"AsyncEventListenerInterface.listen   	        Nr   r   r   r   r%   r%      s"     &  r   r%   c                       e Zd ZdZedefd       Zedefd       Zedee	e   e
eeef      f   fd       Zedee	e   e
eeef      f   fd       Zy)	EventDispatcherInterfacezf
    Represents a dispatcher that dispatches events to listeners
    associated with given event.
    r   c                      y r   r   r   s     r   dispatchz!EventDispatcherInterface.dispatch*   r   r   c                    K   y wr   r   r   s     r   dispatch_asyncz'EventDispatcherInterface.dispatch_async.   r'   r(   mappingsc                      y)zRegister additional listeners.Nr   r   r/   s     r   register_listenersz+EventDispatcherInterface.register_listeners2        	r   c                      y)z3Remove previously registered listeners by identity.Nr   r1   s     r   unregister_listenersz-EventDispatcherInterface.unregister_listeners=   r3   r   N)r   r    r!   r"   r   r#   r,   r.   r   r	   r   r
   r   r%   r2   r5   r   r   r   r*   r*   $   s    
 f   &   L-/JJKLN
  L-/JJKLN
 r   r*   c                   ,     e Zd ZdZdedef fdZ xZS )EventExceptionzM
    Exception wrapper that adds an event object into exception context.
    	exceptionr   c                 @    || _         || _        t        |   |       y r   )r8   r   super__init__)r   r8   r   	__class__s      r   r;   zEventException.__init__N   s    "
#r   )r   r    r!   r"   	Exceptionr#   r;   __classcell__)r<   s   @r   r7   r7   I   s     $) $F $ $r   r7   c                       e Zd Z	 d
deeee   ee   f      fdZ	defdZ
defdZdeee   eeeef      f   fdZdeee   eeeef      f   fd	Zy)EventDispatcherNevent_listenersc                 .   t         t               gt        t               gt        t               gt        t               gt        t               gt        t               gi| _        t        j                         | _        d| _        |r| j#                  |       yy)z]
        Dispatcher that dispatches events to listeners associated with given event.
        N)AfterConnectionReleasedEventReAuthConnectionListener(AfterPooledConnectionsInstantiationEvent"RegisterReAuthForPooledConnections'AfterSingleConnectionInstantiationEvent!RegisterReAuthForSingleConnection'AfterPubSubConnectionInstantiationEventRegisterReAuthForPubSub#AfterAsyncClusterInstantiationEvent"RegisterReAuthForAsyncClusterNodes!AsyncAfterConnectionReleasedEventAsyncReAuthConnectionListener_event_listeners_mapping	threadingRLock_lock_async_lockr2   )r   rA   s     r   r;   zEventDispatcher.__init__V   s     )(*+ 5247 4136 46M6O5P/2T2V1W--/0
 	%. __&
##O4 r   r   c                     | j                   5  t        | j                  j                  t	        |      g             }d d d        D ]  }|j                  |        y # 1 sw Y   "xY wr   )rR   listrO   gettyper   r   r   	listenerslisteners       r   r,   zEventDispatcher.dispatch|   s`    
 ZZ 	QT::>>tE{BOPI	Q! 	#HOOE"	#	Q 	Qs   /AA&c                 r  K   | j                   t        j                         | _         | j                   4 d {    t        | j                  j                  t        |      g             }d d d       d {    D ]  }|j                  |       d {     y 7 c7 '# 1 d {  7  sw Y   7xY w7  wr   )rS   asyncioLockrU   rO   rV   rW   r   rX   s       r   r.   zEventDispatcher.dispatch_async   s     #&||~D
 ## 	Q 	QT::>>tE{BOPI	Q 	Q! 	)H//%(((	)	Q 	Q 	Q 	Q 	Q )sP   6B7BB7/B +B76B7B7B5B7B7 B2&B)'B2.B7r/   c           	          | j                   5  |D ]W  }|| j                  v r5t        t        | j                  |   ||   z               | j                  |<   F||   | j                  |<   Y 	 d d d        y # 1 sw Y   y xY wr   )rR   rO   rU   set)r   r/   
event_types      r   r2   z"EventDispatcher.register_listeners   s     ZZ 
	U& 	U
!>!>>@D 99*E&z23AD11*= AI@TD11*=	U
	U 
	U 
	Us   AA44A=c           	      $   | j                   5  |j                         D ]U  \  }}| j                  j                  |      }|s$|D cg c]  t	        fd|D              r c}| j                  |<   W 	 d d d        y c c}w # 1 sw Y   y xY w)Nc              3   &   K   | ]  }|u 
 y wr   r   ).0targetrZ   s     r   	<genexpr>z7EventDispatcher.unregister_listeners.<locals>.<genexpr>   s     Jf861Js   )rR   itemsrO   rV   all)r   r/   r`   	to_removecurrentrZ   s        `r   r5   z$EventDispatcher.unregister_listeners   s     ZZ 	)1)9 
%
I77;;JG
 %,= J	JJ =--j9
	 	=	 	s   :BB%BBBr   )r   r    r!   r   r   r	   r#   r   r   r;   r,   r.   r
   r%   r2   r5   r   r   r   r@   r@   T   s     	$5!ft$:;;<
$5L#f #
)& 
)UL-/JJKLN
U&L-/JJKLN
r   r@   c                   &    e Zd ZdZd Zed        Zy)rC   zA
    Event that will be fired before each command execution.
    c                     || _         y r   _connection)r   
connections     r   r;   z%AfterConnectionReleasedEvent.__init__   s
    %r   c                     | j                   S r   rl   r   s    r   rn   z'AfterConnectionReleasedEvent.connection       r   N)r   r    r!   r"   r;   propertyrn   r   r   r   rC   rC      s     &    r   rC   c                       e Zd Zy)rM   Nr   r    r!   r   r   r   rM   rM          r   rM   c                       e Zd ZdZy)AfterSlotsCacheRefreshEventa  
    Event fired after NodesManager's slots cache is refreshed, either via a
    full re-initialization or a MOVED-driven slot re-mapping. Signal-only;
    carries no payload. Listeners typically reconcile per-node bookkeeping
    (e.g. ClusterPubSub shard subscriptions).
    N)r   r    r!   r"   r   r   r   rw   rw      s     	r   rw   c                       e Zd Zy) AsyncAfterSlotsCacheRefreshEventNrt   r   r   r   ry   ry      ru   r   ry   c                       e Zd ZdZdZy)
ClientType)sync)asyncN)r   r    r!   SYNCASYNCr   r   r   r{   r{      s    DEr   r{   c                   t    e Zd ZdZ	 ddededee   fdZe	d        Z
e	defd	       Ze	deedf   fd
       Zy)rE   zQ
    Event that will be fired after pooled connection instances was created.
    Nconnection_poolsclient_typecredential_providerc                 .    || _         || _        || _        y r   )_connection_pools_client_type_credential_provider)r   r   r   r   s       r   r;   z1AfterPooledConnectionsInstantiationEvent.__init__   s     "2'$7!r   c                     | j                   S r   )r   rp   s    r   r   z9AfterPooledConnectionsInstantiationEvent.connection_pools   s    %%%r   returnc                     | j                   S r   r   rp   s    r   r   z4AfterPooledConnectionsInstantiationEvent.client_type          r   c                     | j                   S r   r   rp   s    r   r   z<AfterPooledConnectionsInstantiationEvent.credential_provider       (((r   r   )r   r    r!   r"   r   r{   r   r   r;   rr   r   r   r
   r   r   r   r   rE   rE      s     =A	88  8 &&89	8 & & !Z ! ! )U+=t+C%D ) )r   rE   c                       e Zd ZdZdedeej                  ej                  f   fdZ
ed        Zedefd       Zedeej                  ej                  f   fd       Zy	)
rG   z
    Event that will be fired after single connection instances was created.

    :param connection_lock: For sync client thread-lock should be provided,
    for async asyncio.Lock
    r   connection_lockc                 .    || _         || _        || _        y r   )rm   r   _connection_lock)r   rn   r   r   s       r   r;   z0AfterSingleConnectionInstantiationEvent.__init__  s     &' /r   c                     | j                   S r   rl   rp   s    r   rn   z2AfterSingleConnectionInstantiationEvent.connection  rq   r   r   c                     | j                   S r   r   rp   s    r   r   z3AfterSingleConnectionInstantiationEvent.client_type  r   r   c                     | j                   S r   r   rp   s    r   r   z7AfterSingleConnectionInstantiationEvent.connection_lock      $$$r   N)r   r    r!   r"   r{   r
   rP   rQ   r\   r]   r;   rr   rn   r   r   r   r   r   rG   rG      s    0  0 y<=	0     !Z ! ! %y'D!E % %r   rG   c                       e Zd Zdedeej                  ej                  f   fdZ	e
d        Ze
d        Ze
defd       Ze
deej                  ej                  f   fd       Zy	)
rI   r   r   c                 <    || _         || _        || _        || _        y r   )_pubsub_connection_connection_poolr   r   )r   pubsub_connectionconnection_poolr   r   s        r   r;   z0AfterPubSubConnectionInstantiationEvent.__init__  s$     #4 /' /r   c                     | j                   S r   )r   rp   s    r   r   z9AfterPubSubConnectionInstantiationEvent.pubsub_connection'  s    &&&r   c                     | j                   S r   )r   rp   s    r   r   z7AfterPubSubConnectionInstantiationEvent.connection_pool+  r   r   r   c                     | j                   S r   r   rp   s    r   r   z3AfterPubSubConnectionInstantiationEvent.client_type/  r   r   c                     | j                   S r   r   rp   s    r   r   z7AfterPubSubConnectionInstantiationEvent.connection_lock3  r   r   N)r   r    r!   r{   r
   rP   rQ   r\   r]   r;   rr   r   r   r   r   r   r   r   rI   rI     s    
0  	
0
 y<=
0 ' ' % % !Z ! ! %y'D!E % %r   rI   c                   `    e Zd ZdZ	 d	dedee   fdZedefd       Z	ede
edf   fd       Zy)
rK   z
    Event that will be fired after async cluster instance was created.

    Async cluster doesn't use connection pools,
    instead ClusterNode object manages connections.
    Nnodesr   c                      || _         || _        y r   )_nodesr   )r   r   r   s      r   r;   z,AfterAsyncClusterInstantiationEvent.__init__@  s    
 $7!r   r   c                     | j                   S r   )r   rp   s    r   r   z)AfterAsyncClusterInstantiationEvent.nodesH  s    {{r   c                     | j                   S r   r   rp   s    r   r   z7AfterAsyncClusterInstantiationEvent.credential_providerL  r   r   r   )r   r    r!   r"   dictr   r   r;   rr   r   r
   r   r   r   r   rK   rK   8  sk     =A88 &&898 t   )U+=t+C%D ) )r   rK   c                   L    e Zd ZdZdedefdZedefd       Zedefd       Z	y)	OnCommandsFailEventzD
    Event fired whenever a command fails during the execution.
    commandsr8   c                      || _         || _        y r   )	_commands
_exception)r   r   r8   s      r   r;   zOnCommandsFailEvent.__init__V  s    
 "#r   r   c                     | j                   S r   )r   rp   s    r   r   zOnCommandsFailEvent.commands^  s    ~~r   c                     | j                   S r   )r   rp   s    r   r8   zOnCommandsFailEvent.exceptionb  s    r   N)
r   r    r!   r"   tupler=   r;   rr   r   r8   r   r   r   r   r   Q  sT    $$ $ %   9  r   r   c                       e Zd Zy)AsyncOnCommandsFailEventNrt   r   r   r   r   r   g  ru   r   r   c                       e Zd ZdZdefdZy)rD   zG
    Listener that performs re-authentication of given connection.
    r   c                 8    |j                   j                          y r   rn   re_authr   s     r   r   zReAuthConnectionListener.listenp  s      "r   N)r   r    r!   r"   rC   r   r   r   r   rD   rD   k  s    #8 #r   rD   c                       e Zd ZdZdefdZy)rN   zM
    Async listener that performs re-authentication of given connection.
    r   c                 T   K   |j                   j                          d {    y 7 wr   r   r   s     r   r   z$AsyncReAuthConnectionListener.listeny  s     &&(((s   (&(N)r   r    r!   r"   rM   r   r   r   r   rN   rN   t  s    )"C )r   rN   c                   F    e Zd ZdZd ZdefdZd Zd Zde	fdZ
de	fd	Zy
)rF   z
    Listener that registers a re-authentication callback for pooled connections.
    Required by :class:`StreamingCredentialProvider`.
    c                     d | _         y r   _eventrp   s    r   r;   z+RegisterReAuthForPooledConnections.__init__  	    r   r   c                    t        |j                  t              r|| _        |j                  t
        j                  k(  rK|j                  j                  | j                         |j                  j                  | j                         y |j                  j                  | j                         |j                  j                  | j                         y y r   )
isinstancer   r   r   r   r{   r~   on_next_re_authon_error_raise_on_error_re_auth_async_raise_on_error_asyncr   s     r   r   z)RegisterReAuthForPooledConnections.listen  s    e//1LMDK  JOO3))11$--@))2243G3GH))11$2E2EF))2243M3MN Nr   c                 \    | j                   j                  D ]  }|j                  |        y r   r   r   re_auth_callbackr   tokenpools      r   r   z+RegisterReAuthForPooledConnections._re_auth  s)    KK00 	)D!!%(	)r   c                 x   K   | j                   j                  D ]  }|j                  |       d {     y 7 wr   r   r   s      r   r   z1RegisterReAuthForPooledConnections._re_auth_async  s6     KK00 	/D''...	/.s   .:8:errorc                 .    t        || j                        r   r7   r   r   r   s     r   r   z2RegisterReAuthForPooledConnections._raise_on_error      UDKK00r   c                 6   K   t        || j                        wr   r   r   s     r   r   z8RegisterReAuthForPooledConnections._raise_on_error_async       UDKK00   N)r   r    r!   r"   r;   rE   r   r   r   r=   r   r   r   r   r   rF   rF   }  s=    
	OD 	O)/1Y 11 1r   rF   c                   F    e Zd ZdZd ZdefdZd Zd Zde	fdZ
de	fd	Zy
)rH   z
    Listener that registers a re-authentication callback for single connection.
    Required by :class:`StreamingCredentialProvider`.
    c                     d | _         y r   r   rp   s    r   r;   z*RegisterReAuthForSingleConnection.__init__  r   r   r   c                    t        |j                  j                  t              r|| _        |j
                  t        j                  k(  r_|j                  j                  j                  | j                         |j                  j                  j                  | j                         y |j                  j                  j                  | j                         |j                  j                  j                  | j                         y y r   )r   rn   r   r   r   r   r{   r~   r   r   r   r   r   r   r   s     r   r   z(RegisterReAuthForSingleConnection.listen  s    002M
  DK  JOO3  44<<T]]K  44==d>R>RS  44<<T=P=PQ  44==..
r   c                 *   | j                   j                  5  | j                   j                  j                  d|j	                  d      |j                                | j                   j                  j                          d d d        y # 1 sw Y   y xY wNAUTHoidr   r   rn   send_commandtry_get	get_valueread_responser   r   s     r   r   z*RegisterReAuthForSingleConnection._re_auth  sk    [[(( 	3KK""//e,eoo.? KK""002		3 	3 	3s   A)B		Bc                   K   | j                   j                  4 d {    | j                   j                  j                  d|j	                  d      |j                                d {    | j                   j                  j                          d {    d d d       d {    y 7 7 C7 7 # 1 d {  7  sw Y   y xY wwr   r   r   s     r   r   z0RegisterReAuthForSingleConnection._re_auth_async  s     ;;.. 	9 	9++((55e,eoo.?   ++((66888		9 	9 	9 9		9 	9 	9 	9si   CB+CAB3)B-*+B3B/B3C%B1&C-B3/B31C3C9B<:CCr   c                 .    t        || j                        r   r   r   s     r   r   z1RegisterReAuthForSingleConnection._raise_on_error  r   r   c                 6   K   t        || j                        wr   r   r   s     r   r   z7RegisterReAuthForSingleConnection._raise_on_error_async  r   r   N)r   r    r!   r"   r;   rG   r   r   r   r=   r   r   r   r   r   rH   rH     s;    
C 391Y 11 1r   rH   c                   6    e Zd Zd ZdefdZdefdZdefdZ	y)	rL   c                     d | _         y r   r   rp   s    r   r;   z+RegisterReAuthForAsyncClusterNodes.__init__  r   r   r   c                     t        |j                  t              rR|| _        |j                  j	                  | j
                         |j                  j                  | j                         y y r   )r   r   r   r   r   r   r   r   r   s     r   r   z)RegisterReAuthForAsyncClusterNodes.listen  sR    e//1LMDK%%--dmm<%%..t/C/CD Nr   r   c                    K   | j                   j                  D ]2  }| j                   j                  |   j                  |       d {    4 y 7 wr   )r   r   r   )r   r   keys      r   r   z+RegisterReAuthForAsyncClusterNodes._re_auth  sG     ;;$$ 	AC++##C(99%@@@	A@s   AAAAr   c                 6   K   t        || j                        wr   r   r   s     r   r   z2RegisterReAuthForAsyncClusterNodes._raise_on_error  r   r   N)
r   r    r!   r;   rK   r   r   r   r=   r   r   r   r   rL   rL     s0    E? EAN A19 1r   rL   c                   N    e Zd Zd ZdefdZdefdZdefdZde	fdZ
de	fd	Zy
)rJ   c                 J    d | _         d | _        d | _        d | _        d | _        y r   )rm   r   r   r   r   rp   s    r   r;   z RegisterReAuthForPubSub.__init__  s)     $  $r   r   c                    t        |j                  j                  t              rLt	        |j                  j                         d      r&|| _        |j                  | _        |j                  | _	        |j                  | _        |j                  | _        | j                  t        j                  k(  r_| j                  j                  j!                  | j"                         | j                  j                  j%                  | j&                         y | j                  j                  j!                  | j(                         | j                  j                  j%                  | j*                         y y y )N   )r   r   r   r   r   get_protocolr   rm   r   r   r   r   r   r   r{   r~   r   r   r   r   r   r   r   s     r   r   zRegisterReAuthForPubSub.listen  s   ##779T
$U%<%<%I%I%KQODK$66D$)$9$9D! % 1 1D$)$9$9D!  JOO3  44<<T]]K  44==d>R>RS  44<<T=P=PQ  44==.. P
r   r   c                 $   | j                   5  | j                  j                  d|j                  d      |j	                                | j                  j                          d d d        | j                  j                  |       y # 1 sw Y   %xY wr   r   rm   r   r   r   r   r   r   r   s     r   r   z RegisterReAuthForPubSub._re_auth  su    "" 	-))e,eoo.? **,		- 	..u5	- 	-s   ABBc                   K   | j                   4 d {    | j                  j                  d|j                  d      |j	                                d {    | j                  j                          d {    d d d       d {    | j                  j                  |       d {    y 7 7 \7 <7 .# 1 d {  7  sw Y   >xY w7 "wr   r   r   s     r   r   z&RegisterReAuthForPubSub._re_auth_async  s     (( 	3 	3""//e,eoo.?   ""00222		3 	3 ##44U;;;	3 3		3 	3 	3 	3 	<st   CB0C>B8B2!B87B48B8<CB6"C*C+C2B84B86C8C
>C?C
Cr   c                 .    t        || j                        r   r   r   s     r   r   z'RegisterReAuthForPubSub._raise_on_error  r   r   c                 6   K   t        || j                        wr   r   r   s     r   r   z-RegisterReAuthForPubSub._raise_on_error_async  r   r   N)r   r    r!   r;   rI   r   r   r   r   r=   r   r   r   r   r   rJ   rJ     sD    C &6n 6<. <1Y 11 1r   rJ   c                   6    e Zd ZdZ edd      defd       Zy)&InitializeConnectionCountObservabilityzC
    Listener that initializes connection count observability.
    z{Connection count is now tracked via record_connection_count(). This functionality will be removed in the next major versionz7.4.0)reasonversionr   c                 B    t                t        |j                         y r   )r   r   r   r   s     r   r   z-InitializeConnectionCountObservability.listen  s     	 	((>(>?r   N)r   r    r!   r"   r   rE   r   r   r   r   r   r     s4     G
@D @
@r   r   )0r\   rP   abcr   r   enumr   typingr   r   r   r	   r
   redis.auth.tokenr   redis.credentialsr   r   redis.observability.recorderr   r   redis.utilsr   r   r   r%   r*   r=   r7   r@   rC   rM   rw   ry   r{   rE   rG   rI   rK   r   r   rD   rN   rF   rH   rL   rJ   r   r   r   r   <module>r     sB     #  4 4 + M DS # "s "J$Y $c. cL
  
 	(D 		 		'B 	 
) )8% %>% %<) )2 ,	2 	#5 #)$? ) 1)?  1F*1(> *1Z1)? 1$114 11h@-C @r   