
    9i                        d dl mZ d dlZd dlmZ d dlmZ d dlmZ  edd       ed	d
       edd       G d de                           Z	y)    )annotationsN)versionchanged)Version)RedisStoragez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`.
z4.3ztAdded support for using the redis client from :pypi:`valkey` if :paramref:`uri` has the ``valkey+cluster://`` schemac                       e Zd ZU dZddgZ	 ddiZded<   	  ed       ed	      d
Ze	j                  df	 	 	 	 	 	 	 	 	 d fdZddZ xZS )RedisClusterStoragez
    Rate limit storage with redis cluster as backend

    Depends on :pypi:`redis` (or :pypi:`valkey` if :paramref:`uri`
    starts with ``valkey+cluster://``).
    zredis+clusterzvalkey+clustermax_connectionsi  zdict[str, float | str | bool]DEFAULT_OPTIONSz4.2.0z6.0)redisvalkeyFc                   t         j                  j                  |      }i }|j                  r|j                  |d<   |j                  r|j                  |d<   |j
                  j                  d      dz   }g }|j
                  |d j                  d      D ]2  }	|	j                  d      \  }
}|j                  |
t        |      f       4 || _
        d| _        |j                  d      rdnd	| _        i | j                  ||}| j                  | j                     j                   | _        |D cg c]%  } | j"                  j$                  j&                  | ' }}| j                  d	k(  r. | j"                  j$                  j(                  dd
|i|| _        n- | j"                  j$                  j*                  dd
|i|| _        | j                  sJ | j-                  |       t/        t0        | f  ||fi | yc c}w )a  
        :param uri: url of the form
         ``redis+cluster://[:password]@host:port,host:port``

         If the uri scheme is ``valkey+cluster`` the implementation used will be from
         :pypi:`valkey`.
        :param key_prefix: the prefix for each key created in redis
        :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   r   startup_nodes )urllibparseurlparser   r   netlocfindsplitappendint
key_prefixstorage
startswithtarget_serverr   dependenciesmodule
dependencyclusterClusterNodeRedisClusterValkeyClusterinitialize_storagesuperr   __init__)selfurir    wrap_exceptionsoptionsparsedparsed_authsepcluster_hostslochostportmerged_optionscr   	__class__s                  O/var/www/html/venv/lib/python3.12/site-packages/limits/storage/redis_cluster.pyr-   zRedisClusterStorage.__init__8   s   * &&s+57??&,ooK
#??&,ooK
#mm  %)==&,,S1 	4C3JD$  $D	!23	4 %),)AXwKD00KKK7K++D,>,>?FFJWXQ<00<<a@XX(?4??22?? +/=DL A4??22@@ +/=DL |||$lD*3K7K Ys   2*G=c                @   | j                  d      }d}| j                  j                         D ]h  }| j                  j                  |      }|j	                  |      }|t        |D cg c]"  }|j                  |j                  d            $ c}      z  }j |S c c}w )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 :paramref:`RedisClusterStorage.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   zutf-8)prefixed_keyr!   get_primariesget_redis_connectionkeyssumdeletedecode)r.   prefixcountprimarynoderB   ks          r<   resetzRedisClusterStorage.resetn   s     ""3'||113 	IG<<44W=D99V$DS$GQ$++ahhw&78GHHE	I  Hs   ''B)
r/   strr    rL   r0   boolr1   zfloat | str | boolreturnNone)rN   z
int | None)__name__
__module____qualname____doc__STORAGE_SCHEMEr   __annotations__r   DEPENDENCIESr   PREFIXr-   rK   __classcell__)r;   s   @r<   r
   r
      s    2 &'78N. 	46O2  I !%.L '-- %	4L4L 4L 	4L
 &4L 
4Ll    r
   )

__future__r   r   deprecated.sphinxr   packaging.versionr   limits.storage.redisr   r
   r   rY   r<   <module>r^      sn    "  , % -  	 	C], ]	0]rY   