a
    a*j                     @  st   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZ eddd	ed
dd	G dd deZdS )    )annotationsN)versionchanged)Version)RedisStorage)OptionalUnionz3.14.0z_
Dropped support for the :pypi:`redis-py-cluster` library
which has been abandoned/deprecated.
)versionreasonz2.5.0aa  
Cluster support was provided by the :pypi:`redis-py-cluster` library
which has been absorbed into the official :pypi:`redis` client. By
default the :class:`redis.cluster.RedisCluster` client will be used
however if the version of the package is lower than ``4.2.0`` the implementation
will fallback to trying to use :class:`rediscluster.RedisCluster`.
c                      s`   e Zd ZU dZdgZddiZded< dediZdd
dddd fddZ	ddddZ
  ZS )RedisClusterStoragezY
    Rate limit storage with redis cluster as backend

    Depends on :pypi:`redis`.
    zredis+clusterZmax_connectionsi  z"dict[str, Union[float, str, bool]]DEFAULT_OPTIONSredisz4.2.0FstrboolzUnion[float, str, bool]None)uriwrap_exceptionsoptionsreturnc                   s  t j|}i }|jr |j|d< |jr0|j|d< |jdd }g }|j|d dD ]$}|d\}	}
||	t	|
f qXd _
i  j||} jd j _ fd	d
|D } jjjf d|i| _
 j
sJ  | tt j||fi | dS )a  
        :param uri: url of the form
         ``redis+cluster://[:password]@host:port,host:port``
        :param wrap_exceptions: Whether to wrap storage exceptions in
         :exc:`limits.errors.StorageError` before raising it.
        :param options: all remaining keyword arguments are passed
         directly to the constructor of :class:`redis.cluster.RedisCluster`
        :raise ConfigurationError: when the :pypi:`redis` library is not
         available or if the redis cluster cannot be reached.
        usernamepassword@   N,:r   c                   s   g | ]} j jj| qS  )
dependencyclusterZClusterNode).0cselfr   X/var/www/html/assistant/venv/lib/python3.9/site-packages/limits/storage/redis_cluster.py
<listcomp>Q       z0RedisClusterStorage.__init__.<locals>.<listcomp>startup_nodes)urllibparseurlparser   r   netlocfindsplitappendintstorager   dependenciesmoduler   r   ZRedisClusterZinitialize_storagesuperr   __init__)r    r   r   r   parsedZparsed_authsepZcluster_hostslochostportZmerged_optionsr$   	__class__r   r!   r1   0   s.    



zRedisClusterStorage.__init__zOptional[int])r   c                   sR   |  d}d}| j D ]4}| j|  |}|t fdd|D 7 }q|S )a  
        Redis Clusters are sharded and deleting across shards
        can't be done atomically. Because of this, this reset loops over all
        keys that are prefixed with ``self.PREFIX`` and calls delete on them,
        one at a time.

        .. warning::
         This operation was not tested with extremely large data sets.
         On a large production based system, care should be taken with its
         usage as it could be slow on very large data sets*r   c                   s   g | ]}  |d qS )zutf-8)deletedecode)r   knoder   r!   r"   j   r#   z-RedisClusterStorage.reset.<locals>.<listcomp>)Zprefixed_keyr-   Zget_primariesZget_redis_connectionkeyssum)r    prefixcountprimaryr?   r   r=   r!   resetY   s    

zRedisClusterStorage.reset)F)__name__
__module____qualname____doc__ZSTORAGE_SCHEMEr   __annotations__r   ZDEPENDENCIESr1   rD   __classcell__r   r   r7   r!   r
      s   
 )r
   )
__future__r   r%   Zdeprecated.sphinxr   Zpackaging.versionr   Zlimits.storage.redisr   Zlimits.typingr   r   r
   r   r   r   r!   <module>   s   
