
    5j"                        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
rS SKJrJrJrJr   " S S5      r " S S	\\\\5      rg)
    )annotationsN)Counterdefaultdict)floor)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     T/root/lseg-credit/backend/venv/lib/python3.13/site-packages/limits/storage/memory.py__init__Entry.__init__   s    YY[
jj6)    )r   r   N)r   floatreturnNone)__name__
__module____qualname____firstlineno__r   __static_attributes__ r   r   r   r      s    *r   r   c                  @  ^  \ 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rU =r$ )+MemoryStorage   z
rate limit storage using :class:`collections.Counter`
as an in memory storage for fixed and sliding window strategies,
and a simple list to implement moving window strategy.

memoryc                "  > [        5       U l        [        [        R                  5      U l        0 U l        0 U l        [        R                  " SU R                  5      U l
        U R                  R                  5         [        TU ]4  " U4SU0UD6  g )N{Gz?wrap_exceptions)r   storager   	threadingRLocklocksexpirationseventsTimer_MemoryStorage__expire_eventstimerstartsuperr   )r   urir'   _	__class__s       r   r   MemoryStorage.__init__$   sj    3:98CIOO8T
-/.0&/oodD<P<P&Q


CoCCr   c                F    U R                   R                  5       nUS	 US	 U$ )Nr0   r+   )__dict__copyr   states     r   __getstate__MemoryStorage.__getstate__-   s'    ""$'N'Nr   c                    U R                   R                  U5        [        [        R                  5      U l        [        R                  " SU R                  5      U l        U R                  R                  5         g Nr&   )
r8   updater   r)   r*   r+   r.   r/   r0   r1   r:   s     r   __setstate__MemoryStorage.__setstate__3   sL    U# 1
__T4+?+?@


r   c                N   [        U R                  R                  5       5       H  nU R                  U      U R                  R	                  U/ 5      =n(       aJ  [
        R                  " U[        R                  " 5       * S S9nU R                  U   S U U R                  U'   U R                  R	                  US 5      (       d  U R                  R                  US 5        S S S 5        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 ! , (       d  f       GM  = f)Nc                    U R                   * $ r   )r   )events    r   <lambda>/MemoryStorage.__expire_events.<locals>.<lambda>>   s
    r   key)listr-   keysr+   getbisectbisect_leftr   popr,   r(   )r   rI   r-   oldests       r   __expire_eventsMemoryStorage.__expire_events9   s.   ((*+CC![[__S"5565#//2MF (,{{3'7'@DKK${{sD11JJNN3- ! , ((--/0C$		3  d+  $$S$/

sD)	 1 !s   B+F
F$	c                    U R                   R                  5       (       dA  [        R                  " SU R                  5      U l         U R                   R                  5         g g r?   )r0   is_aliver)   r.   r/   r1   r   s    r   __schedule_expiryMemoryStorage.__schedule_expiryI   sB    zz""$$"t/C/CDDJJJ %r   c                    [         $ r   )
ValueErrorrU   s    r   base_exceptionsMemoryStorage.base_exceptionsN   s
     r   c                l   U R                  U5        U R                  5         U R                  U      U R                  U==   U-  ss'   U R                  U   U:X  a%  [        R                  " 5       U-   U R
                  U'   SSS5        U R                  R                  US5      $ ! , (       d  f       N*= 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
Nr   )rL   _MemoryStorage__schedule_expiryr+   r(   r   r,   )r   rI   r   amounts       r   incrMemoryStorage.incrT   s     	 ZZ_LL'||C F*(,		f(<  %  ||Q''	 _s   AB%%
B3c                    U R                  U5        U R                  5         U R                  U      [        U R                  U   U-
  S5      U R                  U'   SSS5        U R                  R                  US5      $ ! , (       d  f       N*= f)z
decrements the counter for a given rate limit key

:param key: the key to decrement
:param amount: the number to decrement by
r   N)rL   r]   r+   maxr(   )r   rI   r^   s      r   decrMemoryStorage.decrd   so     	 ZZ_ #DLL$5$> BDLL  ||Q'' _s   *A??
Bc                H   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      $ )z2
:param key: the key to get the counter value for
r   N)r,   rL   r   r(   rO   r+   r   rI   s     r   rL   MemoryStorage.getr   sx    
 Q'499;6LLS$'  d+JJNN3%||Q''r   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)z.
:param key: the key to clear rate limits for
N)r(   rO   r,   r-   r+   rf   s     r   clearMemoryStorage.clear~   sR     	d#S$'T"

sD!r   c                   XB:  a  gU R                  5         U R                  U      U R                  R                  U/ 5        [        R                  " 5       n U R                  U   X$-
     nU(       a  UR                  XS-
  :  a
   SSS5        g[        U5      /U-  U R                  U   SS&  SSS5        g! [
         a    Sn NZf = f! , (       d  f       g= 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   rI   limitr   r^   	timestampentrys          r   acquire_entryMemoryStorage.acquire_entry   s     > ZZ_KK""3+		IC(8 	(:: _ ).f'?C !$ _
   _s5   2CB9-C C9CCCC
Cc                `    U R                   R                  U[        R                  " 5       5      $ )z+
:param key: the key to get the expiry for
)r,   rL   r   rf   s     r   
get_expiryMemoryStorage.get_expiry   s#    
 ##C55r   c                    [         R                   " 5       nU R                  R                  U/ 5      =n(       a-  [        R                  " XTU-
  * S S9nXVS-
     R
                  U4$ US4$ )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   )rp   s    r   rF   1MemoryStorage.get_moving_window.<locals>.<lambda>   s
    %++r   rH      r   )r   r-   rL   rM   rN   r   )r   rI   rn   r   ro   r-   rP   s          r   get_moving_windowMemoryStorage.get_moving_window   sk     IIK	[[__S"--6-''f,-3MF 1*%++V33!|r   c                H   XB:  a  g[         R                   " 5       nU R                  XU5      u  pgU R                  XgX55      u  nn	n
nX-  U-  U
-   n[        U5      U-   U:  a  gU R	                  USU-  US9n
X-  U-  U
-   n[        U5      U:  a  U R                  Xt5        gg)NF   )r^   T)r   sliding_window_keys_get_sliding_window_infor   r_   rc   )r   rI   rn   r   r^   nowprevious_keycurrent_keyprevious_countprevious_ttlcurrent_countr4   weighted_counts                r   acquire_sliding_window_entry*MemoryStorage.acquire_sliding_window_entry   s     >iik$($<$<S#$N! )),VQ	
'6?-O 6)E1 !IIk1v:fIMM+:VCmSN^$u, 		+.r   c                    U R                  U5      nU R                  U5      nUS:X  a  [        S5      nOSXC-
  U-  S-  -
  U-  nSXC-  S-  -
  U-  U-   nXWXh4$ )Nr   ry   )rL   r   )	r   r   r   r   r   r   r   r   current_ttls	            r   r   &MemoryStorage._get_sliding_window_info   sr     ,/-Q 8L3<6"9Q!>?6ILS\Q./69FB]GGr   c                x    [         R                   " 5       nU R                  XU5      u  pEU R                  XEX#5      $ r   )r   r~   r   r   rI   r   r   r   r   s         r   get_sliding_window MemoryStorage.get_sliding_window   s7     iik$($<$<S#$N!,,\TTr   c                    [         R                   " 5       nU R                  XU5      u  pEU R                  U5        U R                  U5        g r   )r   r~   ri   r   s         r   clear_sliding_window"MemoryStorage.clear_sliding_window   s:    iik$($<$<S#$N!

< 

;r   c                    g)z
check if storage is healthy
Tr    rU   s    r   checkMemoryStorage.check   s    
 r   c                :   [        [        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$ r   )rb   lenr(   r-   ri   r,   r+   )r   	num_itemss     r   resetMemoryStorage.reset   sh    DLL)3t{{+;<	 

r   )r-   r,   r+   r(   r0   )NF)r3   z
str | Noner'   boolr4   str)r   dict[str, limits.typing.Any])r;   r   r   r   )r   r   )r   z-type[Exception] | tuple[type[Exception], ...])ry   )rI   r   r   r   r^   intr   r   )rI   r   r^   r   r   r   )rI   r   r   r   )rI   r   r   r   )
rI   r   rn   r   r   r   r^   r   r   r   )rI   r   r   r   )rI   r   rn   r   r   r   r   ztuple[float, int])
r   r   r   r   r   r   r   r   r   tuple[int, float, int, float])rI   r   r   r   r   r   )rI   r   r   r   r   r   )r   r   )r   z
int | None)r   r   r   r   __doc__STORAGE_SCHEMEr   r<   rA   r/   r]   propertyrZ   r_   rc   rL   ri   rq   rt   rz   r   r   r   r   r   r   r   __classcell__)r5   s   @r   r"   r"      s    ZND D* 
 	6 
( (
("26,   	
  
BHH H 	H
 H 
'H UU #U	&U  r   r"   )
__future__r   rM   r)   r   collectionsr   r   mathr   limits.typinglimitslimits.storage.baser   r   r	   r
   r   r"   r    r   r   <module>r      sF    "    ,   * *k "=?Wkr   