U
    ]g                     @   sl   d dl Z d dlZd dlZd dlmZmZmZmZm	Z	 d dl
mZ d dlmZ erXd dlZG dd deZdS )    N)TYPE_CHECKINGOptionalTupleTypeUnion)Storage)ConcurrentUpdateErrorc                   @   s   e Zd ZdZdgZdgZdZdZefee	edddd	Z
eeee eee d
f f dddZeedddZd ee	ee	e	dddZee	dddZee	dddZedddZee	 dddZeddddZdS )!EtcdStoragezQ
    Rate limit storage with etcd as backend.

    Depends on :pypi:`aetcd`.
    z
async+etcdaetcdZlimits   N)urimax_retriesoptionsreturnc                 K   sB   t j|}| jd j| _| jjf |j|jd|| _	|| _
dS )a  
        :param uri: etcd location of the form
         ``async+etcd://host:port``,
        :param max_retries: Maximum number of attempts to retry
         in the case of concurrent updates to a rate limit key
        :param options: all remaining keyword arguments are passed
         directly to the constructor of :class:`aetcd.client.Client`
        :raise ConfigurationError: when :pypi:`aetcd` is not available
        r
   )hostportN)urllibparseurlparseZdependenciesmodulelibZClienthostnamer   storager   )selfr   r   r   parsed r   ;/tmp/pip-unpacked-wheel-kizsipjx/limits/aio/storage/etcd.py__init__   s     zEtcdStorage.__init__.)r   c                 C   s   | j jS N)r   ZClientErrorr   r   r   r   base_exceptions1   s    zEtcdStorage.base_exceptions)keyr   c                 C   s   | j  d|  S N/)PREFIXencoder   r!   r   r   r   prefixed_key7   s    zEtcdStorage.prefixed_keyF   )r!   expiryelastic_expiryamountr   c                    s  d}|  |}|| jk rt }| j|I d H }|| }	| jj| jj|dkg| jjj|| d|	 	 |j
dg| jj|gdI d H }
|
d r|S |
d d d d }|jd\}}	t|	}	|	|krt| j|j| j|I d H  n|r| j|jI d H  || }	t|| }| jj| jj||jkg| jjj|| d|	 	 |jdgg dI d H d r||S |d7 }qt||d S )Nr      0:)lease)comparesuccessZfailurer(      :)r'   r   timer   r.   ZtransactionZtransactionscreateputr%   idgetvaluesplitfloatasyncioZgatherZrevoke_leasedeleteZrefresh_leaseintr   )r   r!   r)   r*   r+   retriesZetcd_keynowr.   
window_endZcreate_attemptcurZ	cur_valuenewr   r   r   incr:   s\    
  	


zEtcdStorage.incrc                    sH   | j | |I d H }|rD|jd\}}t|t krDt|S dS )Nr1   r   )r   r6   r'   r7   r8   r9   r2   r<   )r   r!   r@   r+   r)   r   r   r   r6   o   s    zEtcdStorage.getc                    sD   | j | |I d H }|r8t|jdd }t|S tt S )Nr1   r(   )r   r6   r'   r9   r7   r8   r<   r2   )r   r!   r@   r?   r   r   r   
get_expiryw   s
    zEtcdStorage.get_expiryc                    s*   z| j  I d H  W dS    Y dS X d S )NTF)r   statusr   r   r   r   check~   s
    zEtcdStorage.checkc                    s    | j | j d I d H jS r"   )r   Zdelete_prefixr$   r%   Zdeletedr   r   r   r   reset   s    zEtcdStorage.resetc                    s   | j | |I d H  d S r   )r   r;   r'   r&   r   r   r   clear   s    zEtcdStorage.clear)Fr(   )__name__
__module____qualname____doc__ZSTORAGE_SCHEMEZDEPENDENCIESr$   ZMAX_RETRIESstrr<   r   propertyr   r   	Exceptionr   r    bytesr'   boolrB   r6   rC   rE   r   rF   rG   r   r   r   r   r	      s<         5r	   )r:   r2   urllib.parser   typingr   r   r   r   r   Zlimits.aio.storage.baser   Zlimits.errorsr   r
   r	   r   r   r   r   <module>   s   