U
    ]g                     @   s   d dl 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	 d dl
mZ d dlmZmZmZmZmZ edd	d
G dd deZdS )    N)cast)versionchanged)Version)ConfigurationError)RedisStorage)DictListOptionalTupleUnionz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`.
)versionreasonc                       s   e Zd ZU dZdgZddiZeeee	ee
f f ed< ededdZeee	ee
f d	d
 fddZeeeef  ee	ee
f d	dddZeeeef  ee	ee
f d	dddZee dddZ  ZS )RedisClusterStoragezY
    Rate limit storage with redis cluster as backend

    Depends on :pypi:`redis`.
    zredis+clusterZmax_connectionsi  DEFAULT_OPTIONSz4.2.0z2.0.0)redisredisclusterN)uri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| _
d| _| j|f| j|| | j
st| | tt| j|f| dS )	a  
        :param uri: url of the form
         ``redis+cluster://[:password]@host:port,host:port``
        :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,:F)urllibparseurlparser   r   netlocfindsplitappendintstorageusing_redis_py"_RedisClusterStorage__pick_storager   AssertionErrorZinitialize_storagesuperr   __init__)
selfr   r   parsedZparsed_authsepcluster_hostslochostport	__class__ @/tmp/pip-unpacked-wheel-kizsipjx/limits/storage/redis_cluster.pyr(   +   s*    	




zRedisClusterStorage.__init__)r,   r   r   c                    sx   zB| j d j  fdd|D } jjf d|i|| _d| _W d S  tk
rr   | j|f| | jsntdY nX d S )Nr   c                    s   g | ]} j j| qS r2   )clusterZClusterNode.0cZredis_pyr2   r3   
<listcomp>P   s     z6RedisClusterStorage.__pick_storage.<locals>.<listcomp>startup_nodesTztUnable to find an implementation for redis cluster Cluster support requires either redis-py>=4.2 or redis-py-cluster)dependenciesmoduler4   RedisClusterr#   r$   r   7_RedisClusterStorage__use_legacy_cluster_implementation)r)   r,   r   r:   r2   r8   r3   Z__pick_storageK   s     
z"RedisClusterStorage.__pick_storagec                 K   s:   | j d j}td |jf ddd |D i|| _d S )Nr   a/  Using redis-py-cluster is deprecated as the library has been absorbed by redis-py (>=4.2). The support will be eventually  removed from the limits library and is no longer tested  against since version: 2.6. To get rid of this warning,  uninstall redis-py-cluster and ensure redis-py>=4.2.0 is installedr:   c                 S   s   g | ]}|d  |d dqS )r   r   )r.   r/   r2   r5   r2   r2   r3   r9   o   s     zKRedisClusterStorage.__use_legacy_cluster_implementation.<locals>.<listcomp>)r;   r<   warningswarnr=   r#   )r)   r,   r   Zredis_clusterr2   r2   r3   Z#__use_legacy_cluster_implementationa   s    	z7RedisClusterStorage.__use_legacy_cluster_implementation)r   c                    s    d}jrXd}j D ]4}j|  |}|t fdd|D 7 }q|S j|}tttfdd|D S d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r6   k)noder2   r3   r9      s     z-RedisClusterStorage.reset.<locals>.<listcomp>c                    s   g | ]} j |d qS rB   )r#   rC   rD   rE   )r)   r2   r3   r9      s     N)	Zprefixed_keyr$   r#   Zget_primariesZget_redis_connectionkeyssumr   r"   )r)   prefixcountZprimaryrH   r2   )rG   r)   r3   resets   s    

 zRedisClusterStorage.reset)__name__
__module____qualname____doc__ZSTORAGE_SCHEMEr   r   strr   floatbool__annotations__r   ZDEPENDENCIESr(   r   r
   r"   r%   r>   r	   rL   __classcell__r2   r2   r0   r3   r      s$   
  !  r   )r   r?   typingr   Zdeprecated.sphinxr   Zpackaging.versionr   Zlimits.errorsr   Zlimits.storage.redisr   Zlimits.typingr   r   r	   r
   r   r   r2   r2   r2   r3   <module>   s   
