
    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	 d dl
mZ d dlmZ d dlmZ er	  ed	d
       G d de             Zy)    )annotationsN)TYPE_CHECKING)versionchanged)Version)ConfigurationError)RedisStorage)RedisClientz4.3zuAdded support for using the redis client from :pypi:`valkey` if :paramref:`uri` has the ``valkey+sentinel://`` schema)versionreasonc                       e Zd ZdZddgZ	  ed       ed       ed       ed      dZdddej                  d	f	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd
Z	e
	 	 dd       ZdddZ xZS )RedisSentinelStoragez
    Rate limit storage with redis sentinel as backend

    Depends on :pypi:`redis` package (or :pypi:`valkey` if :paramref:`uri` starts with
    ``valkey+sentinel://``)
    zredis+sentinelzvalkey+sentinelz3.0z6.0)rediszredis.sentinelvalkeyzvalkey.sentinelNTFc                $   t        t        | 
  |fd|i| t        j                  j                  |      }g }	|r|j                         ni }
i }|j                  r|j                  |d<   |j                  r|j                  |d<   |j                  j                  d      dz   }|j                  |d j                  d      D ]2  }|j                  d      \  }}|	j                  |t        |      f       4 || _        |j                  r|j                  j!                  d	d
      n|| _        | j"                  t%        d      |j'                  d      rdnd| _        | j*                  | j(                   d   j,                  } |j.                  |	fdi ||
ii ||| _        | j0                  j3                  | j"                        | _        | j0                  j7                  | j"                        | _        || _        | j=                  |       y)a  
        :param uri: url of the form
         ``redis+sentinel://host:port,host:port/service_name``

         If the uri scheme is ``valkey+sentinel`` the implementation used will be from
         :pypi:`valkey`.
        :param service_name: sentinel service name
         (if not provided in :attr:`uri`)
        :param use_replicas: Whether to use replicas for read only operations
        :param sentinel_kwargs: kwargs to pass as
         :attr:`sentinel_kwargs` to :class:`redis.sentinel.Sentinel`
        :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.sentinel.Sentinel`
        :raise ConfigurationError: when the redis library is not available
         or if the redis master host cannot be pinged.
        wrap_exceptionsusernamepassword@   N,:/ z'service_name' not providedr   r   z	.sentinelsentinel_kwargs)superr   __init__urllibparseurlparsecopyr   r   netlocfindsplitappendint
key_prefixpathreplaceservice_namer   
startswithtarget_serverdependenciesmoduleSentinelsentinel
master_forstorage	slave_forstorage_slaveuse_replicasinitialize_storage)selfurir)   r4   r   r&   r   optionsparsedsentinel_configurationsentinel_optionsparsed_authseplochostportsentinel_dep	__class__s                    P/var/www/html/venv/lib/python3.12/site-packages/limits/storage/redis_sentinel.pyr   zRedisSentinelStorage.__init__*   s   < 	lD*	
!0	
4;	
 &&s+!#5D?//1"57??&,ooK
#??&,ooK
#mm  %)==&,,S1 	=C3JD$"))4T*;<	= %,2KKFKKR(\ 	 $$%BCC),)AXw((D,>,>+?y)IJQQ---"
?{?.>?
 )((

 %)MM$<$<T=N=N$O*.--*A*A$BSBS*T($    c                    | j                   dk(  r#| j                  d   j                  j                  S | j                  d   j                  j                  S )Nr   r   )r+   r,   r-   
RedisErrorValkeyError)r6   s    rC   base_exceptionsz$RedisSentinelStorage.base_exceptionsp   sT     !!W, g&--88	
 ""8,33??	
rD   c                N    |r| j                   r| j                  S | j                  S )N)r4   r3   r1   )r6   readonlys     rC   get_connectionz#RedisSentinelStorage.get_connectionz   s!    &.43D3Dt!!W4<<WrD   )r7   strr)   z
str | Noner4   boolr   z$dict[str, float | str | bool] | Noner&   rL   r   rM   r8   zfloat | str | boolreturnNone)rN   z-type[Exception] | tuple[type[Exception], ...])F)rJ   rM   rN   r	   )__name__
__module____qualname____doc__STORAGE_SCHEMEr   DEPENDENCIESr   PREFIXr   propertyrH   rK   __classcell__)rB   s   @rC   r   r      s     '(9:NQ !%.%."5>	L $(!@D&-- %D%D% !D% 	D%
 >D% D% D% &D% 
D%L 
	6
 
X XrD   r   )
__future__r   urllib.parser   typingr   deprecated.sphinxr   packaging.versionr   limits.errorsr   limits.storage.redisr   limits.typingr	   r    rD   rC   <module>rb      sR    "    , % , - % 	DcX< cXcXrD   