U
    ]g                     @   sd   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rPd dlZG dd deZdS )    N)TYPE_CHECKINGOptionalTupleTypeUnion)ConcurrentUpdateError)Storagec                   @   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:`etcd3`.
    Zetcdetcd3Zlimits   N)urimax_retriesoptionsreturnc                 K   s<   t j|}| jd j| _| jj|j|jf|| _	|| _
dS )a  
        :param uri: etcd location of the form
         ``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:`etcd3.Etcd3Client`
        :raise ConfigurationError: when :pypi:`etcd3` is not available
        r
   N)urllibparseurlparseZdependenciesmodulelibclienthostnameportstorager   )selfr   r   r   parsed r   7/tmp/pip-unpacked-wheel-kizsipjx/limits/storage/etcd.py__init__   s     zEtcdStorage.__init__.)r   c                 C   s   | j jS N)r   ZEtcd3Exceptionr   r   r   r   base_exceptions/   s    zEtcdStorage.base_exceptions)keyr   c                 C   s   | j  d|  S N/)PREFIXencoder   r!   r   r   r   prefixed_key5   s    zEtcdStorage.prefixed_keyF   )r!   expiryelastic_expiryamountr   c                 C   sl  d}|  |}|| jk r^t }| j|}|| }	| jj| jj|dkg| jjj|| d|	 	 |j
dg| jj|gd}
|
d r|S |
d d d \}}|d\}}	t|	}	|	|kr| j|j | j| nt|r| j|j || }	t|| }| jj| jj||kg| jjj|| d|	 	 |jdgg dd rT|S |d7 }qt||d S )Nr   0:)lease)comparesuccessZfailurer(      :)r'   r   timer   r.   ZtransactionZtransactionscreateputr%   idgetsplitfloatZrevoke_leaseZlease_iddeleteZrefresh_leaseintvaluer   )r   r!   r)   r*   r+   retriesZetcd_keynowr.   
window_endZcreate_attemptcurmetaZ	cur_valuenewr   r   r   incr8   sV    


zEtcdStorage.incrc                 C   sD   | j | |\}}|r@|d\}}t|t kr@t|S dS )Nr1   r   )r   r6   r'   r7   r8   r2   r:   )r   r!   r;   r@   r+   r)   r   r   r   r6   i   s    zEtcdStorage.getc                 C   s@   | j | |\}}|r4t|dd }t|S tt S )Nr1   r(   )r   r6   r'   r8   r7   r:   r2   )r   r!   r;   _r>   r   r   r   
get_expiryq   s
    zEtcdStorage.get_expiryc                 C   s$   z| j   W dS    Y dS X d S )NTF)r   statusr   r   r   r   checkx   s
    
zEtcdStorage.checkc                 C   s   | j | j djS r"   )r   Zdelete_prefixr$   Zdeletedr   r   r   r   reset   s    zEtcdStorage.resetc                 C   s   | j | | d S r   )r   r9   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   rD   rF   r   rG   rH   r   r   r   r   r	      s<         1r	   )r2   urllib.parser   typingr   r   r   r   r   Zlimits.errorsr   Zlimits.storage.baser   r
   r	   r   r   r   r   <module>   s   