a
    a*j*                     @  s
  d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	 ddl
mZ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mZ ddlmZ G dd deZG dd deZG dd deZeddG dd deZeddG dd deZeeeedZdS )z'
Asynchronous rate limiting strategies
    )annotationsN)ABCabstractmethod)floorinf)
deprecatedversionadded   )RateLimitItem)StorageTypes)cast)WindowStats   )MovingWindowSupportStorage)SlidingWindowCounterSupportc                   @  s~   e Zd ZddddZeddddd	d
dddZeddddd	d
dddZeddddddZddddddZdS )RateLimiterr   storagec                 C  s   t |tsJ || _d S N)
isinstancer   r   selfr    r   Q/var/www/html/assistant/venv/lib/python3.9/site-packages/limits/aio/strategies.py__init__   s    zRateLimiter.__init__r   costr
   strintboolitemidentifiersr   returnc                  s   t dS )
        Consume the rate limit

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :param cost: The cost of this hit, default 1
        NNotImplementedErrorr   r"   r   r#   r   r   r   hit   s    
zRateLimiter.hitc                  s   t dS )  
        Check if the rate limit can be consumed

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :param cost: The expected cost to be consumed, default 1
        Nr&   r(   r   r   r   test&   s    
zRateLimiter.testr   r"   r#   r$   c                   s   t dS )z
        Query the reset time and remaining amount for the limit

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :return: (reset time, remaining))
        Nr&   r   r"   r#   r   r   r   get_window_stats2   s    zRateLimiter.get_window_statsNonec                   s   | j |j| I d H S r   )r   clearkey_forr-   r   r   r   r0   @   s    zRateLimiter.clearN)	__name__
__module____qualname__r   r   r)   r+   r.   r0   r   r   r   r   r      s   r   c                      sn   e Zd ZdZddd fddZddd	d
dddddZddd	d
dddddZd	d
ddddZ  ZS )MovingWindowRateLimiterz4
    Reference: :ref:`strategies:moving window`
    r   r/   )r   r$   c                   s2   t |ds"t |ds"td|j t | d S )Nacquire_entryget_moving_windowzBMovingWindowRateLimiting is not implemented for storage of type %shasattrr'   	__class__superr   r   r:   r   r   r   I   s    z MovingWindowRateLimiter.__init__r   r   r
   r   r   r    r!   c                  s,   t t| jj|j| |j| |dI dH S )r%   )amountN)r   r   r   r6   r1   r=   
get_expiryr(   r   r   r   r)   S   s    
zMovingWindowRateLimiter.hitc                  s>   t t| j|j| |j| I dH }|d }||j| kS r*   Nr   )r   r   r   r7   r1   r=   r>   )r   r"   r   r#   resr=   r   r   r   r+   a   s    	
zMovingWindowRateLimiter.testr   r,   c                   sH   t t| j|j| |j| I dH \}}||  }t||j| S )z
        returns the number of requests remaining within this limit.

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :return: (reset time, remaining)
        N)r   r   r   r7   r1   r=   r>   r   )r   r"   r#   Zwindow_startZwindow_itemsresetr   r   r   r.   s   s    z(MovingWindowRateLimiter.get_window_stats)	r2   r3   r4   __doc__r   r)   r+   r.   __classcell__r   r   r<   r   r5   D   s
   
r5   c                   @  sV   e Zd ZdZdddddddd	d
ZdddddddddZddddddZdS )FixedWindowRateLimiterz3
    Reference: :ref:`strategies:fixed window`
    r   r   r
   r   r   r    r!   c                  s*   | j j|j| | d|dI dH |jkS )r%   FZelastic_expiryr=   Nr   incrr1   r>   r=   r(   r   r   r   r)      s    
zFixedWindowRateLimiter.hitc                  s&   | j |j| I dH |j| d k S r?   )r   getr1   r=   r(   r   r   r   r+      s    $zFixedWindowRateLimiter.testr   r,   c                   sF   t d|j| j|j| I dH  }| j|j| I dH }t||S )z
        Query the reset time and remaining amount for the limit

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :return: reset time, remaining
        r   N)maxr=   r   rH   r1   r>   r   )r   r"   r#   	remainingrA   r   r   r   r.      s    z'FixedWindowRateLimiter.get_window_statsN)r2   r3   r4   rB   r)   r+   r.   r   r   r   r   rD      s   rD   z4.1)versionc                      s   e Zd ZdZdd fddZdddddd	d
dZdddddddddZdddddddddZddddddZ  Z	S )SlidingWindowCounterRateLimiterz=
    Reference: :ref:`strategies:sliding window counter`
    r   r   c                   s2   t |drt |ds"td|j t | d S )Nget_sliding_windowacquire_sliding_window_entryzJSlidingWindowCounterRateLimiting is not implemented for storage of type %sr8   r   r<   r   r   r      s    z(SlidingWindowCounterRateLimiter.__init__r
   r   float)r"   previous_countprevious_expires_incurrent_countr$   c                 C  s   || |   | S )zu
        Return the approximated by weighting the previous window count and adding the current window count.
        )r>   )r   r"   rP   rQ   rR   r   r   r   _weighted_count   s    
z/SlidingWindowCounterRateLimiter._weighted_countr   r   r   r    r!   c                  s*   t t| j|j| |j| |I dH S )z
        Consume the rate limit

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The cost of this hit, default 1
        N)r   r   r   rN   r1   r=   r>   r(   r   r   r   r)      s    	z#SlidingWindowCounterRateLimiter.hitc                  sJ   t t| j|j| | I dH \}}}}| |||||j| d k S )a  
        Check if the rate limit can be consumed

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The expected cost to be consumed, default 1
        Nr   )r   r   r   rM   r1   r>   rS   r=   )r   r"   r   r#   rP   rQ   rR   _r   r   r   r+      s    
z$SlidingWindowCounterRateLimiter.testr   r,   c              
     s   t t| j|j| | I dH \}}}}td|jt| 	|||| }t

 }|sf|sft||S | }	tt }
}|r||	|  }
|r||	 }t|t|
| |S )a  
        Query the reset time and remaining amount for the limit.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: (reset time, remaining)
        Nr   )r   r   r   rM   r1   r>   rI   r=   r   rS   timer   r   min)r   r"   r#   rP   rQ   rR   Zcurrent_expires_inrJ   nowZexpiryZprevious_reset_inZcurrent_reset_inr   r   r   r.     s8    


z0SlidingWindowCounterRateLimiter.get_window_stats)
r2   r3   r4   rB   r   rS   r)   r+   r.   rC   r   r   r<   r   rL      s   
rL   c                   @  s*   e Zd ZdZdddddddd	d
ZdS )#FixedWindowElasticExpiryRateLimiterzG
    Reference: :ref:`strategies:fixed window with elastic expiry`
    r   r   r
   r   r   r    r!   c                  s.   | j j|j| | d|dI dH }||jkS )a   
        Consume the rate limit

        :param item: a :class:`limits.limits.RateLimitItem` instance
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :param cost: The cost of this hit, default 1
        TrE   NrF   )r   r"   r   r#   r=   r   r   r   r)   9  s    	z'FixedWindowElasticExpiryRateLimiter.hitN)r2   r3   r4   rB   r)   r   r   r   r   rX   3  s   rX   )zsliding-window-counterzfixed-windowzfixed-window-elastic-expiryzmoving-window)rB   
__future__r   rU   abcr   r   mathr   r   Zdeprecated.sphinxr   r   Zlimitsr
   r   r   typingr   utilr   r   r   Zstorage.baser   r   r5   rD   rL   rX   Z
STRATEGIESr   r   r   r   <module>   s.   /B;q