
    ^g&              	          d dl m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     `/var/www/html/suriana-translation/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                  P    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Z	 d	 	 	 	 	 	 	 	 	 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 and elastic 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__3   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__9   sB    U#
 .
d4467r   c                  K   | j                   j                         D ]  }| j                  |   4 d {    t        | j                   |         D ]T  }|j                  t        j
                         k  s%|| j                   |   v s7| j                   |   j                  |       V | 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 |7 # 1 d {  7  sw Y   xY wwr   )r(   keysr&   listr   r   removegetpopr'   r#   )r   keyevents      r   __expire_eventszMemoryStorage.__expire_events?   si    ;;##% 	.Czz# . .!$++c"23 7E||tyy{2uC@P7PC(//67 {{sD1KKOOC.JJNN3-. . .	. ((--/0 	*C$		3  d+  $$S$/

sD)		*. . . . .sY   1G
F/G
=F45F4A4F4;G
F2AG
AG
2G
4G	:F=;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_expiryO   s:     zzTZZ__. ,,T-A-A-CDDJ /s   AAc                    t         S r   )
ValueErrorrF   s    r   base_exceptionszMemoryStorage.base_exceptionsS   s
     r   c                  K   | j                  |       d{    | j                          d{    | j                  |   4 d{    | j                  |xx   |z  cc<   |s| 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)aD  
        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 elastic_expiry: whether to keep extending the rate limit
         window every hit.
        :param amount: the number to increment by
        N)r=   r7   r&   r#   r   r'   )r   r?   r   elastic_expiryamounts        r   incrzMemoryStorage.incrY   s      hhsm$$&&&::c? 	= 	=LL'c!2f!<(,		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   )r=   r7   r&   maxr#   )r   r?   rM   s      r   decrzMemoryStorage.decro   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'   r=   r   r#   r>   r&   r   r?   s     r   r=   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#   r>   r'   r(   r&   rS   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        |      D cg c]  }t        |       c}| j                  |   dd 	 ddd      d{    y7 7 # t
        $ r d}Y {w xY w7 ^c c}w 7 !# 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   ranger   )r   r?   limitr   rM   	timestampentryr,   s           r   acquire_entryzMemoryStorage.acquire_entry   s     E>$$&&&::c? 	 	KK""3+		I&*kk#&6uv~&F 	F(::	 	 	 @EV}'M!f'MC !$	 	 	 	'	
  	 (N	 	 	 	s   DC)DC+D1D*C-?DDC> D%D2D DD#D$D+D-C;8D:C;;D>D DDDDDDc                f   K   | j                   j                  |t        j                               S w)z;
        :param key: the key to get the expiry for
        )r'   r=   r   rS   s     r   
get_expiryzMemoryStorage.get_expiry   s'     
 ##C55s   /1c                  K   t        j                          }| j                  j                  |      rFt        | j                  j                  |g       D cg c]  }|j                  ||z
  k\  s| c}      S dS c c}w w)z
        returns the number of entries already acquired

        :param key: rate limit key to acquire an entry in
        :param expiry: expiry of the entry
        r   )r   r(   r=   lenr   )r   r?   r   r[   ks        r   get_num_acquiredzMemoryStorage.get_num_acquired   sk      IIK	 {{s# DKKOOC4Vq9vCU8UVW	
 	
Vs   AA?A:-A:1A?c                  K   t        j                          }| j                  ||       d{   }| j                  j                  |g       ddd   D ]$  }|j                  ||z
  k\  s|j                  |fc S  ||fS 7 Q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)
        N)r   rc   r(   r=   r   )r   r?   rZ   r   r[   acquireditems          r   get_moving_windowzMemoryStorage.get_moving_window   s      IIK	..sF;;KKOOC,TrT2 	,DzzY//zz8++	, ("" <s   *B A>;B )B 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   )rM   T)r   sliding_window_keys_get_sliding_window_infor   rN   rQ   )r   r?   rZ   r   rM   now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   rk   rl   )r   r?   r   rm   rn   ro   s         r   get_sliding_windowz MemoryStorage.get_sliding_window   sR      iik$($<$<S&#$N!k22+vs
 
 	
 
s   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      )r=   float)	r   rn   ro   r   rm   rp   rr   rq   current_ttls	            r   rl   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   rF   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   )rP   ra   r#   r(   rU   r'   r&   )r   	num_itemss     r   resetzMemoryStorage.reset  sl     DLL)3t{{+;<	 

s   BB)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], ...])Frx   )
r?   r   r   ry   rL   r   rM   r   r   r   )rx   )r?   r   rM   r   r   r   )r?   r   r   r   )r?   r   r   r   )
r?   r   rZ   r   r   r   rM   r   r   r   )r?   r   r   ry   )r?   r   r   r   r   r   )r?   r   rZ   r   r   r   r   ztuple[float, int])r?   r   r   r   r   tuple[int, float, int, float])
rn   r   ro   r   r   r   rm   ry   r   r   )r   r   )r   z
int | None)r   r   r   __doc__STORAGE_SCHEMEr   r3   r8   rE   r7   propertyrJ   rN   rQ   r=   rU   r]   r_   rc   rh   rt   rv   rl   r|   r   __classcell__)r-   s   @r   r    r       s    %%N ?DDD7;DJMD	D8* E 	6  TU-- %-7;-MP-	-,-	(" @A",/9<	86
 ##"#,/#	#2      	 
   
 D

 #
	&
HH H 	H
 H 
'H r   r    )
__future__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      s`    "   ,  *  
 9* * eF "=?WF Fr   