
    i                         S SK r S SKJr   S SKJ r  S SKJr   S SK	J r
  S SKJr  SSKJr  SSKJr   " S	 S
\5      rg! \ a     S SKrS SKJr   N8! \ a    SrSr  NEf = ff = f! \ a    Sr
Sr NLf = f)    N)urlparse)asyncio)
RedisError)ValkeyError   )AsyncPubSubManager)parse_redis_sentinel_urlc                   V   ^  \ rS rSrSrSr  SU 4S jjrS rS rS r	S r
S	 rS
rU =r$ )AsyncRedisManager   aM  Redis based client manager for asyncio servers.

This class implements a Redis backend for event sharing across multiple
processes.

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

    url = 'redis://hostname:port/0'
    server = socketio.AsyncServer(
        client_manager=socketio.AsyncRedisManager(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. A write-only instance can be used
                   independently of the server to emit to clients from an
                   external process.
:param logger: a custom logger to log it. If not given, the server logger
               is used.
:param json: An alternative JSON module to use for encoding and decoding
             packets. Custom json modules must have ``dumps`` and ``loads``
             functions that are compatible with the standard library
             versions. This setting is only used when ``write_only`` is set
             to ``True``. Otherwise the JSON module configured in the
             server is used.
:param redis_options: additional keyword arguments to be passed to
                      ``Redis.from_url()`` or ``Sentinel()``.
aioredisc                    > [         (       a*  [        [         R                  S5      (       d  [        S5      e[        TU ]  X#UUS9  Xl        U=(       d    0 U l        SU l        S U l	        S U l
        g )Nfrom_urlz*Version 2 of aioredis package is required.)channel
write_onlyloggerjsonF)r   hasattrRedisRuntimeErrorsuper__init__	redis_urlredis_options	connectedredispubsub)selfurlr   r   r   r   r   	__class__s          Y/home/admin/cozy_coffee/venv/lib/python3.13/site-packages/socketio/async_redis_manager.pyr   AsyncRedisManager.__init__A   se    8HNNJ77KLL" 	 	$*0b
    c                    [        U R                  5      nUR                  R                  SS5      S   R	                  5       nUS;   a%  [
        b  [        c  [        S5      e[
        [        4$ US;   a%  [        b  [        c  [        S5      e[        [        4$ US:X  a?  [
        b  [        c%  [        b  [        c  [        S	5      e[        [        4$ [
        [        4$ S
U 3n[        U5      e)N+r   r   )r   redisszLRedis package is not installed (Run "pip install redis" in your virtualenv).)valkeyvalkeyszNValkey package is not installed (Run "pip install valkey" in your virtualenv).unixzdRedis package is not installed (Run "pip install redis" or "pip install valkey" in your virtualenv).zUnsupported Redis URL scheme: )r   r   schemesplitlowerr   r   r   	aiovalkeyr   
ValueError)r   
parsed_urlr*   	error_msgs       r!   _get_redis_module_and_error-AsyncRedisManager._get_redis_module_and_errorN   s    dnn-
""((a0399;((:#5" $: ; ; Z''** K$7" $: ; ; k))V:#5$(;& (> ? ?
 %k11++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        SU l        g )N>   redis+sentinelvalkey+sentinelT)ignore_subscribe_messages)r1   r   r   r*   r	   r   updatesentinelSentinel
master_forr   r   r   r   r   r   )	r   module_r/   	sentinelsservice_nameconnection_kwargskwargsr8   s	            r!   _redis_connect AsyncRedisManager._redis_connectk   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           	         #    U R                  5       u  p#[        SSS5       Hl  n U R                  (       d  U R                  5         U R                  R                  U R                  U R                  R                  U5      5      I S h  vN s  $    g  N	! U ap  nUS:  a6  U R                  5       R                  SS[        U5      0S9  SU l         S nAM  U R                  5       R                  SS[        U5      0S9   S nA  g S nAff = f7f)	Nr   r   z#Cannot publish to redis... retryingredis_exceptionextraFz$Cannot publish to redis... giving up)r1   ranger   rA   r   publishr   r   dumps_get_loggererrorstr)r   datar<   rL   retries_leftexcs         r!   _publishAsyncRedisManager._publish{   s     335!!R,L~~'')!ZZ//LL$))//$"79 9 9	 -9 !#$$&,,#0#c(; - = &+DNN$$&,,$0#c(; - =
 sM   #DA#B	B
BDBD6DD)D DDDc                  #    U R                  5       u  pSnSn  U(       d?  U R                  5         U R                  R                  U R                  5      I S h  vN   SnU R                  R                  5         S h  vN nU7v   M   N0 N
 On! U ae  nU R                  5       R                  SU S3S[        U5      0S9  Sn[        R                  " U5      I S h  vN    US-  nUS:  a  Sn S nAOS nAff = fM  7f)	Nr   Fz)Cannot receive from redis... retrying in z secsrE   rF      <   )r1   rA   r   	subscriber   listenrK   rL   rM   r   sleep)r   r<   rL   retry_sleep
subscribedmessagerP   s          r!   _redis_listen_with_retries,AsyncRedisManager._redis_listen_with_retries   s    335
%!'')++//==="#K%)[[%7%7%9 "'!M >"%9 	%  "(( *8,7=*? 1B3s8/L ) N #
mmK000q #"$K	% sk   D?B B B ;B?B B	B BB DC<AC7C" C72D7C<<Dc                &  #    U R                   R                  S5      nU R                  5         S h  vN nUS   U:X  d  M  US   S:X  d  M  SU;   d  M'  US   7v   M1   N,
 U R                  R	                  U R                   5      I S h  vN    g 7f)Nzutf-8r   typer[   rN   )r   encoder\   r   unsubscribe)r   r   r[   s      r!   _listenAsyncRedisManager._listen   s|     ,,%%g.!<<> 	&'y!W,FOy0Vw5Ffo%	&> kk%%dll333sC   ,BA!AA!BBB
BA!!)B
BB)r   r   r   r   r   )zredis://localhost:6379/0socketioFNNN)__name__
__module____qualname____firstlineno____doc__namer   r1   rA   rQ   r\   rb   __static_attributes____classcell__)r    s   @r!   r   r      s9    #H D?IIM$: .%.4 4r#   r   )r   urllib.parser   r   r   redis.exceptionsr   ImportErroraioredis.exceptionsr'   r-   valkey.exceptionsr   async_pubsub_managerr   redis_managerr	   r    r#   r!   <module>ru      s     !	)++-
 5 3U4* U4'  2 
	  IKs8   = A% A"
A
AA"AA"%
A21A2