
    5jr&              	          S SK Jr  S SKrS SKrS SKrS SKJrJr  S SKJ	r	  S SK
Jr  S SKrS SKJrJrJr  S SKJr   " S S	5      r\" S
S9 " S S\\\\5      5       rg)    )annotationsN)Counterdefaultdict)floor)versionadded)MovingWindowSupportSlidingWindowCounterSupportStorage)TimestampedSlidingWindowc                      \ rS rSrSS jrSrg)Entry   c                `    [         R                   " 5       U l        U R                  U-   U l        g N)timeatimeexpiry)selfr   s     X/root/lseg-credit/backend/venv/lib/python3.13/site-packages/limits/aio/storage/memory.py__init__Entry.__init__   s    YY[
jj6)    )r   r   N)r   intreturnNone)__name__
__module____qualname____firstlineno__r   __static_attributes__ r   r   r   r      s    *r   r   z2.1)versionc                    ^  \ rS rSrSrS/r  S       SU 4S jjjrSS jrSS jrSS jr	SS jr
\  SS	 j5       rSS S
 jjrSS!S jjrS"S jrS#S jr S         S$S jjrS%S jr        S&S jr S         S$S jjr      S'S jrS(S jr          S)S jrS*S jrS+S jrSS jrSrU =r$ ),MemoryStorage   z
rate limit storage using :class:`collections.Counter`
as an in memory storage for fixed & sliding window strategies,
and a simple list to implement moving window strategy.
zasync+memoryc                   > [        5       U l        [        [        R                  5      U l        0 U l        0 U l        S U l        [        TU ](  " U4SU0UD6  g )Nwrap_exceptions)r   storager   asyncioLocklocksexpirationseventstimersuperr   )r   urir'   _	__class__s       r   r   MemoryStorage.__init__*   sK     4;95@5N
-/.004
CoCCr   c                F    U R                   R                  5       nUS	 US	 U$ )Nr.   r+   )__dict__copyr   states     r   __getstate__MemoryStorage.__getstate__4   s'    ""$'N'Nr   c                    U R                   R                  U5        S U l        [        [        R
                  5      U l        [        R                  " U R                  5       5        g r   )	r5   updater.   r   r)   r*   r+   ensure_future_MemoryStorage__schedule_expiryr7   s     r   __setstate__MemoryStorage.__setstate__:   sB    U#
 .
d4467r   c                8  ^#     [         R                   " 5       m[        U R                  R                  5       5       H  nU R                  U    IS h  vN   [
        R                  " U4S jU R                  U   5      I S h  vN nU R                  R                  U/ 5      (       a  U R                  U   S U U R                  U'   U R                  R                  US 5      (       d8  U R                  R                  US 5        U R                  R                  US 5        S S S 5      IS h  vN   M     [        U R                  R                  5       5       H  nU R                  U   [         R                   " 5       ::  d  M+  U R                  R                  US 5        U R                  R                  US 5        U R                  R                  US 5        M     g  GN GN\ N! , IS h  vN  (       d  f       GM  = f! [
        R                   a     g f = f7f)Nc                4   > [         R                  " U T* S S9$ )Nc                    U R                   * $ r   )r   )events    r   <lambda>AMemoryStorage.__expire_events.<locals>.<lambda>.<locals>.<lambda>G   s
    5<<-r   key)bisectbisect_left)evtsnows    r   rE   /MemoryStorage.__expire_events.<locals>.<lambda>F   s    V%7%7 3$,G&r   )r   listr-   keysr+   r)   	to_threadgetpopr,   r(   CancelledError)r   rH   cutoffrL   s      @r   __expire_eventsMemoryStorage.__expire_events@   s    	))+CDKK,,./::c??#*#4#4 C(	$ F {{sB//+/;;s+;GV+DC(;;??355T2

sD1 +?? 0 D,,1134##C(DIIK7LL$$S$/$$((d3JJNN3-	 5 + +???$ %% 		s   HAH  GH  ,G$GBG$!H  ,G"-AH  AH  HH  G$"H  $G=	*G-+G=	7	H   HHHHc                   #    U R                   (       a  U R                   R                  5       (       a*  [        R                  " U R	                  5       5      U l         g g 7fr   )r.   doner)   create_task_MemoryStorage__expire_eventsr   s    r   __schedule_expiryMemoryStorage.__schedule_expiryY   s<     zzTZZ__.. ,,T-A-A-CDDJ /s   AAc                    [         $ r   )
ValueErrorr[   s    r   base_exceptionsMemoryStorage.base_exceptions]   s
     r   c                  #    U R                  U5      I Sh  vN   U R                  5       I Sh  vN   U R                  U    ISh  vN   U R                  U==   U-  ss'   U R                  U   U:X  a%  [        R                  " 5       U-   U R
                  U'   SSS5      ISh  vN   U R                  R                  X5      $  N N N N%! , ISh  vN  (       d  f       N:= f7f)z
increments the counter for a given rate limit key

:param key: the key to increment
:param expiry: amount in seconds for the key to expire in
:param amount: the number to increment by
N)rQ   r>   r+   r(   r   r,   )r   rH   r   amounts       r   incrMemoryStorage.incrc   s      hhsm$$&&&::c??LL'||C F*(,		f(<  % #? ||,, 	&"???sh   C(CC(CC(C
C(ACC(&C' C(C(
C(C(C%CC%!C(c                  #    U R                  U5      I Sh  vN   U R                  5       I Sh  vN   U R                  U    ISh  vN   [        U R                  U   U-
  S5      U R                  U'   SSS5      ISh  vN   U R                  R                  X5      $  N Nr N\ N%! , ISh  vN  (       d  f       N:= f7f)z
decrements the counter for a given rate limit key. 0 is the minimum allowed value.

:param amount: the number to increment by
Nr   )rQ   r>   r+   maxr(   )r   rH   rc   s      r   decrMemoryStorage.decrs   s      hhsm$$&&&::c?? #DLL$5$> BDLL #? ||,, 	&"???sg   CB CB"CB$C*B(5C B& C"C$C&C(B?.B1/B?;Cc                P  #    U R                   R                  US5      [        R                  " 5       ::  aT  U R                  R	                  US5        U R                   R	                  US5        U R
                  R	                  US5        U R                  R                  US5      $ 7f)z2
:param key: the key to get the counter value for
r   N)r,   rQ   r   r(   rR   r+   r   rH   s     r   rQ   MemoryStorage.get   s{      Q'499;6LLS$'  d+JJNN3%||Q''s   B$B&c                   #    U R                   R                  US5        U R                  R                  US5        U R                  R                  US5        U R                  R                  US5        g7f)z.
:param key: the key to clear rate limits for
N)r(   rR   r,   r-   r+   rk   s     r   clearMemoryStorage.clear   sU      	d#S$'T"

sD!s   A2A4c                "  #    XB:  a  gU R                  5       I Sh  vN   U R                  U    ISh  vN   U R                  R                  U/ 5        [        R                  " 5       n U R                  U   X$-
     nU(       a$  UR                  XS-
  :  a   SSS5      ISh  vN   g[        U5      /U-  U R                  U   SS&  SSS5      ISh  vN   g N N! [
         a    Sn Nnf = f NL N! , ISh  vN  (       d  f       g= f7f)z
:param key: rate limit key to acquire an entry in
:param limit: amount of entries allowed
:param expiry: expiry of the entry
:param amount: the number of entries to acquire
FNr   T)r>   r+   r-   
setdefaultr   
IndexErrorr   r   )r   rH   limitr   rc   	timestampentrys          r   acquire_entryMemoryStorage.acquire_entry   s      >$$&&&::c??KK""3+		I&*kk#&6u~&F 	(:: #?? ).f'?C !$ #?? 	'"
   #???s   DCDCD2C5+C?C5D$C1%D* C5
DC3DDC.+C5-C..C51D3D5D;C><DDc                h   #    U R                   R                  U[        R                  " 5       5      $ 7f)z+
:param key: the key to get the expiry for
)r,   rQ   r   rk   s     r   
get_expiryMemoryStorage.get_expiry   s&     
 ##C55s   02c                   #    [         R                   " 5       nU R                  R                  U/ 5      =n(       a-  [        R                  " XTU-
  * S S9nXVS-
     R
                  U4$ US4$ 7f)z
returns the starting point and the number of entries in the moving
window

:param key: rate limit key
:param expiry: expiry of entry
:return: (start of window, number of acquired entries)
c                    U R                   * $ r   )r   )ru   s    r   rE   1MemoryStorage.get_moving_window.<locals>.<lambda>   s
    %++r   rG      r   )r   r-   rQ   rI   rJ   r   )r   rH   rs   r   rt   r-   oldests          r   get_moving_windowMemoryStorage.get_moving_window   sn      IIK	[[__S"--6-''f,-3MF 1*%++V33!|s   A*A,c                  #    XB:  a  g[         R                   " 5       nU R                  XU5      u  pgU R                  XgX55      I S h  vN u  nn	n
nX-  U-  U
-   n[        U5      U-   U:  a  gU R	                  USU-  US9I S h  vN n
X-  U-  U
-   n[        U5      U:  a  U R                  Xt5      I S h  vN   gg Nv N: N
7f)NF   )rc   T)r   sliding_window_keys_get_sliding_window_infor   rd   rh   )r   rH   rs   r   rc   rL   previous_keycurrent_keyprevious_countprevious_ttlcurrent_countr1   weighted_counts                r   acquire_sliding_window_entry*MemoryStorage.acquire_sliding_window_entry   s      >iik$($<$<S#$N! //6WW	
'6?-O 6)E1 #'))KVF)"SSM+:VCmSN^$u, ii444 X T 5s6   ACB>=CC 1C7C8C CCc                   #    [         R                   " 5       nU R                  XU5      u  pEU R                  XEX#5      I S h  vN $  N7fr   )r   r   r   r   rH   r   rL   r   r   s         r   get_sliding_window MemoryStorage.get_sliding_window   sI      iik$($<$<S#$N!22v
 
 	
 
s   ?AAAc                   #    [         R                   " 5       nU R                  XU5      u  pEU R                  U5      I S h  vN   U R                  U5      I S h  vN   g  N N7fr   )r   r   rn   r   s         r   clear_sliding_window"MemoryStorage.clear_sliding_window   sQ     iik$($<$<S#$N!jj%%%jj&&& 	&&s#   >A# AA#A!A#!A#c                   #    U R                  U5      I S h  vN nU R                  U5      I S h  vN nUS:X  a  [        S5      nOSXC-
  U-  S-  -
  U-  nSXC-  S-  -
  U-  U-   nXWXh4$  NS N<7f)Nr   r~   )rQ   float)	r   r   r   r   rL   r   r   r   current_ttls	            r   r   &MemoryStorage._get_sliding_window_info   s       $xx55"hh{33Q 8L3<6"9Q!>?6ILS\Q./69FB]GG 63s   A/A+A/A-;A/-A/c                   #    g7f)z
check if storage is healthy
Tr!   r[   s    r   checkMemoryStorage.check
  s
     
 s   c                B  #    [        [        U R                  5      [        U R                  5      5      nU R                  R	                  5         U R
                  R	                  5         U R                  R	                  5         U R                  R	                  5         U$ 7fr   )rg   lenr(   r-   rn   r,   r+   )r   	num_itemss     r   resetMemoryStorage.reset  sk     DLL)3t{{+;<	 

s   BBc                     U R                   (       a;  U R                   R                  5       (       d  U R                   R                  5         g g g ! [         a     g f = fr   )r.   rX   cancelRuntimeErrorr[   s    r   __del__MemoryStorage.__del__  sG    	zz$**//"3"3

!!# #4z 		s   A
A 
AA)r-   r,   r+   r(   r.   )NF)r0   z
str | Noner'   boolr1   strr   r   )r   dict[str, limits.typing.Any])r8   r   r   r   )r   r   )r   z-type[Exception] | tuple[type[Exception], ...])r~   )rH   r   r   r   rc   r   r   r   )rH   r   rc   r   r   r   )rH   r   r   r   )rH   r   r   r   )
rH   r   rs   r   r   r   rc   r   r   r   )rH   r   r   r   )rH   r   rs   r   r   r   r   ztuple[float, int])rH   r   r   r   r   tuple[int, float, int, float])rH   r   r   r   r   r   )
r   r   r   r   r   r   rL   r   r   r   )r   r   )r   z
int | None)r   r   r   r   __doc__STORAGE_SCHEMEr   r9   r?   rZ   r>   propertyr`   rd   rh   rQ   rn   rv   ry   r   r   r   r   r   r   r   r   r    __classcell__)r2   s   @r   r$   r$      s    %%N ?DDD7;DJMD	D D82E 	6 
- -	(" @A",/9<	66",/	2   	
  
B

 #
	&
'HH H 	H
 H 
'H  r   r$   )
__future__r   r)   rI   r   collectionsr   r   mathr   deprecated.sphinxr   limits.typinglimitslimits.aio.storage.baser   r	   r
   limits.storage.baser   r   r$   r!   r   r   <module>r      sc    "    ,  *  
 9* * eD "=?WD Dr   