o
    >h                     @   sH   d dl Z d dlZd dlmZ dd Zdd Zdd Zd	d
 Zdd Z	dS )    N)asyncioc                 C   sF   |dkrdS t | tr| d} t| d@ }dt| }t|| S )zn
    Maps the value to a node value between 0 and 4095
    using CRC, then down to one of the ring nodes.
       r   utf8i  i   )
isinstancestrencodebinasciicrc32floatint)value	ring_sizebigvalring_divisor r   t/var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/channels_redis/utils.py_consistent_hash   s   

r   c                    s(    j  fdd}t|  _ d S )Nc                    s@    j v rj   }j  =  |  | _| j|i |S )N)_layersrun_until_completeflushclose)selfargskwargslayerlooporiginal_implproxyr   r   _wrapper   s   

z_wrap_close.<locals>._wrapper)r   types
MethodType)r   r   r   r   r   r   _wrap_close   s   	r"   c                    sB   z| j ddI dH  W dS  ty    | jddI dH  Y dS w )zF
    Handle compatibility with redis-py 4.x and 5.x close methods
    T)close_connection_poolN)acloseAttributeErrorr   )
connectionr   r   r   _close_redis&   s   r'   c                 C   s   | sddigS t | ttfrtdg }| D ](}t |tr#|| qt |ttfr7||d |d d q|d|i q|S )z
    Takes the value of the "hosts" argument and returns
    a list of kwargs to use for the Redis connection constructor.
    addresszredis://localhost:6379z?You must pass a list of Redis hosts, even if there is only one.r   r   )hostport)r   r   bytes
ValueErrordictappendtuplelist)hostsresultentryr   r   r   decode_hosts0   s   

r4   c                 C   s   |   } d| v r| d}tjj|fi | S | dd}|dur=| d}| dd}tjj|tjj||dfi | S tjdi | S )z~
    Takes the value of the "host" argument and returns a suited connection pool to
    the corresponding redis instance.
    r(   master_nameN	sentinelssentinel_kwargs)r7   r   )copypopaioredisConnectionPoolfrom_urlsentinelSentinelConnectionPoolSentinel)r)   r(   r5   r6   r7   r   r   r   create_poolJ   s   

r@   )
r   r    redisr   r:   r   r"   r'   r4   r@   r   r   r   r   <module>   s    
