
    9i)                    8   U 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mZmZ ddl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e   z  ee   z  ZeeedZded<   y)z
Rate limiting strategies
    )annotationsN)ABCMetaabstractmethod)floorinf)versionadded)SlidingWindowCounterSupport   )RateLimitItem)MovingWindowSupportStorageStorageTypes)cast)WindowStatsc                  ^    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     D/var/www/html/venv/lib/python3.12/site-packages/limits/strategies.py__init__zRateLimiter.__init__   s    '7+++ '    r
   costc                   t         S  
        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
        NotImplementedErrorr   itemr   identifierss       r   hitzRateLimiter.hit   
     "!r   c                   t         )aT  
        Check the rate limit without consuming from it.

        :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
        r    r"   s       r   testzRateLimiter.test(   r&   r   c                    t         )
  
        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)
        r    r   r#   r$   s      r   get_window_statszRateLimiter.get_window_stats6   s
     "!r   c                R    | j                   j                   |j                  |       S r   )r   clearkey_forr+   s      r   r.   zRateLimiter.clearB   s"    ||!!,$,,"<==r   Nr   r   r#   r   r$   strr   intreturnboolr#   r   r$   r2   r4   r   r#   r   r$   r2   r4   None)	__name__
__module____qualname__r   r   r%   r(   r,   r.    r   r   r   r      sN    ( FG " " GH " " 	" 	">r   r   )	metaclassc                  H     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__K   sI    G_-BU1V%",,-/  	!r   r
   r   c                   t        t        | j                        j                   |j                  | |j
                  |j                         |      S r   )amount)r   r   r   rA   r/   rJ   
get_expiryr"   s       r   r%   zMovingWindowRateLimiter.hitU   sH     '6DDDLL+&T__5Ft E 
 	
r   c                   t        t        | j                        j                   |j                  | |j
                  |j                               d   |j
                  |z
  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
        r
   )r   r   r   rB   r/   rJ   rK   r"   s       r   r(   zMovingWindowRateLimiter.teste   s_     $dll3EEk*! 	
 {{T!"	
r   c                    t        t        | j                        j                   |j                  | |j
                  |j                               \  }}||j                         z   }t        ||j
                  |z
        S )a  
        returns the number of requests remaining within this limit.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: tuple (reset time, remaining)
        )r   r   r   rB   r/   rJ   rK   r   )r   r#   r$   window_startwindow_itemsresets         r   r,   z(MovingWindowRateLimiter.get_window_statsz   sl     &*&


LDLL+6T__EV
W 	#l t005$++"<==r   r0   r1   r6   )	r9   r:   r;   __doc__r   r%   r(   r,   __classcell__rE   s   @r   r?   r?   F   s*    " GH 
  HI 
*>r   r?   c                  4    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                   | j                   j                   |j                  | |j                         |      |j                  k  S rI   )r   incrr/   rK   rJ   r"   s       r   r%   zFixedWindowRateLimiter.hit   sI     LLk*!  
 {{	
r   c               x    | j                   j                   |j                  |       |j                  |z
  dz   k  S rM   )r   getr/   rJ   r"   s       r   r(   zFixedWindowRateLimiter.test   s8     ||k :;dkkD>PST>TTTr   c           	         t        d|j                  | j                  j                   |j                  |       z
        }| j                  j                   |j                  |       }t        ||      S )r*   r   )maxrJ   r   rZ   r/   rK   r   )r   r#   r$   	remainingrQ   s        r   r,   z'FixedWindowRateLimiter.get_window_stats   s_     4;;)9)9,$,,:T)UUV	''k(BC5),,r   Nr1   r6   )r9   r:   r;   rR   r%   r(   r,   r<   r   r   rV   rV      s%     GH 
* HI U-r   rV   z4.1)versionc                  l     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                   t        t        | j                        j                   |j                  | |j
                  |j                         |      S r   )r   r	   r   rc   r/   rJ   rK   r"   s       r   r%   z#SlidingWindowCounterRateLimiter.hit   sG     '

&
&DLL+&KKOO	
	
r   c                   t        t        | j                        j                   |j                  | |j                               \  }}}}| j                  ||||      |j                  |z
  dz   k  S rM   )r   r	   r   rb   r/   rK   rh   rJ   )r   r#   r   r$   re   rf   rg   _s           r   r(   z$SlidingWindowCounterRateLimiter.test   s|     AE'A


\T\\;79J
K 	>+]A
   n&9= kkD 1$%	
r   c                   t        t        | j                        j                   |j                  | |j                               \  }}}}t        d|j                  t        | j                  ||||            z
        }t        j                         }|s|st        ||      S |j                         }	t        t        }}
|r||	|z  z  }
|r||	z  }t        |t        |
|      z   |      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: WindowStats(reset time, remaining)
        r   )r   r	   r   rb   r/   rK   r\   rJ   r   rh   timer   r   min)r   r#   r$   re   rf   rg   current_expires_inr]   nowexpiryprevious_reset_incurrent_reset_ins               r   r,   z0SlidingWindowCounterRateLimiter.get_window_stats  s     RV'R


\T\\;79J
K 	O+]<N KK$$.*=}
	 iik-sI..".13+ 3v7N O1F:3%68H!II9UUr   c                    t        t        | j                        j                   |j                  | |j                               S r   )r   r	   r   clear_sliding_windowr/   rK   r+   s      r   r.   z%SlidingWindowCounterRateLimiter.clear.  s8    />SSDLL+&(9
 	
r   r0   )
r#   r   re   r3   rf   floatrg   r3   r4   rv   r1   r6   r7   )r9   r:   r;   rR   r   rh   r%   r(   r,   r.   rS   rT   s   @r   r`   r`      sl    "
X
X 
X #	
X
 
X 

X GH 
( HI 
,$VL
r   r`   )zsliding-window-counterzfixed-windowzmoving-windowzdict[str, KnownStrategy]
STRATEGIES) rR   
__future__r   rm   abcr   r   mathr   r   deprecated.sphinxr   limits.storage.baser	   limitsr   r   r   r   r   typingr   utilr   r   r?   rV   r`   typeKnownStrategyrw   __annotations__r<   r   r   <module>r      s    #  '  * ; ! ? ?  .>G .>bB>k B>J4-[ 4-n en
k n
 n
d 		()
!"#
"#$  >*,(
$ r   