
    9ir&              	          d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
mZ d dlZd dlmZmZmZ d dlmZ  G d d	      Z ed
       G d deeee             Zy)    )annotationsN)Counterdefaultdict)floor)versionadded)MovingWindowSupportSlidingWindowCounterSupportStorage)TimestampedSlidingWindowc                      e Zd ZddZy)Entryc                ^    t        j                          | _        | j                  |z   | _        y N)timeatimeexpiry)selfr   s     L/var/www/html/venv/lib/python3.12/site-packages/limits/aio/storage/memory.py__init__zEntry.__init__   s    YY[
jj6)    N)r   intreturnNone)__name__
__module____qualname__r    r   r   r   r      s    *r   r   z2.1)versionc                  D    e Zd ZdZdgZ	 	 d	 	 	 	 	 	 	 d fdZddZddZddZddZ	e
	 	 dd       Zddd	Zddd
Zd dZd!dZ	 d	 	 	 	 	 	 	 	 	 d"dZd#dZ	 	 	 	 	 	 	 	 d$dZ	 d	 	 	 	 	 	 	 	 	 d"dZ	 	 	 	 	 	 d%dZd&dZ	 	 	 	 	 	 	 	 	 	 d'dZd(dZd)dZddZ xZS )*MemoryStoragez
    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                    t               | _        t        t        j                        | _        i | _        i | _        d | _        t        | (  |fd|i| y )Nwrap_exceptions)r   storager   asyncioLocklocksexpirationseventstimersuperr   )r   urir"   _	__class__s       r   r   zMemoryStorage.__init__*   sK     4;95@5N
-/.004
CoCCr   c                F    | j                   j                         }|d= |d= |S )Nr)   r&   )__dict__copyr   states     r   __getstate__zMemoryStorage.__getstate__4   s'    ""$'N'Nr   c                    | j                   j                  |       d | _        t        t        j
                        | _        t	        j                  | j                                y r   )	r/   updater)   r   r$   r%   r&   ensure_future_MemoryStorage__schedule_expiryr1   s     r   __setstate__zMemoryStorage.__setstate__:   sB    U#
 .
d4467r   c                  K   	 t        j                          t        | j                  j                               D ]  }| j                  |   4 d {    t        j                  fd| j                  |          d {   }| j                  j                  |g       r| j                  |   d | | j                  |<   | j                  j                  |d       s8| j                  j                  |d        | j                  j                  |d        d d d       d {     t        | j                  j                               D ]{  }| j                  |   t        j                          k  s(| j                  j                  |d        | j                  j                  |d        | j                  j                  |d        } y 7 v7 K7 # 1 d {  7  sw Y   xY w# t
        j                  $ r Y y w xY ww)Nc                8    t        j                  |  d       S )Nc                    | j                    S r   )r   )events    r   <lambda>zAMemoryStorage.__expire_events.<locals>.<lambda>.<locals>.<lambda>G   s    5<<- r   key)bisectbisect_left)evtsnows    r   r=   z/MemoryStorage.__expire_events.<locals>.<lambda>F   s    V%7%7 3$,G& r   )r   listr(   keysr&   r$   	to_threadgetpopr'   r#   CancelledError)r   r?   cutoffrC   s      @r   __expire_eventszMemoryStorage.__expire_events@   s    	))+CDKK,,./ 2::c? 2 2#*#4#4 C(	$ F {{sB/+/;;s+;GV+DC(;;??35T2

sD12 2 22 D,,1134 .##C(DIIK7LL$$S$/$$((d3JJNN3-	.22 2 2 2$ %% 		s   G?AG& GG& *G GBGG& G AG& 1AG& G?G& GG& G#	GG#	G& &G<9G?;G<<G?c                   K   | j                   r| j                   j                         r)t        j                  | j	                               | _         y y wr   )r)   doner$   create_task_MemoryStorage__expire_eventsr   s    r   __schedule_expiryzMemoryStorage.__schedule_expiryY   s:     zzTZZ__. ,,T-A-A-CDDJ /s   AAc                    t         S r   )
ValueErrorrP   s    r   base_exceptionszMemoryStorage.base_exceptions]   s
     r   c                  K   | j                  |       d{    | j                          d{    | j                  |   4 d{    | j                  |xx   |z  cc<   | j                  |   |k(  r$t	        j                         |z   | j
                  |<   ddd      d{    | j                  j                  ||      S 7 7 7 7 &# 1 d{  7  sw Y   6xY ww)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)rG   r7   r&   r#   r   r'   )r   r?   r   amounts       r   incrzMemoryStorage.incrc   s      hhsm$$&&&::c? 	= 	=LL'||C F*(,		f(<  %	= 	= ||V,, 	&	= 	= 	= 	= 	=sh   C"CC"CC"C	C"ACC"$C%!C"C"	C"C"CCCC"c                  K   | j                  |       d{    | j                          d{    | j                  |   4 d{    t        | j                  |   |z
  d      | j                  |<   ddd      d{    | j                  j                  ||      S 7 7 s7 ]7 &# 1 d{  7  sw Y   6xY ww)z
        decrements the counter for a given rate limit key. 0 is the minimum allowed value.

        :param amount: the number to increment by
        Nr   )rG   r7   r&   maxr#   )r   r?   rV   s      r   decrzMemoryStorage.decrs   s      hhsm$$&&&::c? 	C 	C #DLL$5$> BDLL	C 	C ||V,, 	&	C 	C 	C 	C 	Csg   B>B!B>B#B>B%B>*B)5B> B'!B>#B>%B>'B>)B;/B20B;7B>c                L  K   | j                   j                  |d      t        j                         k  rT| j                  j	                  |d       | j                   j	                  |d       | j
                  j	                  |d       | j                  j                  |d      S w)zB
        :param key: the key to get the counter value for
        r   N)r'   rG   r   r#   rH   r&   r   r?   s     r   rG   zMemoryStorage.get   s|      Q'499;6LLS$'  d+JJNN3%||Q''s   B"B$c                   K   | j                   j                  |d       | j                  j                  |d       | j                  j                  |d       | j                  j                  |d       yw)z>
        :param key: the key to clear rate limits for
        N)r#   rH   r'   r(   r&   r\   s     r   clearzMemoryStorage.clear   sV      	d#S$'T"

sD!s   A2A4c                  K   ||kD  ry| j                          d{    | j                  |   4 d{    | j                  j                  |g        t	        j                         }	 | j                  |   ||z
     }|r$|j                  ||z
  k\  r	 ddd      d{    yt        |      g|z  | j                  |   dd 	 ddd      d{    y7 7 # t
        $ r d}Y hw xY w7 K7 # 1 d{  7  sw Y   yxY ww)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)r7   r&   r(   
setdefaultr   
IndexErrorr   r   )r   r?   limitr   rV   	timestampentrys          r   acquire_entryzMemoryStorage.acquire_entry   s     E>$$&&&::c? 	 	KK""3+		I&*kk#&6uv~&F 	F(::	 	 	 ).f'?C !$	 	 	 	'	
  	 	 	 	 	s   DCDCD1C/*C?C/DC+ D% C/DC-DDC(%C/'C((C/+D-D/D5C86D=Dc                f   K   | j                   j                  |t        j                               S w)z;
        :param key: the key to get the expiry for
        )r'   rG   r   r\   s     r   
get_expiryzMemoryStorage.get_expiry   s'     
 ##C55s   /1c                   K   t        j                          }| j                  j                  |g       x}r1t        j                  |||z
   d       }||dz
     j
                  |fS |dfS w)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                    | j                    S r   )r   )rd   s    r   r=   z1MemoryStorage.get_moving_window.<locals>.<lambda>   s    %++ r   r>      r   )r   r(   rG   r@   rA   r   )r   r?   rb   r   rc   r(   oldests          r   get_moving_windowzMemoryStorage.get_moving_window   ss      IIK	[[__S"--6-'')f,-3MF &1*%++V33!|s   A(A*c                  K   ||kD  ryt        j                          }| j                  |||      \  }}| j                  ||||       d {   \  }}	}
}||	z  |z  |
z   }t        |      |z   |kD  ry| j	                  |d|z  |       d {   }
||	z  |z  |
z   }t        |      |kD  r| j                  ||       d {    yy7 z7 ;7 
w)NF   )rV   T)r   sliding_window_keys_get_sliding_window_infor   rW   rZ   )r   r?   rb   r   rV   rC   previous_keycurrent_keyprevious_countprevious_ttlcurrent_countr,   weighted_counts                r   acquire_sliding_window_entryz*MemoryStorage.acquire_sliding_window_entry   s      E>iik$($<$<S&#$N!k //k6SVWW	
',6?-O 6)E1 #'))KVF)"SSM+l:VCmSN^$u, iiV444 X T 5s7   AC
CA CC2C>C	?CC	Cc                   K   t        j                          }| j                  |||      \  }}| j                  ||||       d {   S 7 wr   )r   ro   rp   r   r?   r   rC   rq   rr   s         r   get_sliding_windowz MemoryStorage.get_sliding_window   sR      iik$($<$<S&#$N!k22+vs
 
 	
 
s   AAA	Ac                   K   t        j                          }| j                  |||      \  }}| j                  |       d {    | j                  |       d {    y 7 7 wr   )r   ro   r^   ry   s         r   clear_sliding_windowz"MemoryStorage.clear_sliding_window   sW     iik$($<$<S&#$N!kjj%%%jj&&& 	&&s#   ?A$A A$A"A$"A$c                   K   | j                  |       d {   }| j                  |       d {   }|dk(  rt        d      }nd||z
  |z  dz  z
  |z  }d||z  dz  z
  |z  |z   }||||fS 7 V7 ?w)Nr   rj   )rG   float)	r   rq   rr   r   rC   rs   ru   rt   current_ttls	            r   rp   z&MemoryStorage._get_sliding_window_info   s       $xx55"hh{33Q 8L3<6"9Q!>?6ILS6\Q./69FB|]KGG 63s   A2A.A2A0>A20A2c                   K   yw)z-
        check if storage is healthy
        Tr   rP   s    r   checkzMemoryStorage.check
  s     
 s   c                B  K   t        t        | j                        t        | j                              }| j                  j	                          | j
                  j	                          | j                  j	                          | j                  j	                          |S wr   )rY   lenr#   r(   r^   r'   r&   )r   	num_itemss     r   resetzMemoryStorage.reset  sl     DLL)3t{{+;<	 

s   BBc                    	 | j                   r6| j                   j                         s| j                   j                          y y y # t        $ r Y y w xY wr   )r)   rM   cancelRuntimeErrorrP   s    r   __del__zMemoryStorage.__del__  sD    	zz$**//"3

!!# #4z 		s   A A 	AA)NF)r+   z
str | Noner"   boolr,   strr   r   )r   dict[str, limits.typing.Any])r2   r   r   r   )r   r   )r   z-type[Exception] | tuple[type[Exception], ...])rj   )r?   r   r   r~   rV   r   r   r   )r?   r   rV   r   r   r   )r?   r   r   r   )r?   r   r   r   )
r?   r   rb   r   r   r   rV   r   r   r   )r?   r   r   r~   )r?   r   rb   r   r   r   r   ztuple[float, int])r?   r   r   r   r   tuple[int, float, int, float])r?   r   r   r   r   r   )
rq   r   rr   r   r   r   rC   r~   r   r   )r   r   )r   z
int | None)r   r   r   __doc__STORAGE_SCHEMEr   r3   r8   rO   r7   propertyrT   rW   rZ   rG   r^   re   rg   rl   rw   rz   r|   rp   r   r   r   __classcell__)r-   s   @r   r    r       s    %%N ?DDD7;DJMD	D82E 	6 
- -	(" @A",/9<	66",/	2   	
  
B

 #
	&
'HH H 	H
 H 
'H r   r    )
__future__r   r$   r@   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   