
    #Iid                         S SK r S SKrS SKJr   S SKrS SKJr   S SKrS SK	J
r
  S SKJr  SSKJr  \ R                  " S5      rS	 r " S
 S\5      rg! \ a    SrSr NDf = f! \ a    SrSr
 NIf = f)    N)urlparse)
RedisError)ValkeyError)json   )PubSubManagersocketioc                    [        U 5      nUR                  S;  a  [        S5      e/ nUR                  R	                  S5      S   R	                  S5       H3  nUR                  SS5      u  pEUR                  U[        U5      45        M5     0 nUR                  (       a  UR                  US'   UR                  (       a  UR                  US	'   S
nUR                  (       aa  UR                  R	                  S5      n[        U5      S:  a  US   S:w  a  [        US   5      US'   [        U5      S:  a  US   S:w  a  US   nX'U4$ )zuParse a Redis Sentinel URL with the format:
redis+sentinel://[:password]@host1:port1,host2:port2,.../db/service_name
   redis+sentinelvalkey+sentinelzInvalid Redis Sentinel URL@,:r   usernamepasswordN/    db   )r   scheme
ValueErrornetlocsplitrsplitappendintr   r   pathlen)	url
parsed_url	sentinels	host_porthostportkwargsservice_namepartss	            </venv/lib/python3.13/site-packages/socketio/redis_manager.pyparse_redis_sentinel_urlr,      s,    #J EE566I&&,,S1"5;;C@	%%c1-
$D	*+ A F'00z'00zL%%c*u:?uQx2~uQx=F4Lu:?uQx2~ 8LF**    c                   b   ^  \ rS rSrSrSr  SU 4S jjrU 4S jrS rS r	S r
S	 rS
 rSrU =r$ )RedisManager3   a:  Redis based client manager.

This class implements a Redis backend for event sharing across multiple
processes. Only kept here as one more example of how to build a custom
backend, since the kombu backend is perfectly adequate to support a Redis
message queue.

To use a Redis backend, initialize the :class:`Server` instance as
follows::

    url = 'redis://hostname:port/0'
    server = socketio.Server(client_manager=socketio.RedisManager(url))

:param url: The connection URL for the Redis server. For a default Redis
            store running on the same host, use ``redis://``.  To use a
            TLS connection, use ``rediss://``. To use Redis Sentinel, use
            ``redis+sentinel://`` with a comma-separated list of hosts
            and the service name after the db in the URL path. Example:
            ``redis+sentinel://user:pw@host1:1234,host2:2345/0/myredis``.
:param channel: The channel name on which the server sends and receives
                notifications. Must be the same in all the servers.
:param write_only: If set to ``True``, only initialize to emit events. The
                   default of ``False`` initializes the class for emitting
                   and receiving.
:param redis_options: additional keyword arguments to be passed to
                      ``Redis.from_url()`` or ``Sentinel()``.
redisc                    > [         c  [        c  [        S5      e[        TU ]  X#US9  Xl        U=(       d    0 U l        U R                  5         g )NzdRedis package is not installed (Run "pip install redis" or "pip install valkey" in your virtualenv).)channel
write_onlylogger)r1   valkeyRuntimeErrorsuper__init__	redis_urlredis_options_redis_connect)selfr"   r3   r4   r5   r;   	__class__s         r+   r9   RedisManager.__init__Q   sP    =V^  6 7 7 	O*0br-   c                   > [         TU ]  5         SnU R                  R                  S:X  a  SSKJn  U" S5      nO(SU R                  R                  ;   a  SSKJn  U" S5      nU(       d"  [        SU R                  R                  -   5      eg )	NTeventletr   )is_monkey_patchedsocketgevent)is_module_patchedz<Redis requires a monkey patched socket library to work with )	r8   
initializeserver
async_modeeventlet.patcherrB   gevent.monkeyrE   r7   )r=   monkey_patchedrB   rE   r>   s       r+   rF   RedisManager.initialize]   s}    ;;!!Z/:.x8N///7.x8N++0012 2 r-   c                 R   [        U R                  5      nUR                  R                  SS5      S   R	                  5       nUS:X  a%  [
        b  [        c  [        S5      e[
        [        4$ US:X  a%  [        b  [        c  [        S5      e[        [        4$ SU 3n[        U5      e)	N+r   r   r1   zLRedis package is not installed (Run "pip install redis" in your virtualenv).r6   zNValkey package is not installed (Run "pip install valkey" in your virtualenv).zUnsupported Redis URL schema: )r   r:   r   r   lowerr1   r   r7   r6   r   r   )r=   r#   schema	error_msgs       r+   _get_redis_module_and_error(RedisManager._get_redis_module_and_errorl   s    dnn-
""((a0399;W}
 2" $: ; ; *$$X~!4" $: ; ; ;&&4VH=	##r-   c                    U R                  5       u  p[        U R                  5      nUR                  S;   a|  [	        U R                  5      u  pEnU R
                  nUR                  U5        UR                  R                  " U40 UD6nUR                  U=(       d    U R                  5      U l        O6UR                  R                  " U R                  40 U R
                  D6U l        U R                  R                  SS9U l        g )Nr   T)ignore_subscribe_messages)rR   r   r:   r   r,   r;   updatesentinelSentinel
master_forr3   r1   Redisfrom_urlpubsub)	r=   module_r#   r$   r)   connection_kwargsr(   rW   s	            r+   r<   RedisManager._redis_connect~   s    446	dnn-
 EE(8 7I%6''FMM+,//	DVDH!,,\-IT\\JDJ..t~~ E151C1CEDJjj''$'Gr-   c                    SnU R                  5       u  p4  U(       d  U R                  5         U R                  R                  U R                  [
        R                  " U5      5      $ ! U aV  nU(       a&  [        R                  SS[        U5      0S9  Sn S nAO,[        R                  SS[        U5      0S9   S nAg S nAff = fM  )NTz#Cannot publish to redis... retryingredis_exceptionextraFz$Cannot publish to redis... giving up)
rR   r<   r1   publishr3   r   dumpsr5   errorstr)r=   dataretryr^   rg   excs         r+   _publishRedisManager._publish   s    335'')zz))$,,

48HII LL=0#c(; !  "ELL>0#c(; !   s   AA( (C.(B?B??Cc              #     #    SnSnU R                  5       u  p4  U(       a7  U R                  5         U R                  R                  U R                  5        SnU R                  R                  5        S h  vN   Mc   N! U aR  n[        R                  SU S3S[        U5      0S9  Sn[        R                  " U5        US-  nUS	:  a  S	n S nANWS nAff = f7f)
Nr   FTz)Cannot receive from redis... retrying in z secsrb   rc   r   <   )rR   r<   r\   	subscriber3   listenr5   rg   rh   timesleep)r=   retry_sleepconnectr^   rg   rk   s         r+   _redis_listen_with_retries'RedisManager._redis_listen_with_retries   s     335%'')KK))$,,7"#K;;--///  0 % ,,7=?$5s3x#@  B 

;'q #"$K%sB   CAA? 6A=7A? ;C=A? ?CACCCCc              #   N  #    U R                   R                  S5      nU R                  R                  U R                   5        U R	                  5        H(  nUS   U:X  d  M  US   S:X  d  M  SU;   d  M!  US   v   M*     U R                  R                  U R                   5        g 7f)Nzutf-8r3   typemessageri   )r3   encoder\   rp   rv   unsubscribe)r=   r3   rz   s      r+   _listenRedisManager._listen   s     ,,%%g.dll+668Gy!W,FOy0Vw5Ffo% 9 	-s   AB%!B%,B%41B%)r\   r1   r;   r:   )zredis://localhost:6379/0r	   FNN)__name__
__module____qualname____firstlineno____doc__namer9   rF   rR   r<   rl   rv   r}   __static_attributes____classcell__)r>   s   @r+   r/   r/   3   s>    6 D?I>B
2$$H,%*. .r-   r/   )loggingrr   urllib.parser   r1   redis.exceptionsr   ImportErrorr6   valkey.exceptionsr   engineior   pubsub_managerr   	getLoggerr5   r,   r/    r-   r+   <module>r      s      !+
-
  )			:	&+4L.= L.W  EJ  FKs"   
A 
A" 
AA"
A/.A/