U
    ]àôg#  ã                   @   sl   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mZmZ erXd dlZG dd„ de	ƒZdS )	é    N)ÚTYPE_CHECKING)ÚVersion)ÚConfigurationError)ÚRedisStorage)ÚDictÚOptionalÚUnionc                       s¤   e Zd ZdZdgZdedƒiZdeee e	ee
eeeee	f f  e	eeee	f ddœ‡ fd	d
„Zeedœ‡ fdd„Zeedœ‡ fdd„Ze	dœ‡ fdd„Z‡  ZS )ÚRedisSentinelStorageza
    Rate limit storage with redis sentinel as backend

    Depends on :pypi:`redis` package
    zredis+sentinelúredis.sentinelz3.0NTF)ÚuriÚservice_nameÚuse_replicasÚsentinel_kwargsÚwrap_exceptionsÚoptionsÚreturnc                    s6  t t| ƒj|fd|i|—Ž tj |¡}g }|r8| ¡ ni }	i }
|jrP|j|
d< |jr`|j|
d< |j	 
d¡d }|j	|d…  d¡D ]$}| d¡\}}| |t|ƒf¡ q„|jr¾|j d	d
¡n|| _| jdkrÖtdƒ‚| jd j}|j|fd|
|	–i|
|–—Ž| _| j | j¡| _| j | j¡| _|| _|  |¡ dS )a   
        :param uri: url of the form
         ``redis+sentinel://host:port,host:port/service_name``
        :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 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.
        r   ÚusernameÚpasswordú@é   Nú,ú:ú/Ú z'service_name' not providedr
   r   )Úsuperr   Ú__init__ÚurllibÚparseÚurlparseÚcopyr   r   ÚnetlocÚfindÚsplitÚappendÚintÚpathÚreplacer   r   ZdependenciesÚmoduleZSentinelÚsentinelZ
master_forÚstorageZ	slave_forÚstorage_slaver   Zinitialize_storage)Úselfr   r   r   r   r   r   ÚparsedZsentinel_configurationZsentinel_optionsZparsed_authÚsepÚlocÚhostÚportZsentinel_dep©Ú	__class__© úA/tmp/pip-unpacked-wheel-kizsipjx/limits/storage/redis_sentinel.pyr      sF    
ÿÿÿ

ÿ
ÿþýzRedisSentinelStorage.__init__)Úkeyr   c                    s   t ƒ  || jr| jn| j¡S )zB
        :param key: the key to get the counter value for
        )r   Ú_getr   r*   r)   ©r+   r5   r1   r3   r4   ÚgetY   s     ÿzRedisSentinelStorage.getc                    s   t ƒ  || jr| jn| j¡S )z;
        :param key: the key to get the expiry for
        )r   Z_get_expiryr   r*   r)   r7   r1   r3   r4   Ú
get_expiryb   s     ÿzRedisSentinelStorage.get_expiry)r   c                    s   t ƒ  | jr| jn| j¡S )zo
        Check if storage is healthy by calling :class:`aredis.StrictRedis.ping`
        on the slave.
        )r   Ú_checkr   r*   r)   )r+   r1   r3   r4   Úcheckk   s    zRedisSentinelStorage.check)NTNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZSTORAGE_SCHEMEr   ZDEPENDENCIESÚstrr   Úboolr   r   Úfloatr   r$   r8   r9   r;   Ú__classcell__r3   r3   r1   r4   r	      s&       úø?		r	   )Úurllib.parser   Útypingr   Zpackaging.versionr   Zlimits.errorsr   Zlimits.storage.redisr   Zlimits.typingr   r   r   Zredis.sentinelZredisr	   r3   r3   r3   r4   Ú<module>   s   