
    9i)                       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e      Z G d de      Z G d de      Z ed       G d de             ZeeedZy)z'
Asynchronous rate limiting strategies
    )annotationsN)ABCabstractmethod)floorinf)versionadded   )RateLimitItem)StorageTypes)cast)WindowStats   )MovingWindowSupportStorage)SlidingWindowCounterSupportc                  j    e Zd Zd	dZeddd
d       Zeddd
d       Ze	 	 	 	 	 	 dd       ZddZy)RateLimiterc                6    t        |t              sJ || _        y N)
isinstancer   storage)selfr   s     H/var/www/html/venv/lib/python3.12/site-packages/limits/aio/strategies.py__init__zRateLimiter.__init__   s    '7+++ '    r   costc                  K   t         w)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   itemr   identifierss       r   hitzRateLimiter.hit         "!   	c                  K   t         w):  
        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"   s       r   testzRateLimiter.test(   r&   r'   c                   K   t         w)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$   s      r   get_window_statszRateLimiter.get_window_stats6   r&   r'   c                n   K   | j                   j                   |j                  |        d {   S 7 wr   )r   clearkey_forr,   s      r   r/   zRateLimiter.clearD   s,     \\''k(BCCCCs   ,535Nr   r   r#   r
   r$   strr   intreturnboolr#   r
   r$   r3   r5   r   r#   r
   r$   r3   r5   None)	__name__
__module____qualname__r   r   r%   r*   r-   r/    r   r   r   r      sg    ( LM " " MN " " "!"14"	" "Dr   r   c                  T     e Zd ZdZd fdZddd	dZddd	dZ	 	 	 	 	 	 d
dZ xZS )MovingWindowRateLimiterz4
    Reference: :ref:`strategies:moving window`
    c                    t        |d      s$t        |d      st        d|j                         t        |   |       y )Nacquire_entryget_moving_windowz@MovingWindowRateLimiting is not implemented for storage of type hasattrr!   	__class__superr   r   r   rE   s     r   r   z MovingWindowRateLimiter.__init__M   sI    G_-BU1V%",,-/  	!r   r   r   c                  K   t        t        | j                        j                   |j                  | |j
                  |j                         |       d{   S 7 wr   )amountN)r   r   r   rA   r0   rJ   
get_expiryr"   s       r   r%   zMovingWindowRateLimiter.hitW   sV      -t||<JJDLL+&T__5Ft K 
 
 	
 
s   AAAAc                  K   t        t        | j                        j                   |j                  | |j
                  |j                                d{   }|d   }||j
                  |z
  k  S 7 wr)   Nr   )r   r   r   rB   r0   rJ   rK   )r   r#   r   r$   resrJ   s         r   r*   zMovingWindowRateLimiter.testg   sl      ,dll;MMDLL+&KKOO
 

 Qt+++
s   AA4A2A4c                  K   t        t        | j                        j                   |j                  | |j
                  |j                                d{   \  }}||j                         z   }t        ||j
                  |z
        S 7 3w)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   rB   r0   rJ   rK   r   )r   r#   r$   window_startwindow_itemsresets         r   r-   z(MovingWindowRateLimiter.get_window_stats{   sz      ,0,


LDLL+6T__EV
W&X"l t005$++"<==&Xs   ABB
4B)r   r   r5   r9   r2   r7   )	r:   r;   r<   __doc__r   r%   r*   r-   __classcell__rE   s   @r   r?   r?   H   sB    " MN 
  NO ,(>!>14>	>r   r?   c                  @    e Zd ZdZddddZddddZ	 	 	 	 	 	 d	dZy)
FixedWindowRateLimiterz3
    Reference: :ref:`strategies:fixed window`
    r   r   c                  K   | j                   j                   |j                  | |j                         |       d{   |j                  k  S 7 wrI   )r   incrr0   rK   rJ   r"   s       r   r%   zFixedWindowRateLimiter.hit   sW      ,,##k*! $  
 {{	
s   =AA Ac                  K   | j                   j                   |j                  |        d{   |j                  |z
  dz   k  S 7 wrM   )r   getr0   rJ   r"   s       r   r*   zFixedWindowRateLimiter.test   sD      ,,""<4<<#=>>tASVWAWW	
>s   ,AAAc           	       K   t        d|j                  | j                  j                   |j                  |        d{   z
        }| j                  j                   |j                  |        d{   }t        ||      S 7 F7 w)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)maxrJ   r   r[   r0   rK   r   )r   r#   r$   	remainingrR   s        r   r-   z'FixedWindowRateLimiter.get_window_stats   sy      KK 0 0{1K LLL
	 ll--ldllK.HII5),,	 MIs"   =B
B 5B
5B6B
B
Nr2   r7   )r:   r;   r<   rS   r%   r*   r-   r=   r   r   rW   rW      s<     MN 
* NO 
 -!-14-	-r   rW   z4.1)versionc                  x     e Zd ZdZd
 fdZ	 	 	 	 	 	 	 	 	 	 ddZddddZddddZ	 	 	 	 	 	 ddZdd	Z	 xZ
S )SlidingWindowCounterRateLimiterz=
    Reference: :ref:`strategies:sliding window counter`
    c                    t        |d      rt        |d      st        d|j                         t        |   |       y )Nget_sliding_windowacquire_sliding_window_entryzHSlidingWindowCounterRateLimiting is not implemented for storage of type rC   rG   s     r   r   z(SlidingWindowCounterRateLimiter.__init__   sO    w 45W3>
 &",,-/  	!r   c                4    ||z  |j                         z  |z   S )zu
        Return the approximated by weighting the previous window count and adding the current window count.
        )rK   )r   r#   previous_countprevious_expires_incurrent_counts        r   _weighted_countz/SlidingWindowCounterRateLimiter._weighted_count   s!      33doo6GG-WWr   r   r   c                  K   t        t        | j                        j                   |j                  | |j
                  |j                         |       d{   S 7 w)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   rd   r0   rJ   rK   r"   s       r   r%   z#SlidingWindowCounterRateLimiter.hit   sU      '

&
&DLL+&KKOO	

 	
 
s   AAAAc                  K   t        t        | j                        j                   |j                  | |j                                d{   \  }}}}| j                  ||||      |j                  |z
  dz   k  S 7 0w)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   rc   r0   rK   ri   rJ   )r   r#   r   r$   rf   rg   rh   _s           r   r*   z$SlidingWindowCounterRateLimiter.test   s      GK'G


\T\\;79J
KAL=+]A
   n&9= kkD 1$%	
	ALs   A	A>A<1A>c                  K   t        t        | j                        j                   |j                  | |j                                d{   \  }}}}t        d|j                  t        | j                  ||||            z
        }t        j                         }|s|st        ||      S |j                         }	t        t        }}
|r||	|z  z  }
|r||	z  }t        |t        |
|      z   |      S 7 w)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   rc   r0   rK   r]   rJ   r   ri   timer   r   min)r   r#   r$   rf   rg   rh   current_expires_inr^   nowexpiryprevious_reset_incurrent_reset_ins               r   r-   z0SlidingWindowCounterRateLimiter.get_window_stats  s    " 2DLLATTDLL+&(9
 
	

 KK$$.*=}
	 iik-sI..".13+ 3v7N O1F:3%68H!II9UU7
s   A	C5C3B(C5c                   K   t        t        | j                        j                   |j                  | |j                                d {   S 7 wr   )r   r   r   clear_sliding_windowr0   rK   r,   s      r   r/   z%SlidingWindowCounterRateLimiter.clearA  sH     '


|t||[94??;L
MN 	N Ns   A	AAAr1   )
r#   r
   rf   r4   rg   floatrh   r4   r5   rw   r2   r7   r8   )r:   r;   r<   rS   r   ri   r%   r*   r-   r/   rT   rU   s   @r   ra   ra      s    "
X
X 
X #	
X
 
X 

X MN 
( NO 
.,V!,V14,V	,V\Nr   ra   )zsliding-window-counterzfixed-windowzmoving-window)rS   
__future__r   rn   abcr   r   mathr   r   deprecated.sphinxr   limitsr
   r   r   typingr   utilr   r   r   storage.baser   r   r?   rW   ra   
STRATEGIESr=   r   r   <module>r      s    #  #  * " "   1 50D# 0DfC>k C>L;-[ ;-| ewNk wN wNv >*,
r   