
    !ni                          d dl Z d dlZd dlmZ 	 d dlZd dlmZ 	 d dlZd dl	m
Z
 d dlmZ ddlmZ  e j                  d      Zd	 Z G d
 de      Zy# e$ r dZdZY Cw xY w# e$ r dZdZ
Y Gw xY w)    N)urlparse)
RedisError)ValkeyError)json   )PubSubManagersocketioc                 d   t        |       }|j                  dvrt        d      g }|j                  j	                  d      d   j	                  d      D ]3  }|j                  dd      \  }}|j                  |t        |      f       5 i }|j                  r|j                  |d<   |j                  r|j                  |d	<   d
}|j                  r]|j                  j	                  d      }t        |      dk\  r|d   dk7  rt        |d         |d<   t        |      dk\  r|d   dk7  r|d   }|||fS )z}Parse 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	            V/home/homepc/tiktok-worker/venv/lib/python3.12/site-packages/socketio/redis_manager.pyparse_redis_sentinel_urlr,      s4    #J EE566I&&,,S1"5;;C@ ,	%%c1-
d$D	*+, F'00z'00zL%%c*u:?uQx2~uQx=F4Lu:?uQx2~ 8LlF**    c                   T     e Zd ZdZdZ	 	 d
 fd	Z fdZd Zd Zd Z	d Z
d	 Z xZS )RedisManagera  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                 x    t         |   |||       || _        |xs i | _        d| _        d | _        d | _        y )N)channel
write_onlyloggerF)super__init__	redis_urlredis_options	connectedr0   pubsub)selfr"   r2   r3   r4   r8   	__class__s         r+   r6   zRedisManager.__init__Q   s@    ZO*0b
r-   c                 
   t         |           d}| j                  j                  dk(  rddlm}  |d      }n&d| j                  j                  v rddlm}  |d      }|s"t        d| j                  j                  z         y )	NTeventletr   )is_monkey_patchedsocketgevent)is_module_patchedz<Redis requires a monkey patched socket library to work with )	r5   
initializeserver
async_modeeventlet.patcherr?   gevent.monkeyrB   RuntimeError)r;   monkey_patchedr?   rB   r<   s       r+   rC   zRedisManager.initializeZ   s}    ;;!!Z/:.x8N///7.x8N++0012 2 r-   c                    t        | j                        }|j                  j                  dd      d   j	                         }|dv r#t
        t        t        d      t
        t        fS |dv r#t        t        t        d      t        t        fS |dk(  r;t
        t        #t        t        t        d	      t        t        fS t
        t        fS d
| }t        |      )N+r   r   )r0   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   r7   r   r   lowerr0   r   rH   rM   r   r   )r;   r#   r   	error_msgs       r+   _get_redis_module_and_errorz(RedisManager._get_redis_module_and_errori   s    dnn-
""((a0399;((}
 2" $: ; ; *$$**~!4" $: ; ; ;&&V}
 2>[%8& (> ? ?
 ";..j((4VH=	##r-   c                    | j                         \  }}t        | j                        }|j                  dv rxt	        | j                        \  }}}| j
                  }|j                  |        |j                  j                  |fi |}|j                  |xs | j                        | _        n6 |j                  j                  | j                  fi | j
                  | _        | j                  j                  d      | _        d| _        y )Nr   T)ignore_subscribe_messages)rR   r   r7   r   r,   r8   updatesentinelSentinel
master_forr2   r0   Redisfrom_urlr:   r9   )	r;   module_r#   r$   r)   connection_kwargsr(   rV   s	            r+   _redis_connectzRedisManager._redis_connect   s    446	dnn-
 EE(8 7I|%6''FMM+,/v//	DVDH!,,\-IT\\JDJ...t~~ E151C1CEDJjj''$'Gr-   c           	         | j                         \  }}t        ddd      D ]Y  }	 | j                  s| j                          | j                  j                  | j                  t        j                  |            c S  y # |$ ra}|dkD  r*t        j                  ddt        |      i       d| _        n(t        j                  ddt        |      i       Y d }~ y Y d }~d }~ww xY w)	Nr   r   r   z#Cannot publish to redis... retryingredis_exceptionextraFz$Cannot publish to redis... giving up)rR   ranger9   r^   r0   publishr2   r   dumpsr4   errorstr)r;   datar\   rf   retries_leftexcs         r+   _publishzRedisManager._publish   s    3355!!R, 	L~~'')zz))$,,

48HII		
  !#LL=0#c(; !  &+DNLL>0#c(; !   #s   AA>>C$ACC$c              #     K   | j                         \  }}d}d}	 	 |s7| j                          | j                  j                  | j                         d}| j                  j                         E d {    ]7 # |$ rS}t        j                  d| ddt        |      i       d}t        j                  |       |dz  }|dkD  rd}Y d }~Vd }~ww xY ww)	Nr   Fz)Cannot receive from redis... retrying in z secsr`   ra   r   <   )rR   r^   r:   	subscriber2   listenr4   rf   rg   timesleep)r;   r\   rf   retry_sleep
subscribedrj   s         r+   _redis_listen_with_retriesz'RedisManager._redis_listen_with_retries   s     3355
%!'')KK))$,,7"#K;;--///  0 % ,,7=?$5s3x#@  B #


;'q #"$K%sB   CAA: 2A83A: 7C8A: :C?A	CCCCc              #      K   | j                   j                  d      }| j                         D ]   }|d   |k(  s|d   dk(  sd|v s|d    " | j                  j	                  | j                          y w)Nzutf-8r2   typemessagerh   )r2   encodert   r:   unsubscribe)r;   r2   rw   s      r+   _listenzRedisManager._listen   st     ,,%%g.668 	&Gy!W,FOy0Vw5Ffo%	& 	-s   7A7A7A7/A7)zredis://localhost:6379/0r	   FNN)__name__
__module____qualname____doc__namer6   rC   rR   r^   rk   rt   rz   __classcell__)r<   s   @r+   r/   r/   3   s8    6 D?I>B2$: *%*.r-   r/   )loggingrp   urllib.parser   r0   redis.exceptionsr   ImportErrorrM   valkey.exceptionsr   engineior   pubsub_managerr   	getLoggerr4   r,   r/    r-   r+   <module>r      s      !+
-
  )			:	&+4S.= S.W  EJ  FKs"   
A 
A! 	AA!	A-,A-