
    ZeIi(                        d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dl	m
Z
mZ d dlmZ d dlmZ d dlmZmZmZ d dlmZmZmZmZmZmZ d d	lmZ  G d
 deee          ZdS )    )annotationsN)Iterable)ceilfloor)
ModuleType)ConfigurationError)SlidingWindowCounterSupportStorageTimestampedSlidingWindow)AnyCallableMemcachedClientPPRcast)get_dependencyc                       e Zd ZdZdgZ	 dgZ	 d<d= fdZed>d            Zd?dZ	d@dZ
edAd            ZdBd!ZdCd%ZdDd&Z	 	 dEdFd-ZdGd.ZdHd/ZdId0ZdJd2Z	 dKdLd4ZdMd6ZdNd7ZdOd;Z xZS )PMemcachedStoragez[
    Rate limit storage with memcached as backend.

    Depends on :pypi:`pymemcache`.
    	memcached
pymemcacheFuristrwrap_exceptionsbooloptions$str | Callable[[], MemcachedClientP]returnNonec                   t           j                            |          }g | _        |j                                                            d          D ]F}|s|                    d          \  }}| j                            |t          |          f           G|j	        r|j        s|j
        s|j	        g| _        | j        d         j        | _        t          |                    dd                    | _        t          |                    dd                    | _        t%          t&          t(          t*          t,          t          t          f                  gt.          f         |                    d| j                            | _        || _        t7          | j                  st9          d	| j                   t;          j                    | _        d
| j        _         tC                      "                    ||           d
S )an  
        :param uri: memcached location of the form
         ``memcached://host:port,host:port``,
         ``memcached:///var/tmp/path/to/sock``
        :param wrap_exceptions: Whether to wrap storage exceptions in
         :exc:`limits.errors.StorageError` before raising it.
        :param options: all remaining keyword arguments are passed
         directly to the constructor of :class:`pymemcache.client.base.PooledClient`
         or :class:`pymemcache.client.hash.HashClient` (if there are more than
         one hosts specified)
        :raise ConfigurationError: when :pypi:`pymemcache` is not available
        ,:r   libraryzpymemcache.clientcluster_libraryzpymemcache.client.hashclient_getterz5memcached prerequisite not available. please install N)r   )#urllibparseurlparsehostsnetlocstripsplitappendintpathportdependenciesmodule
dependencyr   popr"   r#   r   r   r   listtupler   
get_clientr$   r   r   r   	threadinglocallocal_storagestoragesuper__init__)	selfr   r   r   parsedlochostr/   	__class__s	           ]/var/www/html/MCyber-Diagnostic/venv/lib/python3.11/site-packages/limits/storage/memcached.pyr<   zMemcachedStorage.__init__'   s   $ &&s++
=&&((..s33 		+ 		+C 3JD$JtSYY/0000 { +6= + +$k]
+L9@7;;y2EFFGG"KK)+CDD 
  
 "j$uS#X"78:JJKKK99
 
 dl++ 	$VVV   '_..%)"o>>>>>    -type[Exception] | tuple[type[Exception], ...]c                    | j         j        S N)r2   MemcacheErrorr=   s    rB   base_exceptionsz MemcachedStorage.base_exceptionsZ   s     ,,rC   r1   r   r(   list[tuple[str, int]]kwargsr   c                    t          t          t          |          dk    r |j        |fi |n |j        |i |          S )z
        returns a memcached client.

        :param module: the memcached module
        :param hosts: list of memcached hosts
           )r   r   len
HashClientPooledClient)r=   r1   r(   rK   s       rB   r6   zMemcachedStorage.get_client`   s\      u::>> "!%226222(V(%:6::
 
 	
rC   funcCallable[P, R]argsP.argsP.kwargsr   c                    d|v r9t          j        |          }d|j        v s|j        s|                    d            ||i |S )Nnoreply)inspectgetfullargspecrS   varkwr3   )r=   rQ   rS   rK   argspecs        rB   call_memcached_funcz$MemcachedStorage.call_memcached_funcs   sZ     ,T22G---

9%%%tT$V$$$rC   c                l   t          | j        d          r| j        j        sut          t	          | j                  dk    r| j        n| j                  d         }|st          d| j                    | j	        || j        fi | j
        | j        _        t          t          | j        j                  S )zQ
        lazily creates a memcached client instance using a thread local
        r:   rM   r   zUnable to import )hasattrr9   r:   r   rN   r(   r#   r"   r   r$   r   r   r   )r=   r2   s     rB   r:   zMemcachedStorage.storage~   s     *I66 		4;M;U 		'(+DJ!(;(;$$ J  U()ST=Q)S)STTT););DJ* **.,* *D& $d&8&@AAArC   keyr-   c                R    t          | j                            |d                    S )zB
        :param key: the key to get the counter value for
        0)r-   r:   getr=   r_   s     rB   rb   zMemcachedStorage.get   s$     4<##C--...rC   keysIterable[str]dict[str, Any]c                6    | j                             |          S )z
        Return multiple counters at once

        :param keys: the keys to get the counter values for

        :meta private:
        )r:   get_many)r=   rd   s     rB   rh   zMemcachedStorage.get_many   s     |$$T***rC   c                :    | j                             |           dS )z>
        :param key: the key to clear rate limits for
        N)r:   deleterc   s     rB   clearzMemcachedStorage.clear   s      	C     rC   rM   Texpiryfloatamountset_expiration_keyc                   |                      | j        j        ||d          x}	 |S |                      | j        j        ||t	          |          d          s| j                            ||          p|S |rX|                      | j        j        |                     |          |t          j                    z   t	          |          d           |S )a  
        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
         window every hit.
        :param amount: the number to increment by
        :param set_expiration_key: set the expiration key with the expiration time if needed. If set to False, the key will still expire, but memcached cannot provide the expiration time.
        FrW   N)expirerW   )r\   r:   incraddr   set_expiration_keytime)r=   r_   rl   rn   ro   values         rB   rs   zMemcachedStorage.incr   s    " --!3 .   E 	
 L++ #vtF||U ,    |((f55??% ,,(,,S11,#F|| % -    MrC   c                    t          | j                            |                     |                    pt	          j                              S )z;
        :param key: the key to get the expiry for
        )rm   r:   rb   rv   rw   rc   s     rB   
get_expiryzMemcachedStorage.get_expiry   s;    
 T\%%d&:&:3&?&?@@ODIKKPPPrC   c                    |dz   S )z
        Return the expiration key for the given counter key.

        Memcached doesn't natively return the expiration time or TTL for a given key,
        so we implement the expiration time on a separate key.
        z/expires rc   s     rB   rv   z MemcachedStorage._expiration_key   s     ZrC   c                X    	 |                      | j        j        d           dS #  Y dS xY w)zq
        Check if storage is healthy by calling the ``get`` command
        on the key ``limiter-check``
        zlimiter-checkTF)r\   r:   rb   rH   s    rB   checkzMemcachedStorage.check   s7    
	$$T\%5GGG4	55s    $ )
int | Nonec                    t           rF   )NotImplementedErrorrH   s    rB   resetzMemcachedStorage.reset   s    !!rC   limitc                   ||k    rdS t          j                     }|                     |||          \  }}|                     ||||          \  }}	}
}||	z  |z  |
z   }t          |          |z   |k    rdS |                     |d|z  |d          }
t          d|	t          j                     |z
  z
            }||z  |z  |
z   }t          |          |k    r%|                     | j        j        ||d           dS dS )NF)now   )rn   ro   r   Trq   )	rw   sliding_window_keys_get_sliding_window_infor   rs   minr\   r:   decr)r=   r_   r   rl   rn   r   previous_keycurrent_keyprevious_countprevious_ttlcurrent_count_weighted_countactualised_previous_ttls                 rB   acquire_sliding_window_entryz-MemcachedStorage.acquire_sliding_window_entry   sK    E>>5ikk$($<$<S&#$N$N!k9=9V9V+v3 :W :
 :
6mQ (,6?-O  6)E115
 !IIQZ5 &  M '*!\TY[[3=N-O&P&P#!886AMQ  ^$$u,, ((L% 	 )    u4rC   tuple[int, float, int, float]c                    t          j                     }|                     |||          \  }}|                     ||||          S rF   )rw   r   r   r=   r_   rl   r   r   r   s         rB   get_sliding_windowz#MemcachedStorage.get_sliding_window  sF     ikk$($<$<S&#$N$N!k,,\;PSTTTrC   c                    t          j                     }|                     |||          \  }}|                     |           |                     |           d S rF   )rw   r   rk   r   s         rB   clear_sliding_windowz%MemcachedStorage.clear_sliding_window  sS    ikk$($<$<S&#$N$N!k

<   

;rC   r   r   r   c                8   |                      ||g          }t          |                    |d                    t          |                    |d                    }}|dk    rt          d          }nd||z
  |z  dz  z
  |z  }d||z  dz  z
  |z  |z   }	||||	fS )Nr   rM   )rh   r-   rb   rm   )
r=   r   r   rl   r   resultr   r   r   current_ttls
             rB   r   z)MemcachedStorage._get_sliding_window_info#  s     k:;;

<++,,

;**++ &
 Q 88LL3<6"9Q!>?6ILS6\Q./69FB|]KGGrC   )F)r   r   r   r   r   r   r   r   )r   rD   )r1   r   r(   rJ   rK   r   r   r   )rQ   rR   rS   rT   rK   rU   r   r   )r   r   )r_   r   r   r-   )rd   re   r   rf   )r_   r   r   r   )rM   T)
r_   r   rl   rm   rn   r-   ro   r   r   r-   )r_   r   r   rm   )r_   r   r   r   )r   r   )r   r   )rM   )
r_   r   r   r-   rl   r-   rn   r-   r   r   )r_   r   rl   r-   r   r   )r_   r   rl   r-   r   r   )
r   r   r   r   rl   r-   r   rm   r   r   )__name__
__module____qualname____doc__STORAGE_SCHEMEDEPENDENCIESr<   propertyrI   r6   r\   r:   rb   rh   rk   rs   rz   rv   r~   r   r   r   r   r   __classcell__)rA   s   @rB   r   r      s         "]N* >L
 !&1? 1? 1? 1? 1? 1? 1?f - - - X-

 
 
 
&	% 	% 	% 	% B B B XB$/ / / /+ + + +! ! ! ! #'% % % % %NQ Q Q Q       
 
 
 
" " " " ' ' ' ' 'RU U U U       H H H H H H H HrC   r   )
__future__r   rX   r7   rw   urllib.parser%   collections.abcr   mathr   r   typesr   limits.errorsr   limits.storage.baser	   r
   r   limits.typingr   r   r   r   r   r   limits.utilr   r   r|   rC   rB   <module>r      ss   " " " " " "           $ $ $ $ $ $               , , , , , ,         
                ' & & & & &UH UH UH UH UHw ;=U UH UH UH UH UHrC   