o
    KDi)                     @  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   N/var/www/Datamplify/venv/lib/python3.10/site-packages/limits/aio/strategies.py__init__   s   
zRateLimiter.__init__r   costitemr	   identifiersstrr   intreturnboolc                     t )B  
        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

        :return: True if ``cost`` could be deducted from the rate limit without exceeding it
        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

        :return: True if the rate limit is not depleted
        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_stats6   r(   zRateLimiter.get_window_statsNonec                   s   | j |j| I d H S r   )r   clearkey_forr+   r   r   r   r.   D   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   M   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'   W   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*   g   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   window_startwindow_itemsresetr   r   r   r,   {   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   H   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   rK   r/   rA   r   )r   r   r   	remainingrF   r   r   r   r,      s   
z'FixedWindowRateLimiter.get_window_statsNr1   r2   )r4   r5   r6   rG   r'   r*   r,   r   r   r   r   rI      s
    rI   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   rR   rS   rU   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 )aS  
        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

        :return: True if ``cost`` could be deducted from the rate limit without exceeding it
        N)r   r   r   rQ   r/   r@   rA   r&   r   r   r   r'      s   z#SlidingWindowCounterRateLimiter.hitc                  sL   t t| j|j| | I dH \}}}}| |||||j| d k S )aK  
        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

        :return: True if the rate limit is not depleted
        Nr   )r   r   r   rP   r/   rA   rV   r@   )r   r   r   r   rR   rS   rU   _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   rP   r/   rA   rL   r@   r   rV   timer   r   min)r   r   r   rR   rS   rU   current_expires_inrM   nowexpiryprevious_reset_in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   clear_sliding_windowr/   rA   r+   r   r   r   r.   A  s   z%SlidingWindowCounterRateLimiter.clearr0   )
r   r	   rR   r   rS   rT   rU   r   r    rT   r1   r2   r3   )r4   r5   r6   rG   r   rV   r'   r*   r,   r.   rH   r   r   r>   r   rO      s    


.rO   )zsliding-window-counterzfixed-windowzmoving-window)rG   
__future__r   rX   abcr   r   mathr   r   deprecated.sphinxr   limitsr	   r   r
   typingr   utilr   r   r   storage.baser   r   r7   rI   rO   
STRATEGIESr   r   r   r   <module>   s*    3F>{
