
    ZeIi"                        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
Zd dlmZmZmZmZ  G d d          Z G d d	eeee          ZdS )
    )annotationsN)Counterdefaultdict)floor)MovingWindowSupportSlidingWindowCounterSupportStorageTimestampedSlidingWindowc                      e Zd ZddZdS )EntryexpiryfloatreturnNonec                T    t          j                     | _        | j        |z   | _        d S N)timeatimer   )selfr   s     Z/var/www/html/MCyber-Diagnostic/venv/lib/python3.11/site-packages/limits/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   c                       e Zd ZdZdgZd0d1 fdZd2dZd3dZd4dZd4dZ	e
d5d            Zd6d7dZd6d8dZd9dZd:dZd6d;d!Zd<d"Zd=d$Z	 d6d;d%Zd>d*Zd?d+Zd@d,ZdAd-ZdBd/Z xZS )CMemoryStoragez
    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.

    memoryNFuri
str | Nonewrap_exceptionsbool_strc                .   t                      | _        t          t          j                  | _        i | _        i | _        t          j        d| j	                  | _
        | j
                                          t                      j        |fd|i| d S )N{Gz?r"   )r   storager   	threadingRLocklocksexpirationseventsTimer_MemoryStorage__expire_eventstimerstartsuperr   )r   r    r"   r$   	__class__s       r   r   zMemoryStorage.__init__$   s    3:998CIO8T8T
-/.0&/odD<P&Q&Q

CCoCCCCCCr   r   dict[str, limits.typing.Any]c                D    | j                                         }|d= |d= |S )Nr0   r+   )__dict__copyr   states     r   __getstate__zMemoryStorage.__getstate__-   s(    ""$$'N'Nr   r9   r   c                    | j                             |           t          t          j                  | _        t          j        d| j                  | _        | j        	                                 d S Nr'   )
r6   updater   r)   r*   r+   r.   r/   r0   r1   r8   s     r   __setstate__zMemoryStorage.__setstate__3   sY    U### 11
_T4+?@@

r   c                   t          | j                                                  D ]}| j        |         5  | j                            |g           x}rGt          j        |t          j                     d           }| j        |         d |         | j        |<   | j                            |d           s| j                            |d            d d d            n# 1 swxY w Y   t          | j	                                                  D ]u}| j	        |         t          j                    k    rQ| j
                            |d            | j	                            |d            | j                            |d            vd S )Nc                    | j          S r   )r   )events    r   <lambda>z/MemoryStorage.__expire_events.<locals>.<lambda>>   s
     r   key)listr-   keysr+   getbisectbisect_leftr   popr,   r(   )r   rD   r-   oldests       r   __expire_eventszMemoryStorage.__expire_events9   s   ((**++ 	. 	.CC . .![__S"5556 A#/2M2M  F (,{3'7'@DK${sD11 .JNN3---. . . . . . . . . . . . . . . (--//00 	* 	*C$	33  d+++ $$S$///
sD)))		* 	*s   BCC!	$C!	c                    | j                                         s:t          j        d| j                  | _         | j                                          d S d S r<   )r0   is_aliver)   r.   r/   r1   r   s    r   __schedule_expiryzMemoryStorage.__schedule_expiryI   sR    z""$$ 	"t/CDDDJJ	 	r   -type[Exception] | tuple[type[Exception], ...]c                    t           S r   )
ValueErrorrO   s    r   base_exceptionszMemoryStorage.base_exceptionsN   s
     r      rD   r   r   amountintc                \   |                      |           |                                  | j        |         5  | j        |xx         |z  cc<   | j        |         |k    rt	          j                    |z   | j        |<   ddd           n# 1 swxY w Y   | j                             |d          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
        Nr   )rG   _MemoryStorage__schedule_expiryr+   r(   r   r,   )r   rD   r   rV   s       r   incrzMemoryStorage.incrT   s     	   Z_ 	= 	=L'|C F**(,	f(< %	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= |Q'''s   ABBBc                    |                      |           |                                  | j        |         5  t          | j        |         |z
  d          | j        |<   ddd           n# 1 swxY w Y   | j                             |d          S )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)rG   rY   r+   maxr(   )r   rD   rV   s      r   decrzMemoryStorage.decrd   s     	   Z_ 	C 	C #DL$5$> B BDL	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C 	C |Q'''s   'A**A.1A.c                :   | 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,   rG   r   r(   rJ   r+   r   rD   s     r   rG   zMemoryStorage.getr   s    
 Q''49;;66LS$'''  d+++JNN3%%%|Q'''r   c                    | j                             |d           | j                            |d           | j                            |d           | j                            |d           dS )z>
        :param key: the key to clear rate limits for
        N)r(   rJ   r,   r-   r+   r_   s     r   clearzMemoryStorage.clear~   sg     	d###S$'''T"""
sD!!!!!r   limitc                   ||k    rdS |                                   | j        |         5  | 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S t          |          g|z  | j        |         dd<   	 ddd           dS # 1 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)rY   r+   r-   
setdefaultr   
IndexErrorr   r   )r   rD   rb   r   rV   	timestampentrys          r   acquire_entryzMemoryStorage.acquire_entry   sd    E>>5   Z_ 	 	K""3+++	IC(8     	F(:::	 	 	 	 	 	 	 	 ).f'?C !$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s;   /CA10C1B =C?B  C!$CCCc                Z    | j                             |t          j                              S )z;
        :param key: the key to get the expiry for
        )r,   rG   r   r_   s     r   
get_expiryzMemoryStorage.get_expiry   s#    
 ##C555r   tuple[float, int]c                    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   )rg   s    r   rB   z1MemoryStorage.get_moving_window.<locals>.<lambda>   s
    %+ r   rC   rU   r   )r   r-   rG   rH   rI   r   )r   rD   rb   r   rf   r-   rK   s          r   get_moving_windowzMemoryStorage.get_moving_window   sy     IKK	[__S"---6 	4')f,-3M3M  F &1*%+V33!|r   c                   ||k    rdS t          j                     }|                     |||          \  }}|                     ||||          \  }}	}
}||	z  |z  |
z   }t          |          |z   |k    rdS |                     |d|z  |          }
||	z  |z  |
z   }t          |          |k    r|                     ||           dS dS )NF   )rV   T)r   sliding_window_keys_get_sliding_window_infor   rZ   r]   )r   rD   rb   r   rV   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>>5ikk$($<$<S&#$N$N!k )),VSQQ	
',6?-O  6)E115 !IIk1v:fIMMM+l:VCmSN^$$u,, 		+v...u4r   rt   ru   rs   tuple[int, float, int, float]c                    |                      |          }|                      |          }|dk    rt          d          }nd||z
  |z  dz  z
  |z  }d||z  dz  z
  |z  |z   }||||fS )Nr   rU   )rG   r   )	r   rt   ru   r   rs   rv   rx   rw   current_ttls	            r   rr   z&MemoryStorage._get_sliding_window_info   s     ,//--Q 88LL3<6"9Q!>?6ILS6\Q./69FB|]KGGr   c                    t          j                     }|                     |||          \  }}|                     ||||          S r   )r   rq   rr   r   rD   r   rs   rt   ru   s         r   get_sliding_windowz MemoryStorage.get_sliding_window   sF     ikk$($<$<S&#$N$N!k,,\;PSTTTr   c                    t          j                     }|                     |||          \  }}|                     |           |                     |           d S r   )r   rq   ra   r   s         r   clear_sliding_windowz"MemoryStorage.clear_sliding_window   sS    ikk$($<$<S&#$N$N!k

<   

;r   c                    dS )z-
        check if storage is healthy
        Tr   rO   s    r   checkzMemoryStorage.check   s	    
 tr   
int | Nonec                6   t          t          | j                  t          | j                            }| j                                         | j                                         | j                                         | j                                         |S r   )r\   lenr(   r-   ra   r,   r+   )r   	num_itemss     r   resetzMemoryStorage.reset   s|    DL))3t{+;+;<<	   
r   )NF)r    r!   r"   r#   r$   r%   )r   r4   )r9   r4   r   r   )r   r   )r   rQ   )rU   )rD   r%   r   r   rV   rW   r   rW   )rD   r%   rV   rW   r   rW   )rD   r%   r   rW   )rD   r%   r   r   )
rD   r%   rb   rW   r   rW   rV   rW   r   r#   )rD   r%   r   r   )rD   r%   rb   rW   r   rW   r   rk   )
rt   r%   ru   r%   r   rW   rs   r   r   r{   )rD   r%   r   rW   r   r{   )rD   r%   r   rW   r   r   )r   r#   )r   r   )r   r   r   __doc__STORAGE_SCHEMEr   r:   r>   r/   rY   propertyrT   rZ   r]   rG   ra   rh   rj   rn   rz   rr   r   r   r   r   __classcell__)r3   s   @r   r   r      s         ZND D D D D D D      * * * *    
    X
( ( ( ( ( ( ( ( ( (
( 
( 
( 
(" " " "    26 6 6 6   ,     BH H H H U U U U                 r   r   )
__future__r   rH   r)   r   collectionsr   r   mathr   limits.typinglimitslimits.storage.baser   r   r	   r
   r   r   r   r   r   <module>r      s   " " " " " "       , , , , , , , ,                     * * * * * * * *k k k k k "=?Wk k k k kr   