o
    ¨Ýjƒ  ã                   @   sÜ   d dl Z d dlmZ zd dlm Z d dlmZ W n# ey;   zd dlZd dlmZ W n ey8   dZdZY nw Y nw zd dl	m Z
 d dlmZ W n eyW   dZ
dZY nw ddlmZ ddlmZ G d	d
„ d
eƒZdS )é    N)Úurlparse)Úasyncio)Ú
RedisError)ÚValkeyErroré   )ÚAsyncPubSubManager)Úparse_redis_sentinel_urlc                       sR   e Zd ZdZdZ		d‡ fdd„	Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
‡  ZS )ÚAsyncRedisManageraÉ  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()``.
    Úaioredisúredis://localhost:6379/0ÚsocketioFNc                    sR   t rtt jdƒstdƒ‚tƒ j||||d� || _|pi | _d| _d | _	d | _
d S )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__© úW/var/www/html/chatgpt/venv/lib/python3.10/site-packages/socketio/async_redis_manager.pyr   A   s   
ÿÿ

zAsyncRedisManager.__init__c                 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   r   ÚschemeÚsplitÚlowerr
   r   r   Ú	aiovalkeyr   Ú
ValueError)r   Ú
parsed_urlr%   Ú	error_msgr    r    r!   Ú_get_redis_moduleN   s$   

z#AsyncRedisManager._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 )N>   zvalkey+sentinelzredis+sentinelT)Zignore_subscribe_messages)r,   r   r   r%   r   r   ÚupdateÚsentinelZSentinelZ
master_forr   r   r   r   r   r   )r   Úmoduler*   Z	sentinelsÚservice_nameZconnection_kwargsÚkwargsr.   r    r    r!   Ú_redis_connectk   s   

ÿ
ÿ
z AsyncRedisManager._redis_connectc                 Ã   sÂ   �t dddƒD ]W}z| js|  ¡  | j | j| j |¡¡I d H W   S  ty^ } 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   z#Cannot publish to redis... retryingÚredis_exception©ÚextraFz$Cannot publish to redis... giving up)Úranger   r2   r   Úpublishr   r   ÚdumpsÚ	ExceptionÚ_get_loggerÚerrorÚstr)r   ÚdataZretries_leftÚexcr    r    r!   Ú_publish{   s0   €ÿ
þ
þû€ûúzAsyncRedisManager._publishc              
   C  sÆ   �d}d}	 z$|s|   ¡  | j | j¡I d H  d}| j ¡ 2 z	3 d H W }|V  q6 W n7 tya } z+|  ¡ jd|› d�dt|ƒid� d}t	 
|¡I d H  |d9 }|d	krWd	}W Y d }~nd }~ww q)
Nr   FTz)Cannot receive from redis... retrying in z secsr4   r5   é   é<   )r2   r   Ú	subscriber   Úlistenr:   r;   r<   r=   r   Úsleep)r   Zretry_sleepZ
subscribedÚmessager?   r    r    r!   Ú_redis_listen_with_retriesŽ   s6   €ÿÿ
ý€÷øz,AsyncRedisManager._redis_listen_with_retriesc                 C  sh   �| j  d¡}|  ¡ 2 z3 d H W }|d |kr&|d dkr&d|v r&|d V  q6 | j | j ¡I d H  d S )Nzutf-8r   ÚtyperF   r>   )r   ÚencoderG   r   Zunsubscribe)r   r   rF   r    r    r!   Ú_listen¤   s   €
€ýzAsyncRedisManager._listen)r   r   FNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer   r,   r2   r@   rG   rJ   Ú__classcell__r    r    r   r!   r	      s    $ÿr	   )r   Úurllib.parser   r   r
   Zredis.exceptionsr   ÚImportErrorZaioredis.exceptionsr#   r(   Zvalkey.exceptionsr   Zasync_pubsub_managerr   Zredis_managerr   r	   r    r    r    r!   Ú<module>   s2    þ€üþ