
    9iv                     r    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y)	    )annotationsN)TYPE_CHECKINGcast)RedisBridge)ConfigurationError)AsyncRedisClientCallablec                  r   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	 d&	 	 	 	 	 	 	 d'dZd(dZd)dZd*dZ	 	 	 	 	 	 	 	 d+dZ	 	 	 	 	 	 	 	 d,dZ	 d&	 	 	 	 	 	 	 	 	 d-dZ	 d&	 	 	 	 	 	 	 	 	 	 	 d.dZd/dZd0dZd*dZy)1RedispyBridgemax_connections  zdict[str, float | str | bool]DEFAULT_CLUSTER_OPTIONSc                0    | j                   j                  fS N)
dependency
RedisErrorselfs    S/var/www/html/venv/lib/python3.12/site-packages/limits/aio/storage/redis/redispy.pybase_exceptionszRedispyBridge.base_exceptions   s    **,,    c                    g }|j                         } j                  j                  j                  d      dz   } j                  j                  |d  j	                  d      D ]2  }|j	                  d      \  }	}
|j                  |	t        |
      f       4  j                  j                  r& j                  j                  j                  dd      n|}|t        d        j                  j                  j                  |fdi  j                  |xs i ii  j                  | _         j                  j                  |       _         j                  j#                  |       _         fd	 _        y )
N@   ,:/ z'service_name' not providedsentinel_kwargsc                <    | rrj                   S j                  S r   )storage_replicastorage)readonlyr   use_replicass    r   <lambda>z,RedispyBridge.use_sentinel.<locals>.<lambda>8   s!    $,D   CG<< r   )copy
parsed_urinetlocfindsplitappendintpathreplacer   r   asyncioSentinelparsed_authsentinel
master_forr"   	slave_forr!   connection_getter)r   service_namer$   r   optionssentinel_configurationconnection_optionsseplochostports   ` `        r   use_sentinelzRedispyBridge.use_sentinel   sb    "$$\\^oo$$))#.2??))#$/55c: 	=C3JD$"))4T*;<	=
 ## OO  ((b1 	 $%BCC8//88"
Kt//KO4IrK
 9!!8%78

 }}//=#}}66|D"
r   c                    |j                  dd       x}r.  j                  j                  j                  dd|i| _        n@  j                  j                  j                  j
                   j                  fi | _         fd _        y )Nconnection_poolc                    j                   S r   r"   _r   s    r   r%   z)RedispyBridge.use_basic.<locals>.<lambda>D       4<< r    )popr   r/   Redisr"   from_urlurir5   )r   r7   r@   s   `  r   	use_basiczRedispyBridge.use_basic<   s    %kk*;TBB?B84??2288  /3:DL B4??2288AA$((VgVDL!7r   c           	          j                   j                  j                  d      dz   }g } j                   j                  |d  j                  d      D ]_  }|j                  d      \  }}|j	                   j
                  j                  j                  j                  |t        |                   a   j
                  j                  j                  dd|ii  j                   j                  | _         fd _        y )	Nr   r   r   r   )r<   r=   startup_nodesc                    j                   S r   rB   rC   s    r   r%   z+RedispyBridge.use_cluster.<locals>.<lambda>T   rE   r   rF   )r'   r(   r)   r*   r+   r   r/   clusterClusterNoder,   RedisClusterr   r1   r"   r5   )r   r7   r:   cluster_hostsr;   r<   r=   s   `      r   use_clusterzRedispyBridge.use_clusterF   s    oo$$))#.2??))#$/55c: 	C3JD$  ''//;;CPTI;V	 <t..;; 
'
M--M1A1AMWM
 "8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]r5   c                $    | j                  |      S r   )r5   )r   r#   s     r   get_connectionzRedispyBridge.get_connection^   s    %%h//r   c                ,   | j                         j                  | j                        | _        | j                         j                  | j                        | _        | j                         j                  | j                        | _        | j                         j                  | j                        | _	        | j                         j                  | j                        | _        | j                         j                  | j                        | _        y r   )r[   register_scriptSCRIPT_MOVING_WINDOWrT   SCRIPT_ACQUIRE_MOVING_WINDOWrU   SCRIPT_CLEAR_KEYSrX   SCRIPT_INCR_EXPIRErY   SCRIPT_SLIDING_WINDOWrV   SCRIPT_ACQUIRE_SLIDING_WINDOWrW   r   s    r   register_scriptszRedispyBridge.register_scriptsa   s    !%!4!4!6!F!F%%"
 *.)<)<)>)N)N--*
& #113CC""
  $224DD## 
 #'"5"5"7"G"G&&#
 +/*=*=*?*O*O..+
'r   c                   K   | j                  |      }t        t        | j                  |g||g       d{         S 7 w)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   r,   rY   )r   keyexpiryamounts       r   incrzRedispyBridge.incrv   s?      $Ct33SEFF;KLLMMLs   4A?
	Ac                   K   | j                  |      }t        | j                  d      j                  |       d{   xs d      S 7 w)zC

        :param key: the key to get the counter value for
        T)r#   Nr   )rf   r,   r[   getr   rg   s     r   rl   zRedispyBridge.get   sG      $,,d,;??DDIJJDs   ;AA
Ac                   K   | j                  |      }| j                         j                  |       d{    y7 w)z?
        :param key: the key to clear rate limits for

        N)rf   r[   deleterm   s     r   clearzRedispyBridge.clear   s5     
 $!!#**3///s   4><>c                |   K   t        t        | j                  | j                  d      g       d {         S 7 w)N*)r   r,   rX   rf   r   s    r   	lua_resetzRedispyBridge.lua_reset   s2     Ct22D4E4Ec4J3KLLMMLs   /<:
	<c                   K   | j                  |      }t        j                         }| j                  |g||z
  |g       d{   }|rt        |d         |d   fS |dfS 7 w)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   )rf   timerT   float)r   rg   limitrh   	timestampwindows         r   get_moving_windowzRedispyBridge.get_moving_window   so      $IIK	--sei&6H%5PQQ#VAY..!| Rs   AA#A!A#c           
     J  K   | j                  | j                  |      | j                  |      g|g       d {   x}r`t        |d   xs d      t        dt	        |d   xs d            dz  t        |d   xs d      t        dt	        |d   xs d            dz  fS y7 gw)Nr   r   r         )r           r   r~   )rV   rf   r,   maxrv   )r   previous_keycurrent_keyrh   ry   s        r   get_sliding_windowz RedispyBridge.get_sliding_window   s       22|,d.?.?.LMPVx
 
 
6 
 F1IN#AuVAY^!,-4F1IN#AuVAY^!,-4	  
s   7B#B!A(B#c                   K   | j                  |      }t        j                         }| j                  |g||||g       d{   }t        |      S 7 w)z
        :param key: rate limit key to acquire an entry in
        :param limit: amount of entries allowed
        :param expiry: expiry of the entry

        N)rf   ru   rU   bool)r   rg   rw   rh   ri   rx   acquireds          r   acquire_entryzRedispyBridge.acquire_entry   sY      $IIK	77EIuff5
 
 H~	
s   A AAAc                   K   | j                  |      }| j                  |      }| j                  ||g|||g       d {   }t        |      S 7 wr   )rf   rW   r   )r   r   r   rw   rh   ri   r   s          r   acquire_sliding_window_entryz*RedispyBridge.acquire_sliding_window_entry   s_      ((6''488;'%)@
 
 H~
s   =AA Ac                   K   | j                  |      }t        | j                         j                  |       d{   d      t	        j                         z   S 7 w)z;
        :param key: the key to get the expiry for
        Nr   )rf   r   r[   ttlru   rm   s     r   
get_expiryzRedispyBridge.get_expiry   sJ     
 $,,.22377;diikII7s   9AAAc                l   K   	 | j                         j                          d{    y7 #  Y yxY ww)z-
        check if storage is healthy
        NTF)r[   pingr   s    r   checkzRedispyBridge.check   s7     	%%',,... /	s    4!- +- 4- 14c                D  K   | j                  d      }| j                  j                  || j                  j                  j
                  j                  j                         d {   }d}|D ](  }|| j                  j                  |       d {   z  }* |S 7 57 w)Nrr   )target_nodesr   )	rf   r"   keysr   r/   rO   rQ   	ALL_NODESro   )r   prefixr   countrg   s        r   resetzRedispyBridge.reset   s     ""3'\\&&!8!8!@!@!M!M!W!W ' 
 
  	4C4<<..s333E	4

 4s$   A$B &B'*B BB B N)returnz-type[Exception] | tuple[type[Exception], ...])
r6   z
str | Noner$   r   r   z$dict[str, str | float | bool] | Noner7   str | float | boolr   None)r7   r   r   r   )F)r#   r   r   r   )r   r   )r   )rg   strrh   r,   ri   r,   r   r,   )rg   r   r   r,   )rg   r   r   r   )r   z
int | None)rg   r   rw   r,   rh   r,   r   ztuple[float, int])r   r   r   r   rh   r,   r   ztuple[int, float, int, float])
rg   r   rw   r,   rh   r,   ri   r,   r   r   )r   r   r   r   rw   r,   rh   r,   ri   r,   r   r   )rg   r   r   rv   )r   r   )__name__
__module____qualname__r   __annotations__propertyr   r>   rK   rS   r[   rd   rj   rl   rp   rs   rz   r   r   r   r   r   r   rF   r   r   r   r      s   4>:  D- -"
 "
 "
 >	"

 &"
 
"
H88  219922 ::..//990
2 	NN N 	N
 
N"K0N",/	$.1;>	&&   	
  
6   	
   
J	r   r   )
__future__r   ru   typingr   r   limits.aio.storage.redis.bridger   limits.errorsr   limits.typingr   r	   redis.commandsredisr   rF   r   r   <module>r      s+    "  & 7 , 4lK lr   