o
    ;h'                     @  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 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eedZdS )z'
Asynchronous rate limiting strategies
    )annotationsN)ABCabstractmethod)floorinf)versionadded   )RateLimitItem)StorageTypes)cast)WindowStats   )MovingWindowSupportStorage)SlidingWindowCounterSupportc                   @  sV   e Zd ZdddZedddddZedddddZedddZdddZdS )RateLimiterstorager
   c                 C  s   t |tsJ || _d S N)
isinstancer   r   selfr    r   H/var/www/html/venv/lib/python3.10/site-packages/limits/aio/strategies.py__init__   s   
zRateLimiter.__init__r   costitemr	   identifiersstrr   intreturnboolc                     t )
        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
        NotImplementedErrorr   r   r   r   r   r   r   hit      
zRateLimiter.hitc                  r"   )  
        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
        r$   r&   r   r   r   test&   r(   zRateLimiter.testr   c                   r"   )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$   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   r.   @   s   zRateLimiter.clearNr   r
   r   r	   r   r   r   r   r    r!   r   r	   r   r   r    r   r   r	   r   r   r    r-   )	__name__
__module____qualname__r   r   r'   r*   r,   r.   r   r   r   r   r      s    
r   c                      sL   e Zd ZdZd fddZdd	dddZdd	dddZdddZ  ZS )MovingWindowRateLimiterz4
    Reference: :ref:`strategies:moving window`
    r   r
   r    r-   c                   s4   t |dst |dstd|j t | d S )Nacquire_entryget_moving_windowz@MovingWindowRateLimiting is not implemented for storage of type hasattrr%   	__class__superr   r   r<   r   r   r   I   s   z MovingWindowRateLimiter.__init__r   r   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   r8   r/   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   r9   r/   r@   rA   )r   r   r   r   resr@   r   r   r   r*   a   s   	
zMovingWindowRateLimiter.testr   c                   sJ   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   r9   r/   r@   rA   r   )r   r   r   Zwindow_startZwindow_itemsresetr   r   r   r,   s   s   z(MovingWindowRateLimiter.get_window_stats)r   r
   r    r-   r1   r2   )	r4   r5   r6   __doc__r   r'   r*   r,   __classcell__r   r   r>   r   r7   D   s    
r7   c                   @  s:   e Zd ZdZdddddZdddddZdddZdS )FixedWindowRateLimiterz3
    Reference: :ref:`strategies:fixed window`
    r   r   r   r	   r   r   r   r   r    r!   c                  s*   | j j|j| | |dI dH |jkS r?   )r   incrr/   rA   r@   r&   r   r   r   r'      s   
zFixedWindowRateLimiter.hitc                  s(   | j |j| I dH |j| d k S rB   )r   getr/   r@   r&   r   r   r   r*      s   $zFixedWindowRateLimiter.testr   c                   sH   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   rI   r/   rA   r   )r   r   r   	remainingrD   r   r   r   r,      s   
z'FixedWindowRateLimiter.get_window_statsNr1   r2   )r4   r5   r6   rE   r'   r*   r,   r   r   r   r   rG      s
    rG   z4.1)versionc                      s`   e Zd ZdZd  fddZd!ddZddd"ddZddd"ddZd#ddZd$ddZ	  Z
S )%SlidingWindowCounterRateLimiterz=
    Reference: :ref:`strategies:sliding window counter`
    r   r
   c                   s4   t |dr
t |dstd|j t | d S )Nget_sliding_windowacquire_sliding_window_entryzHSlidingWindowCounterRateLimiting is not implemented for storage of type r:   r   r>   r   r   r      s   z(SlidingWindowCounterRateLimiter.__init__r   r	   previous_countr   previous_expires_infloatcurrent_countr    c                 C  s   || |   | S )zu
        Return the approximated by weighting the previous window count and adding the current window count.
        )rA   )r   r   rP   rQ   rS   r   r   r   _weighted_count   s   
z/SlidingWindowCounterRateLimiter._weighted_countr   r   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   rO   r/   r@   rA   r&   r   r   r   r'      s   	z#SlidingWindowCounterRateLimiter.hitc                  sL   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   rN   r/   rA   rT   r@   )r   r   r   r   rP   rQ   rS   _r   r   r   r*      s   
z$SlidingWindowCounterRateLimiter.testr   c              
     s   t t| j|j| | I dH \}}}}td|jt| 	|||| }t

 }|s4|s4t||S | }	tt}
}|rE||	|  }
|rK||	 }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   rN   r/   rA   rJ   r@   r   rT   timer   r   min)r   r   r   rP   rQ   rS   Zcurrent_expires_inrK   nowZexpiryZprevious_reset_inZcurrent_reset_inr   r   r   r,     s:   


z0SlidingWindowCounterRateLimiter.get_window_statsr-   c                   s&   t t| j|j| | I d H S r   )r   r   r   Zclear_sliding_windowr/   rA   r+   r   r   r   r.   1  s   z%SlidingWindowCounterRateLimiter.clearr0   )
r   r	   rP   r   rQ   rR   rS   r   r    rR   r1   r2   r3   )r4   r5   r6   rE   r   rT   r'   r*   r,   r.   rF   r   r   r>   r   rM      s    


.rM   )zsliding-window-counterzfixed-windowzmoving-window)rE   
__future__r   rV   abcr   r   mathr   r   Zdeprecated.sphinxr   Zlimitsr	   r   r
   typingr   utilr   r   r   Zstorage.baser   r   r7   rG   rM   Z
STRATEGIESr   r   r   r   <module>   s*    /B:w
