
    ZeIir&              	          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dS )    )annotationsN)Counterdefaultdict)floor)versionadded)MovingWindowSupportSlidingWindowCounterSupportStorage)TimestampedSlidingWindowc                      e Zd ZddZdS )EntryexpiryintreturnNonec                T    t          j                     | _        | j        |z   | _        d S N)timeatimer   )selfr   s     ^/var/www/html/MCyber-Diagnostic/venv/lib/python3.11/site-packages/limits/aio/storage/memory.py__init__zEntry.__init__   s!    Y[[
j6)    N)r   r   r   r   )__name__
__module____qualname__r    r   r   r   r      s(        * * * * * *r   r   z2.1)versionc                       e Zd ZdZdgZ	 	 d1d2 fdZd3dZd4dZd5dZd5dZ	e
d6d            Zd7d8dZd7d9dZd:dZd;dZ	 d7d<d!Zd=d"Zd>d$Z	 d7d<d%Zd?d'Zd@d(ZdAd,ZdBd-ZdCd/Zd5d0Z xZS )D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+memoryNFuri
str | Nonewrap_exceptionsbool_strr   r   c                    t                      | _        t          t          j                  | _        i | _        i | _        d | _         t                      j
        |fd|i| d S )Nr#   )r   storager   asyncioLocklocksexpirationseventstimersuperr   )r   r!   r#   r%   	__class__s       r   r   zMemoryStorage.__init__*   sa     4;995@5N5N
-/.004
CCoCCCCCCr   dict[str, limits.typing.Any]c                D    | j                                         }|d= |d= |S )Nr.   r+   )__dict__copyr   states     r   __getstate__zMemoryStorage.__getstate__4   s(    ""$$'N'Nr   r6   c                    | j                             |           d | _        t          t          j                  | _        t	          j        |                                            d S r   )	r3   updater.   r   r)   r*   r+   ensure_future_MemoryStorage__schedule_expiryr5   s     r   __setstate__zMemoryStorage.__setstate__:   sS    U###
 ..
d446677777r   c                  K   	 t          j                     t          | j                                                  D ]}| j        |         4 d {V  t          j        fd| j        |                    d {V }| j                            |g           r| j        |         d |         | j        |<   | j                            |d           s6| j                            |d            | j                            |d            d d d           d {V  n# 1 d {V swxY w Y   t          | j	                                                  D ]u}| j	        |         t          j                     k    rQ| j
                            |d            | j	                            |d            | j                            |d            vd S # t
          j        $ r Y d S w xY w)Nc                6    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   rA   z/MemoryStorage.__expire_events.<locals>.<lambda>F   s&    V%7 3$,G,G& & & r   )r   listr-   keysr+   r)   	to_threadgetpopr,   r(   CancelledError)r   rC   cutoffrG   s      @r   __expire_eventszMemoryStorage.__expire_events@   s}     	)++CDK,,..// 2 2:c? 2 2 2 2 2 2 2 2#*#4    C(	$ $      F {sB// E+/;s+;GVG+DC(;??355 2T222
sD1112 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 D,113344 . .#C(DIKK77L$$S$///$((d333JNN3---	. .
 % 	 	 	FF	s8   AG B3DG 
D"	"G %D"	&B G GGc                   K   | j         r| j                                         r-t          j        |                                           | _         d S d S r   )r.   doner)   create_task_MemoryStorage__expire_eventsr   s    r   __schedule_expiryzMemoryStorage.__schedule_expiryY   sS      z 	ETZ__.. 	E ,T-A-A-C-CDDDJJJ	E 	Er   -type[Exception] | tuple[type[Exception], ...]c                    t           S r   )
ValueErrorrT   s    r   base_exceptionszMemoryStorage.base_exceptions]   s
     r      rC   r   floatamountr   c                  K   |                      |           d{V  |                                  d{V  | j        |         4 d{V  | j        |xx         |z  cc<   | j        |         |k    rt	          j                    |z   | j        |<   	 ddd          d{V  n# 1 d{V swxY w Y   | j                             ||          S )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)rK   r;   r+   r(   r   r,   )r   rC   r   r\   s       r   incrzMemoryStorage.incrc   sn      hhsmm$$&&&&&&&&&:c? 	= 	= 	= 	= 	= 	= 	= 	=L'|C F**(,	f(< %<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= |V,,,s   AB##
B-0B-c                `  K   |                      |           d{V  |                                  d{V  | j        |         4 d{V  t          | j        |         |z
  d          | j        |<   ddd          d{V  n# 1 d{V swxY w Y   | j                             ||          S )z
        decrements the counter for a given rate limit key. 0 is the minimum allowed value.

        :param amount: the number to increment by
        Nr   )rK   r;   r+   maxr(   )r   rC   r\   s      r   decrzMemoryStorage.decrs   sh      hhsmm$$&&&&&&&&&:c? 	C 	C 	C 	C 	C 	C 	C 	C #DL$5$> B BDL	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C |V,,,s   'B
BBc                >  K   | j                             |d          t          j                    k    rQ| j                            |d           | j                             |d           | j                            |d           | j                            |d          S )zB
        :param key: the key to get the counter value for
        r   N)r,   rK   r   r(   rL   r+   r   rC   s     r   rK   zMemoryStorage.get   s       Q''49;;66LS$'''  d+++JNN3%%%|Q'''r   c                   K   | j                             |d           | j                            |d           | j                            |d           | j                            |d           dS )z>
        :param key: the key to clear rate limits for
        N)r(   rL   r,   r-   r+   rc   s     r   clearzMemoryStorage.clear   sk       	d###S$'''T"""
sD!!!!!r   limitc                  K   ||k    rdS |                                   d{V  | j        |         4 d{V  | j                            |g            t	          j                    }	 | j        |         ||z
           }n# t
          $ r d}Y nw xY w|r"|j        ||z
  k    r	 ddd          d{V  dS t          |          g|z  | j        |         dd<   	 ddd          d{V  dS # 1 d{V swxY w Y   dS )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   rC   rf   r   r\   	timestampentrys          r   acquire_entryzMemoryStorage.acquire_entry   s      E>>5$$&&&&&&&&&:c? 	 	 	 	 	 	 	 	K""3+++	I&*k#&6uv~&F     @	F(:::	 	 	 	 	 	 	 	 	 	 	 	 	 	 ).f'?C !$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s;   /C,(A?>C,?BC,BC,5$C,,
C69C6c                ^   K   | j                             |t          j                              S )z;
        :param key: the key to get the expiry for
        )r,   rK   r   rc   s     r   
get_expiryzMemoryStorage.get_expiry   s'      
 ##C555r   tuple[float, int]c                   K   t          j                     }| j                            |g           x}r.t          j        |||z
   d           }||dz
           j        |fS |dfS )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   )rk   s    r   rA   z1MemoryStorage.get_moving_window.<locals>.<lambda>   s
    %+ r   rB   rZ   r   )r   r-   rK   rD   rE   r   )r   rC   rf   r   rj   r-   oldests          r   get_moving_windowzMemoryStorage.get_moving_window   s}       IKK	[__S"---6 	4')f,-3M3M  F &1*%+V33!|r   c                  K   ||k    rdS t          j                     }|                     |||          \  }}|                     ||||           d {V \  }}	}
}||	z  |z  |
z   }t          |          |z   |k    rdS |                     |d|z  |           d {V }
||	z  |z  |
z   }t          |          |k    r|                     ||           d {V  dS dS )NF   )r\   T)r   sliding_window_keys_get_sliding_window_infor   r^   ra   )r   rC   rf   r   r\   rG   previous_keycurrent_keyprevious_countprevious_ttlcurrent_countr%   weighted_counts                r   acquire_sliding_window_entryz*MemoryStorage.acquire_sliding_window_entry   s3      E>>5ikk$($<$<S&#$N$N!k //k6SVWWWWWWWW	
',6?-O  6)E115 #'))KVF)"S"SSSSSSSM+l:VCmSN^$$u,, iiV444444444u4r   tuple[int, float, int, float]c                   K   t          j                     }|                     |||          \  }}|                     ||||           d {V S r   )r   rv   rw   r   rC   r   rG   rx   ry   s         r   get_sliding_windowz MemoryStorage.get_sliding_window   sn       ikk$($<$<S&#$N$N!k22+vs
 
 
 
 
 
 
 
 	
r   c                   K   t          j                     }|                     |||          \  }}|                     |           d {V  |                     |           d {V  d S r   )r   rv   re   r   s         r   clear_sliding_windowz"MemoryStorage.clear_sliding_window   s{      ikk$($<$<S&#$N$N!kjj%%%%%%%%%jj&&&&&&&&&&&r   rx   ry   rG   c                   K   |                      |           d {V }|                      |           d {V }|dk    rt          d          }nd||z
  |z  dz  z
  |z  }d||z  dz  z
  |z  |z   }||||fS )Nr   rZ   )rK   r[   )	r   rx   ry   r   rG   rz   r|   r{   current_ttls	            r   rw   z&MemoryStorage._get_sliding_window_info   s        $xx55555555"hh{33333333Q 88LL3<6"9Q!>?6ILS6\Q./69FB|]KGGr   c                
   K   dS )z-
        check if storage is healthy
        Tr   rT   s    r   checkzMemoryStorage.check
  s      
 tr   
int | Nonec                :  K   t          t          | j                  t          | j                            }| j                                         | j                                         | j                                         | j                                         |S r   )r`   lenr(   r-   re   r,   r+   )r   	num_itemss     r   resetzMemoryStorage.reset  s      DL))3t{+;+;<<	   
r   c                    	 | j         r4| j                                         s| j                                          d S d S d S # t          $ r Y d S w xY wr   )r.   rQ   cancelRuntimeErrorrT   s    r   __del__zMemoryStorage.__del__  st    	z $$*//"3"3 $
!!#####$ $ $ $ 	 	 	DD	s   9A 
AA)NF)r!   r"   r#   r$   r%   r&   r   r   )r   r1   )r6   r1   r   r   )r   r   )r   rV   )rZ   )rC   r&   r   r[   r\   r   r   r   )rC   r&   r\   r   r   r   )rC   r&   r   r   )rC   r&   r   r   )
rC   r&   rf   r   r   r   r\   r   r   r$   )rC   r&   r   r[   )rC   r&   rf   r   r   r   r   ro   )rC   r&   r   r   r   r   )rC   r&   r   r   r   r   )
rx   r&   ry   r&   r   r   rG   r[   r   r   )r   r$   )r   r   )r   r   r   __doc__STORAGE_SCHEMEr   r7   r<   rS   r;   propertyrY   r^   ra   rK   re   rl   rn   rs   r~   r   r   rw   r   r   r   __classcell__)r0   s   @r   r    r       s         %%N ?DD D D D D D D   8 8 8 8   2E E E E    X
- - - - - - - - - -	( 	( 	( 	(" " " " @A    66 6 6 6   2     B
 
 
 
' ' ' 'H H H H              r   r    )
__future__r   r)   rD   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      sJ   " " " " " "    , , , , , , , ,       * * * * * *             
 9 8 8 8 8 8* * * * * * * * eD D D D D "=?WD D D D Dr   