
    ZeIiv                     z    d dl m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
mZmZ erd dlZ G d de          ZdS )	    )annotationsN)TYPE_CHECKINGcast)RedisBridge)ConfigurationError)AsyncRedisClientCallablec                     e Zd ZU ddiZded<   	 ed;d            Zd<dZd=dZd=dZ	ded<   ded<   ded<   ded<   ded<   ded<   ded<   d>d?d Z
d@d!Z	 dAdBd(ZdCd)ZdDd*ZdEd,ZdFd/ZdGd3Z	 dAdHd4Z	 dAdId5ZdJd7ZdKd8ZdEd9Zd:S )LRedispyBridgemax_connections  zdict[str, float | str | bool]DEFAULT_CLUSTER_OPTIONSreturn-type[Exception] | tuple[type[Exception], ...]c                    | j         j        fS N)
dependency
RedisErrorselfs    e/var/www/html/MCyber-Diagnostic/venv/lib/python3.11/site-packages/limits/aio/storage/redis/redispy.pybase_exceptionszRedispyBridge.base_exceptions   s    *,,    service_name
str | Noneuse_replicasboolsentinel_kwargs$dict[str, str | float | bool] | Noneoptionsstr | float | boolNonec                    g }|                                 } j        j                            d          dz   } j        j        |d                              d          D ]>}|                    d          \  }	}
|                    |	t          |
          f           ? j        j        r  j        j                            dd          n|}|t          d            j
        j        j        |fdi  j        |pi ii  j        | _         j                            |           _         j                            |           _         fd	 _        d S )
N@   ,:/ z'service_name' not providedr   c                (    | r	rj         nj        S r   )storage_replicastorage)readonlyr   r   s    r   <lambda>z,RedispyBridge.use_sentinel.<locals>.<lambda>8   s    $,OOD  4< r   )copy
parsed_urinetlocfindsplitappendintpathreplacer   r   asyncioSentinelparsed_authsentinel
master_forr,   	slave_forr+   connection_getter)r   r   r   r   r    sentinel_configurationconnection_optionsseplochostports   ` `        r   use_sentinelzRedispyBridge.use_sentinel   s    "$$\\^^o$))#..2?)#$$/55c:: 	= 	=C3JD$"))4T*;<<<< #DO ((b111 	 $%BCCC8/8"
 
Kt/KO4IrK
 9!8%78
 

 }//==#}66|DD"
 "
 "
 "
 "
r   c                     |                     dd           x}r  j        j        j        dd|i| _        n'  j        j        j        j         j        fi | _         fd _        d S )Nconnection_poolc                    j         S r   r,   _r   s    r   r.   z)RedispyBridge.use_basic.<locals>.<lambda>D   	    4< r    )popr   r8   Redisr,   from_urlurir>   )r   r    rG   s   `  r   	use_basiczRedispyBridge.use_basic<   s    %kk*;TBBB? 	W84?28   /3: DLL B4?28A$(VVgVVDL!7!7!7!7r   c           	          j         j                            d          dz   }g } j         j        |d                              d          D ]`}|                    d          \  }}|                     j        j        j                            |t          |                               a  j        j        j
        dd|ii  j         j        | _         fd _        d S )	Nr$   r%   r&   r'   )rC   rD   startup_nodesc                    j         S r   rI   rJ   s    r   r.   z+RedispyBridge.use_cluster.<locals>.<lambda>T   rL   r   rM   )r0   r1   r2   r3   r4   r   r8   clusterClusterNoder5   RedisClusterr   r:   r,   r>   )r   r    rA   cluster_hostsrB   rC   rD   s   `      r   use_clusterzRedispyBridge.use_clusterF   s   o$))#..2?)#$$/55c:: 	 	C3JD$  '/;;CPTII;VV    <t.; 
 
'
M-M1AMWM
 
 "8!7!7!7r   zredis.commands.core.Scriptlua_moving_windowlua_acquire_moving_windowlua_sliding_windowlua_acquire_sliding_windowlua_clear_keyslua_incr_expirez"Callable[[bool], AsyncRedisClient]r>   Fr-   r   c                ,    |                      |          S r   )r>   )r   r-   s     r   get_connectionzRedispyBridge.get_connection^   s    %%h///r   c                R   |                                                      | j                  | _        |                                                      | j                  | _        |                                                      | j                  | _        |                                                      | j                  | _	        |                                                      | j
                  | _        |                                                      | j                  | _        d S r   )rb   register_scriptSCRIPT_MOVING_WINDOWr[   SCRIPT_ACQUIRE_MOVING_WINDOWr\   SCRIPT_CLEAR_KEYSr_   SCRIPT_INCR_EXPIREr`   SCRIPT_SLIDING_WINDOWr]   SCRIPT_ACQUIRE_SLIDING_WINDOWr^   r   s    r   register_scriptszRedispyBridge.register_scriptsa   s   !%!4!4!6!6!F!F%"
 "
 *.)<)<)>)>)N)N-*
 *
& #1133CC"
 
  $2244DD# 
  
 #'"5"5"7"7"G"G&#
 #
 +/*=*=*?*?*O*O.+
 +
'''r   r%   keystrexpiryr5   amountc                   K   |                      |          }t          t          |                     |g||g           d{V           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)prefixed_keyr   r5   r`   )r   rl   rn   ro   s       r   incrzRedispyBridge.incrv   sT       $$Ct33SEFF;KLLLLLLLLMMMr   c                   K   |                      |          }t          |                     d                              |           d{V pd          S )zC

        :param key: the key to get the counter value for
        T)r-   Nr   )rq   r5   rb   getr   rl   s     r   rt   zRedispyBridge.get   s_       $$,,d,;;??DDDDDDDDIJJJr   c                   K   |                      |          }|                                                     |           d{V  dS )z?
        :param key: the key to clear rate limits for

        N)rq   rb   deleteru   s     r   clearzRedispyBridge.clear   sS      
 $$!!##**3///////////r   
int | Nonec                   K   t          t          |                     |                     d          g           d {V           S )N*)r   r5   r_   rq   r   s    r   	lua_resetzRedispyBridge.lua_reset   sG      Ct22D4E4Ec4J4J3KLLLLLLLLMMMr   limittuple[float, int]c                   K   |                      |          }t          j                    }|                     |g||z
  |g           d{V }|rt          |d                   |d         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: (previous count, previous TTL, current count, current TTL)
        Nr   r%   )rq   timer[   float)r   rl   r}   rn   	timestampwindows         r   get_moving_windowzRedispyBridge.get_moving_window   s       $$IKK	--sei&6H%5PQQQQQQQQ 	/##VAY..!|r   previous_keycurrent_keytuple[int, float, int, float]c           
       K   |                      |                     |          |                     |          g|g           d {V x}r|t          |d         pd          t          dt	          |d         pd                    dz  t          |d         pd          t          dt	          |d         pd                    dz  fS dS )Nr   r%   r         )r           r   r   )r]   rq   r5   maxr   )r   r   r   rn   r   s        r   get_sliding_windowz RedispyBridge.get_sliding_window   s        22|,,d.?.?.L.LMPVx
 
 
 
 
 
 
 
 
6 	 F1IN##AuVAY^!,,--4F1IN##AuVAY^!,,--4	  ~r   c                   K   |                      |          }t          j                    }|                     |g||||g           d{V }t          |          S )z
        :param key: rate limit key to acquire an entry in
        :param limit: amount of entries allowed
        :param expiry: expiry of the entry

        N)rq   r   r\   r   )r   rl   r}   rn   ro   r   acquireds          r   acquire_entryzRedispyBridge.acquire_entry   sw       $$IKK	77EIuff5
 
 
 
 
 
 
 
 H~~r   c                   K   |                      |          }|                      |          }|                     ||g|||g           d {V }t          |          S r   )rq   r^   r   )r   r   r   r}   rn   ro   r   s          r   acquire_sliding_window_entryz*RedispyBridge.acquire_sliding_window_entry   s       ((66''4488;'%)@
 
 
 
 
 
 
 
 H~~r   r   c                   K   |                      |          }t          |                                                     |           d{V d          t	          j                    z   S )z;
        :param key: the key to get the expiry for
        Nr   )rq   r   rb   ttlr   ru   s     r   
get_expiryzRedispyBridge.get_expiry   sb      
 $$,,..22377777777;;dikkIIr   c                t   K   	 |                                                                   d{V  dS #  Y dS xY w)z-
        check if storage is healthy
        NTF)rb   pingr   s    r   checkzRedispyBridge.check   sP      	%%'',,.........4	55s   ,2 7c                   K   |                      d          }| j                            || j        j        j        j        j                   d {V }d}|D ]%}|| j                            |           d {V z  }&|S )Nr{   )target_nodesr   )	rq   r,   keysr   r8   rV   rX   	ALL_NODESrw   )r   prefixr   countrl   s        r   resetzRedispyBridge.reset   s      ""3''\&&!8!@!M!W ' 
 
 
 
 
 
 
 
  	4 	4C4<..s333333333EEr   N)r   r   )
r   r   r   r   r   r   r    r!   r   r"   )r    r!   r   r"   )F)r-   r   r   r   )r   r"   )r%   )rl   rm   rn   r5   ro   r5   r   r5   )rl   rm   r   r5   )rl   rm   r   r"   )r   ry   )rl   rm   r}   r5   rn   r5   r   r~   )r   rm   r   rm   rn   r5   r   r   )
rl   rm   r}   r5   rn   r5   ro   r5   r   r   )r   rm   r   rm   r}   r5   rn   r5   ro   r5   r   r   )rl   rm   r   r   )r   r   )__name__
__module____qualname__r   __annotations__propertyr   rE   rR   rZ   rb   rk   rr   rt   rx   r|   r   r   r   r   r   r   r   rM   r   r   r   r      s&        4>     D- - - X-"
 "
 "
 "
H8 8 8 88 8 8 8  211199992222::::....////99990 0 0 0 0
 
 
 
2 	N N N N N"K K K K0 0 0 0N N N N   $   &     6     J J J J	 	 	 	     r   r   )
__future__r   r   typingr   r   limits.aio.storage.redis.bridger   limits.errorsr   limits.typingr   r	   redis.commandsredisr   rM   r   r   <module>r      s    " " " " " "  & & & & & & & & 7 7 7 7 7 7 , , , , , , 4 4 4 4 4 4 4 4 l l l l lK l l l l lr   