
    wg3                         d dl Z d dlZd dlZ	 d dlZddlmZ  e j                  d      Z G d de      Z	y# e$ r dZY -w xY w)    N   )PubSubManagersocketioc                   N     e Zd ZdZdZ	 	 d	 fd	Z f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 an
                SSL connection, use ``rediss://``.
    :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()``.
    redisc                     t         t        d      || _        |xs i | _        | j	                          t
        |   |||       y )NzLRedis package is not installed (Run "pip install redis" in your virtualenv).)channel
write_onlylogger)r   RuntimeError	redis_urlredis_options_redis_connectsuper__init__)selfurlr
   r   r   r   	__class__s         [/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/socketio/redis_manager.pyr   zRedisManager.__init__*   sP    =  . / / *0bZO    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 )	r   
initializeserver
async_modeeventlet.patcherr   gevent.monkeyr   r   )r   monkey_patchedr   r   r   s       r   r   zRedisManager.initialize5   s}    ;;!!Z/:.x8N///7.x8N++0012 2 r   c                     t        j                  j                  | j                  fi | j                  | _         | j                   j                  d      | _        y )NT)ignore_subscribe_messages)r   Redisfrom_urlr   r   pubsub)r   s    r   r   zRedisManager._redis_connectD   sG    [[))$.. @,0,>,>@
jj''$'Gr   c                 J   d}	 	 |s| j                          | j                  j                  | j                  t	        j
                  |            S # t        j                  j                  $ r4 |rt        j                  d       d}nt        j                  d       Y y Y nw xY w)NTz#Cannot publish to redis... retryingFz$Cannot publish to redis... giving up)
r   r   publishr
   pickledumps
exceptions
RedisErrorr   error)r   dataretrys      r   _publishzRedisManager._publishI   s    
'')zz))$,,T8JKK##.. LL!FG!ELL!GH  s   A
A AB! B!c              #     K   d}d}	 	 |r7| j                          | j                  j                  | j                         d}| j                  j	                         E d {    ]7 # t
        j                  j                  $ rJ t        j                  dj                  |             d}t        j                  |       |dz  }|dkD  rd}Y iw xY ww)Nr   FTz0Cannot receive from redis... retrying in {} secs   <   )r   r(   	subscriber
   listenr   r-   r.   r   r/   formattimesleep)r   retry_sleepconnects      r   _redis_listen_with_retriesz'RedisManager._redis_listen_with_retriesX   s     %'')KK))$,,7"#K;;--///  0##.. % 3396+3FH

;'q #"$K%s<   CAA' A% A' $C%A' 'A$CCCCc              #   <  K   | j                   j                  d      }| j                  j                  | j                          | j	                         D ]   }|d   |k(  s|d   dk(  sd|v s|d    " | j                  j                  | j                          y w)Nzutf-8r
   typemessager0   )r
   encoder(   r6   r=   unsubscribe)r   r
   r@   s      r   _listenzRedisManager._listenk   s     ,,%%g.dll+668 	&Gy!W,FOy0Vw5Ffo%	& 	-s   ABB(B-/B)zredis://localhost:6379/0r   FNN)__name__
__module____qualname____doc__namer   r   r   r2   r=   rC   __classcell__)r   s   @r   r   r      s5    0 D?I>B	P2H
%&.r   r   )
loggingr+   r9   r   ImportErrorpubsub_managerr   	getLoggerr   r    r   r   <module>rO      sQ       *			:	&c.= c.  Es   6 A A 