o
    ¨Ýj"  ã                   @   s¤   d dl Z d dlmZ zd dlZd dlmZ W n ey#   dZdZY nw zd dlZd dlm	Z	 W n ey=   dZdZ	Y nw ddl
mZ dd„ ZG d	d
„ d
eƒZdS )é    N)Úurlparse)Ú
RedisError)ÚValkeyErroré   )ÚPubSubManagerc           	      C   sè   t | ƒ}|jdvrtdƒ‚g }|j d¡d  d¡D ]}| dd¡\}}| |t|ƒf¡ qi }|jr8|j|d< |j	r@|j	|d	< d
}|j
ro|j
 d¡}t|ƒdkr_|d dkr_t|d ƒ|d< t|ƒdkro|d dkro|d }|||fS )z}Parse a Redis Sentinel URL with the format:
    redis+sentinel://[:password]@host1:port1,host2:port2,.../db/service_name
    ¾   zvalkey+sentinelzredis+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Úparts© r%   úQ/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/redis_manager.pyÚparse_redis_sentinel_url   s(   



r'   c                       s^   e Zd ZdZdZ		d‡ fdd„	Z‡ fd	d
„Zdd„ Zdd„ Zdd„ Z	dd„ Z
dd„ Z‡  ZS )ÚRedisManagera6  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. 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()``.
    Úredisúredis://localhost:6379/0ÚsocketioFNc                    s:   t ƒ j||||d� || _|pi | _d| _d | _d | _d S )N)ÚchannelÚ
write_onlyÚloggerÚjsonF)ÚsuperÚ__init__Ú	redis_urlÚredis_optionsÚ	connectedr)   Úpubsub)Úselfr   r,   r-   r.   r/   r3   ©Ú	__class__r%   r&   r1   W   s   ÿ

zRedisManager.__init__c                    sh   t ƒ  ¡  d}| jjdkrddlm} |dƒ}nd| jjv r(ddlm} |dƒ}|s2td| jj ƒ‚d S )	NTZeventletr   )Úis_monkey_patchedÚsocketZgevent)Úis_module_patchedz<Redis requires a monkey patched socket library to work with )	r0   Ú
initializeÚserverZ
async_modeZeventlet.patcherr9   Zgevent.monkeyr;   ÚRuntimeError)r6   Zmonkey_patchedr9   r;   r7   r%   r&   r<   a   s   

ÿÿÿzRedisManager.initializec                 C   s²   t | jƒ}|j dd¡d  ¡ }|dv r"td u std u r tdƒ‚tS |dv r4td u s.t	d u r2tdƒ‚tS |dkrPtd u s@td u rNtd u sHt	d u rLtd	ƒ‚tS tS d
|› �}t
|ƒ‚)Nú+r   r   )r)   ZredisszLRedis package is not installed (Run "pip install redis" in your virtualenv).)ÚvalkeyZ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   r2   r   r   Úlowerr)   r   r>   r@   r   r   )r6   r   r   Ú	error_msgr%   r%   r&   Ú_get_redis_modulep   s$   

zRedisManager._get_redis_modulec                 C   s˜   |   ¡ }t| jƒ}|jdv r2t| jƒ\}}}| j}| |¡ |jj|fi |¤Ž}| 	|p.| j
¡| _n|jj| jfi | j¤Ž| _| jjdd�| _d| _d S )Nr   T)Zignore_subscribe_messages)rD   r   r2   r   r'   r3   ÚupdateÚsentinelZSentinelZ
master_forr,   r)   ZRedisZfrom_urlr5   r4   )r6   Úmoduler   r   r#   Zconnection_kwargsr"   rF   r%   r%   r&   Ú_redis_connect�   s   

ÿ
ÿ
zRedisManager._redis_connectc                 C   sº   t dddƒD ]T}z| js|  ¡  | j | j| j |¡¡W   S  tyZ } z0|dkr;|  	¡ j
ddt|ƒid� d| _n|  	¡ j
ddt|ƒid� W Y d }~ d S W Y d }~qd }~ww d S )	Nr   r	   r   z#Cannot publish to redis... retryingÚredis_exception©ÚextraFz$Cannot publish to redis... giving up)Úranger4   rH   r)   Úpublishr,   r/   ÚdumpsÚ	ExceptionÚ_get_loggerÚerrorÚstr)r6   ÚdataZretries_leftÚexcr%   r%   r&   Ú_publish�   s*   
þ
þú€úûzRedisManager._publishc              
   c   sª   � d}d}	 z|s|   ¡  | j | j¡ d}| j ¡ E d H  W n4 tyS } z(|  ¡ jd|› d�dt|ƒid� d}t	 
|¡ |d9 }|d	krId	}W Y d }~nd }~ww q)
Nr   FTz)Cannot receive from redis... retrying in z secsrI   rJ   r   é<   )rH   r5   Ú	subscriber,   ÚlistenrO   rP   rQ   rR   ÚtimeÚsleep)r6   Zretry_sleepZ
subscribedrT   r%   r%   r&   Ú_redis_listen_with_retries±   s2   €ÿ
ý
€÷ùz'RedisManager._redis_listen_with_retriesc                 c   sX   � | j  d¡}|  ¡ D ]}|d |kr"|d dkr"d|v r"|d V  q| j | j ¡ d S )Nzutf-8r,   ÚtypeÚmessagerS   )r,   Úencoder[   r5   Zunsubscribe)r6   r,   r]   r%   r%   r&   Ú_listenÆ   s   €
€zRedisManager._listen)r*   r+   FNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer1   r<   rD   rH   rU   r[   r_   Ú__classcell__r%   r%   r7   r&   r(   /   s    %ÿ
r(   )rY   Úurllib.parser   r)   Zredis.exceptionsr   ÚImportErrorr@   Zvalkey.exceptionsr   Zpubsub_managerr   r'   r(   r%   r%   r%   r&   Ú<module>   s&    þþ