
    5j)                    
   S r SSKJr  SSKrSSKJrJr  SSKJrJ	r	  SSK
Jr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJrJr  SSKJr   " S S\5      r " S S\5      r " S S\5      r\" SS9 " S S\5      5       r\\\S.rg)z'
Asynchronous rate limiting strategies
    )annotationsN)ABCabstractmethod)floorinf)versionadded   )RateLimitItem)StorageTypes)cast)WindowStats   )MovingWindowSupportStorage)SlidingWindowCounterSupportc                      \ rS rSrSS jr\SS.SS jj5       r\SS.SS jj5       r\      SS j5       rSS jr	S	r
g
)RateLimiter   c                >    [        U[        5      (       d   eXl        g N)
isinstancer   storage)selfr   s     T/root/lseg-credit/backend/venv/lib/python3.13/site-packages/limits/aio/strategies.py__init__RateLimiter.__init__   s    '7++++ '    r   costc                  #    [         e7f)
  
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   hitRateLimiter.hit         "!   	c                  #    [         e7f)  
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   testRateLimiter.test(   r)   r*   c                   #    [         e7f)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_statsRateLimiter.get_window_stats6   r)   r*   c                n   #    U R                   R                  UR                  " U6 5      I S h  vN $  N7fr   )r   clearkey_forr0   s      r   r4   RateLimiter.clearD   s)     \\''k(BCCCCs   ,535)r   Nr   r   r%   r
   r&   strr   intreturnboolr%   r
   r&   r9   r;   r   r%   r
   r&   r9   r;   None)__name__
__module____qualname____firstlineno__r   r   r'   r-   r1   r4   __static_attributes__ r   r   r   r      sg    ( LM " " MN " " "!"14"	" "Dr   r   c                  j   ^  \ rS rSrSrS
U 4S jjrSS.SS jjrSS.SS jjr      SS jrS	r	U =r
$ )MovingWindowRateLimiterH   z,
Reference: :ref:`strategies:moving window`
c                   > [        US5      (       d)  [        US5      (       d  [        SUR                   35      e[        TU ]  U5        g )Nacquire_entryget_moving_windowz@MovingWindowRateLimiting is not implemented for storage of type hasattrr#   	__class__superr   r   r   rN   s     r   r    MovingWindowRateLimiter.__init__M   sO    G_--BU1V1V%",,-/  	!r   r   r   c                  #    [        [        U R                  5      R                  UR                  " U6 UR
                  UR                  5       US9I Sh  vN $  N7fr!   )amountN)r   r   r   rJ   r5   rT   
get_expiryr$   s       r   r'   MovingWindowRateLimiter.hitW   sS      -t||<JJLL+&T__5Ft K 
 
 	
 
s   AAAAc                  #    [        [        U R                  5      R                  UR                  " U6 UR
                  UR                  5       5      I Sh  vN nUS   nXQR
                  U-
  :*  $  N7fr,   Nr   )r   r   r   rK   r5   rT   rU   )r   r%   r   r&   resrT   s         r   r-   MovingWindowRateLimiter.testg   sg      ,dll;MMLL+&KKOO
 

 Qt+++
s   AA3A1A3c                  #    [        [        U R                  5      R                  UR                  " U6 UR
                  UR                  5       5      I Sh  vN u  p4X1R                  5       -   n[        XQR
                  U-
  5      $  N07f)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   rK   r5   rT   rU   r   )r   r%   r&   window_startwindow_itemsresets         r   r1   (MovingWindowRateLimiter.get_window_stats{   sq      ,0,


DLL+6T__EV
W&X" 005++"<==&Xs   AB	B1B	rE   )r   r   r;   r?   r8   r=   )r@   rA   rB   rC   __doc__r   r'   r-   r1   rD   __classcell__rN   s   @r   rG   rG   H   sG    " MN 
  NO ,(>!>14>	> >r   rG   c                  R    \ rS rSrSrSS.S
S jjrSS.S
S jjr      SS jrSrg	)FixedWindowRateLimiter   z+
Reference: :ref:`strategies:fixed window`
r   r   c                  #    U R                   R                  UR                  " U6 UR                  5       US9I Sh  vN UR                  :*  $  N7frS   )r   incrr5   rU   rT   r$   s       r   r'   FixedWindowRateLimiter.hit   sT      ,,##k*! $  
 {{	
s   :AAAc                  #    U R                   R                  UR                  " U6 5      I Sh  vN UR                  U-
  S-   :  $  N7frX   )r   getr5   rT   r$   s       r   r-   FixedWindowRateLimiter.test   sA      ,,""4<<#=>>tASVWAWW	
>s   ,AAAc           	       #    [        SUR                  U R                  R                  UR                  " U6 5      I Sh  vN -
  5      nU R                  R                  UR                  " U6 5      I Sh  vN n[        XC5      $  NE N7f)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)maxrT   r   rj   r5   rU   r   )r   r%   r&   	remainingr^   s        r   r1   'FixedWindowRateLimiter.get_window_stats   sq      KK 0 0{1K LLL
	 ll--dllK.HII5,,	 MIs"   =B	B 5B	5B6B	B	rE   Nr8   r=   )	r@   rA   rB   rC   r`   r'   r-   r1   rD   rE   r   r   rd   rd      s<     MN 
* NO 
 -!-14-	-r   rd   z4.1)versionc                     ^  \ rS rSrSrSU 4S jjr          SS jrSS.SS jjrSS.SS jjr      SS	 jr	SS
 jr
SrU =r$ )SlidingWindowCounterRateLimiter   z5
Reference: :ref:`strategies:sliding window counter`
c                   > [        US5      (       a  [        US5      (       d  [        SUR                   35      e[        TU ]  U5        g )Nget_sliding_windowacquire_sliding_window_entryzHSlidingWindowCounterRateLimiting is not implemented for storage of type rL   rP   s     r   r   (SlidingWindowCounterRateLimiter.__init__   sW    w 455W3>
 >
 &",,-/  	!r   c                2    X#-  UR                  5       -  U-   $ )ze
Return the approximated by weighting the previous window count and adding the current window count.
)rU   )r   r%   previous_countprevious_expires_incurrent_counts        r   _weighted_count/SlidingWindowCounterRateLimiter._weighted_count   s     3doo6GG-WWr   r   r   c                  #    [        [        U R                  5      R                  UR                  " U6 UR
                  UR                  5       U5      I Sh  vN $  N7f)a  
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   rv   r5   rT   rU   r$   s       r   r'   #SlidingWindowCounterRateLimiter.hit   sR      '

&
&LL+&KKOO	

 	
 
s   AAAAc                  #    [        [        U R                  5      R                  UR                  " U6 UR                  5       5      I Sh  vN u  pEpgU R                  XXV5      UR                  U-
  S-   :  $  N,7f)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

:return: True if the rate limit is not depleted
Nr   )r   r   r   ru   r5   rU   r|   rT   )r   r%   r   r&   ry   rz   r{   _s           r   r-   $SlidingWindowCounterRateLimiter.test   s      GK'G


T\\;79J
KAL=]
   &9 kkD 1$%	
	ALs   A	A:A8-A:c                
  #    [        [        U R                  5      R                  UR                  " U6 UR                  5       5      I Sh  vN u  nnnn[        SUR                  [        U R                  XXE5      5      -
  5      n[        R                  " 5       nU(       d  U(       d  [        X5      $ UR                  5       n	[        [        pU(       a  XIU-  -  n
U(       a  Xi-  n[        U[        X5      -   U5      $  N7f)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 this
 instance of the limit
:return: (reset time, remaining)
Nr   )r   r   r   ru   r5   rU   rm   rT   r   r|   timer   r   min)r   r%   r&   ry   rz   r{   current_expires_inrn   nowexpiryprevious_reset_incurrent_reset_ins               r   r1   0SlidingWindowCounterRateLimiter.get_window_stats  s     " 2DLLATTLL+&(9
 
	

 KK$$*=
	 iik-s..".13+ 37N O1:3%6!II9UU7
s   A	DDB6Dc                   #    [        [        U R                  5      R                  UR                  " U6 UR                  5       5      I S h  vN $  N7fr   )r   r   r   clear_sliding_windowr5   rU   r0   s      r   r4   %SlidingWindowCounterRateLimiter.clearA  sE     '


t||[94??;L
MN 	N Ns   A	AAArE   r7   )
r%   r
   ry   r:   rz   floatr{   r:   r;   r   r8   r=   r>   )r@   rA   rB   rC   r`   r   r|   r'   r-   r1   r4   rD   ra   rb   s   @r   rr   rr      s    "
X
X 
X #	
X
 
X 

X MN 
( NO 
.,V!,V14,V	,V\N Nr   rr   )zsliding-window-counterzfixed-windowzmoving-window)r`   
__future__r   r   abcr   r   mathr   r   deprecated.sphinxr   limitsr
   r   r   typingr   utilr   r   r   storage.baser   r   rG   rd   rr   
STRATEGIESrE   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   