
    ZeIi)                    6   d Z ddlmZ ddlZddlmZmZ ddlmZm	Z	 ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZ ddlmZ  G d de          Z G d de          Z G d de          Z ed           G d de                      ZeeedZdS )z'
Asynchronous rate limiting strategies
    )annotationsN)ABCabstractmethod)floorinf)versionadded   )RateLimitItem)StorageTypes)cast)WindowStats   )MovingWindowSupportStorage)SlidingWindowCounterSupportc                  r    e Zd ZddZedddd            Zedddd            Zedd            ZddZdS )RateLimiterstorager   c                B    t          |t                    sJ || _        d S N)
isinstancer   r   )selfr   s     Z/var/www/html/MCyber-Diagnostic/venv/lib/python3.11/site-packages/limits/aio/strategies.py__init__zRateLimiter.__init__   s#    '7+++++ '    r   costitemr
   identifiersstrr   intreturnboolc                  K   t           )B  
        Consume the rate limit

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :param cost: The cost of this hit, default 1

        :return: True if ``cost`` could be deducted from the rate limit without exceeding it
        NotImplementedErrorr   r   r   r   s       r   hitzRateLimiter.hit          "!r   c                  K   t           ):  
        Check if the rate limit can be consumed

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :param cost: The expected cost to be consumed, default 1

        :return: True if the rate limit is not depleted
        r&   r(   s       r   testzRateLimiter.test(   r*   r   r   c                   K   t           )z
        Query the reset time and remaining amount for the limit

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :return: (reset time, remaining))
        r&   r   r   r   s      r   get_window_statszRateLimiter.get_window_stats6   r*   r   Nonec                V   K   | j                              |j        |            d {V S r   )r   clearkey_forr/   s      r   r3   zRateLimiter.clearD   s7      \''k(BCCCCCCCCCr   Nr   r   r   r
   r   r    r   r!   r"   r#   r   r
   r   r    r"   r   r   r
   r   r    r"   r1   )	__name__
__module____qualname__r   r   r)   r-   r0   r3    r   r   r   r      s        ( ( ( ( LM " " " " " ^" MN " " " " " ^" " " " ^"D D D D D Dr   r   c                  H     e Zd ZdZd fdZddddZddddZddZ xZS )MovingWindowRateLimiterz4
    Reference: :ref:`strategies:moving window`
    r   r   r"   r1   c                    t          |d          s't          |d          st          d|j                   t                                          |           d S )Nacquire_entryget_moving_windowz@MovingWindowRateLimiting is not implemented for storage of type hasattrr'   	__class__superr   r   r   rD   s     r   r   z MovingWindowRateLimiter.__init__M   sq    G_--	18BU1V1V	 &/",/ /   	!!!!!r   r   r   r   r
   r   r    r   r!   r#   c                  K   t          t          | j                                       |j        | |j        |                                |           d{V S r%   )amountN)r   r   r   r@   r4   rI   
get_expiryr(   s       r   r)   zMovingWindowRateLimiter.hitW   so       -t|<<JJDL+&T__5F5Ft K 
 
 
 
 
 
 
 
 	
r   c                  K   t          t          | j                                       |j        | |j        |                                           d{V }|d         }||j        |z
  k    S r,   Nr   )r   r   r   rA   r4   rI   rJ   )r   r   r   r   resrI   s         r   r-   zMovingWindowRateLimiter.testg   s       ,dl;;MMDL+&KOO
 
 
 
 
 
 
 

 Qt+++r   r   c                  K   t          t          | j                                       |j        | |j        |                                           d{V \  }}||                                z   }t          ||j        |z
            S )z
        returns the number of requests remaining within this limit.

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :return: (reset time, remaining)
        N)r   r   r   rA   r4   rI   rJ   r   )r   r   r   window_startwindow_itemsresets         r   r0   z(MovingWindowRateLimiter.get_window_stats{   s       ,0,
 ,


LDL+6T__EVEV
W
W&X &X &X &X &X &X"l t0005$+"<===r   )r   r   r"   r1   r6   r7   )	r9   r:   r;   __doc__r   r)   r-   r0   __classcell__rD   s   @r   r>   r>   H   s         " " " " " " MN 
 
 
 
 
 
  NO , , , , , ,(> > > > > > > >r   r>   c                  6    e Zd ZdZddddZddddZddZdS )FixedWindowRateLimiterz3
    Reference: :ref:`strategies:fixed window`
    r   r   r   r
   r   r    r   r!   r"   r#   c                  K   | j                              |j        | |                                |           d{V |j        k    S rH   )r   incrr4   rJ   rI   r(   s       r   r)   zFixedWindowRateLimiter.hit   sm       ,##k*!! $        
 {	
r   c               t   K   | j                              |j        |            d{V |j        |z
  dz   k     S rL   )r   getr4   rI   r(   s       r   r-   zFixedWindowRateLimiter.test   sM       ,""<4<#=>>>>>>>>tASVWAWW	
r   r   c           	        K   t          d|j        | j                             |j        |            d{V z
            }| j                             |j        |            d{V }t          ||          S )z
        Query the reset time and remaining amount for the limit

        :param item: the rate limit item
        :param identifiers: variable list of strings to uniquely identify the
         limit
        :return: reset time, remaining
        r   N)maxrI   r   rZ   r4   rJ   r   )r   r   r   	remainingrQ   s        r   r0   z'FixedWindowRateLimiter.get_window_stats   s       K 0 0{1K L LLLLLLLL
 
	 l--ldlK.HIIIIIIII5),,,r   Nr6   r7   )r9   r:   r;   rR   r)   r-   r0   r<   r   r   rV   rV      s|          MN 
 
 
 
 
 
* NO 
 
 
 
 
 
 - - - - - -r   rV   z4.1)versionc                  X     e Zd ZdZd fdZddZddddZddddZddZddZ	 xZ
S )SlidingWindowCounterRateLimiterz=
    Reference: :ref:`strategies:sliding window counter`
    r   r   c                    t          |d          rt          |d          st          d|j                   t                                          |           d S )Nget_sliding_windowacquire_sliding_window_entryzHSlidingWindowCounterRateLimiting is not implemented for storage of type rB   rF   s     r   r   z(SlidingWindowCounterRateLimiter.__init__   sv    w 455 	W3>
 >
 	 &/",/ /   	!!!!!r   r   r
   previous_countr!   previous_expires_infloatcurrent_countr"   c                <    ||z  |                                 z  |z   S )zu
        Return the approximated by weighting the previous window count and adding the current window count.
        )rJ   )r   r   rd   re   rg   s        r   _weighted_countz/SlidingWindowCounterRateLimiter._weighted_count   s$      33doo6G6GG-WWr   r   r   r   r    r   r#   c                  K   t          t          | j                                       |j        | |j        |                                |           d{V S )aS  
        Consume the rate limit

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The cost of this hit, default 1

        :return: True if ``cost`` could be deducted from the rate limit without exceeding it
        N)r   r   r   rc   r4   rI   rJ   r(   s       r   r)   z#SlidingWindowCounterRateLimiter.hit   sp       '
 

&
&DL+&KOO	
 

 
 
 
 
 
 	
r   c                  K   t          t          | j                                       |j        | |                                           d{V \  }}}}|                     ||||          |j        |z
  dz   k     S )aK  
        Check if the rate limit can be consumed

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The expected cost to be consumed, default 1

        :return: True if the rate limit is not depleted
        Nr   )r   r   r   rb   r4   rJ   ri   rI   )r   r   r   r   rd   re   rg   _s           r   r-   z$SlidingWindowCounterRateLimiter.test   s       GK'G
 G


\T\;79J9J
K
KAL AL AL AL AL AL=+]A
   n&9=  kD 1$%	
r   r   c                  K   t          t          | j                                       |j        | |                                           d{V \  }}}}t          d|j        t          | 	                    ||||                    z
            }t          j
                    }|s|st          ||          S |                                }	t          t          }}
|r||	|z  z  }
|r||	z  }t          |t          |
|          z   |          S )a  
        Query the reset time and remaining amount for the limit.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: (reset time, remaining)
        Nr   )r   r   r   rb   r4   rJ   r\   rI   r   ri   timer   r   min)r   r   r   rd   re   rg   current_expires_inr]   nowexpiryprevious_reset_incurrent_reset_ins               r   r0   z0SlidingWindowCounterRateLimiter.get_window_stats  sJ     " 2DLAATTDL+&(9(9
 
 
 
 
 
 
 
	

 K$$.*=}  
 
	 ikk 	/- 	/sI..."".13+ 	P 3v7N O 	;1F:3%68H!I!II9UUUr   r1   c                   K   t          t          | j                                       |j        | |                                           d {V S r   )r   r   r   clear_sliding_windowr4   rJ   r/   s      r   r3   z%SlidingWindowCounterRateLimiter.clearA  sg      '
 


|t|[94??;L;L
M
MN N N N N N 	Nr   r5   )
r   r
   rd   r!   re   rf   rg   r!   r"   rf   r6   r7   r8   )r9   r:   r;   rR   r   ri   r)   r-   r0   r3   rS   rT   s   @r   r`   r`      s         " " " " " "
X 
X 
X 
X MN 
 
 
 
 
 
( NO 
 
 
 
 
 
.,V ,V ,V ,V\N N N N N N N Nr   r`   )zsliding-window-counterzfixed-windowzmoving-window)rR   
__future__r   rn   abcr   r   mathr   r   deprecated.sphinxr   limitsr
   r   r   typingr   utilr   r   r   storage.baser   r   r>   rV   r`   
STRATEGIESr<   r   r   <module>r      s    # " " " " "  # # # # # # # #         * * * * * * " " " " " " " " " " " "             1 1 1 1 1 1 1 1 5 5 5 5 5 50D 0D 0D 0D 0D# 0D 0D 0DfC> C> C> C> C>k C> C> C>L;- ;- ;- ;- ;-[ ;- ;- ;-| ewN wN wN wN wNk wN wN wNv >*, 


r   