
    5j                     |    S SK Jr  S SKrS SKJrJr  S SKJr  S SKJ	r	  S SK
JrJr  \(       a  S SKr " S S\5      rg)	    )annotationsN)TYPE_CHECKINGcast)RedisBridge)ConfigurationError)AsyncRedisClientCallablec                     \ rS rSr% SS0rS\S'    \S"S j5       r          S#S jrS$S jr	S$S	 jr
S
\S'   S
\S'   S
\S'   S
\S'   S
\S'   S
\S'   S\S'   S%S&S jjrS'S jr S(       S)S jjrS*S jrS+S jrS,S jr        S-S jr        S.S jr S(         S/S jjr S(           S0S jjrS1S jrS2S jrS,S jrS rg!)3RedispyBridge   max_connections  zdict[str, float | str | bool]DEFAULT_CLUSTER_OPTIONSc                0    U R                   R                  4$ N)
dependency
RedisErrorselfs    _/root/lseg-credit/backend/venv/lib/python3.13/site-packages/limits/aio/storage/redis/redispy.pybase_exceptionsRedispyBridge.base_exceptions   s    **,,    c                V  ^ ^ / nUR                  5       nUR                  T R                  R                  5        T R                  R                  (       a&  T R                  R                  R                  SS5      OUnUc  [        S5      eT R                  R                  R                  " U4S0 T R                  EU=(       d    0 E00 T R                  EUED6T l        T R                  R                  U5      T l        T R                  R                  U5      T l        U U4S jT l        g )N/ z'service_name' not providedsentinel_kwargsc                P   > U (       a  T(       a  TR                   $ TR                  $ r   )storage_replicastorage)readonlyr   use_replicass    r   <lambda>,RedispyBridge.use_sentinel.<locals>.<lambda>4   s    $,D  O4<<Or   )copyextendoptions_from_uri	locationspathreplacer   r   asyncioSentinelparsed_authsentinel
master_forr    	slave_forr   connection_getter)r   service_namer"   r   optionssentinel_configurationconnection_optionss   ` `    r   use_sentinelRedispyBridge.use_sentinel   s    "$$\\^%%d&;&;&E&EF $$)) !!&&..sB7 	 $%BCC//88"
Kt//KO4IrK
 9!!8%78

 }}//=#}}66|D"
r   c                  ^  UR                  SS 5      =n(       a.  T R                  R                  R                  " SSU0UD6T l        OT R
                  R                  (       a,  T R                  R                  R                  " S0 UD6T l        O@T R                  R                  R                  R                  " T R                  40 UD6T l        U 4S jT l	        g )Nconnection_poolc                   > TR                   $ r   r    _r   s    r   r#   )RedispyBridge.use_basic.<locals>.<lambda>E   	    4<<r    )
popr   r+   Redisr    r'   emptyfrom_urlurir1   )r   r3   r9   s   `  r   	use_basicRedispyBridge.use_basic8   s    %kk*;TBB?B??2288  /3:DL $$**#66<<GwG#66<<EEHH  '  "8r   c           	     |  ^  / nT R                   R                   HK  u  p4UR                  T R                  R                  R
                  R                  U[        U5      S95        MM     T R                  R                  R                  " S0 0 T R                  ESU0ET R                  EUED6T l        U 4S jT l        g )N)hostportstartup_nodesc                   > TR                   $ r   r;   r<   s    r   r#   +RedispyBridge.use_cluster.<locals>.<lambda>W   r?   r   r@   )r'   r(   appendr   r+   clusterClusterNodeintRedisClusterr   r-   r    r1   )r   r3   cluster_hostsrI   rJ   s   `    r   use_clusterRedispyBridge.use_clusterG   s    //99JD  ''//;;CPTI;V :
 ..;; 
.."M2 "" 	
 "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]r1   c                $    U R                  U5      $ r   )r1   )r   r!   s     r   get_connectionRedispyBridge.get_connectiona   s    %%h//r   c                ,   U R                  5       R                  U R                  5      U l        U R                  5       R                  U R                  5      U l        U R                  5       R                  U R                  5      U l        U R                  5       R                  U R                  5      U l	        U R                  5       R                  U R                  5      U l        U R                  5       R                  U R                  5      U l        g r   )r]   register_scriptSCRIPT_MOVING_WINDOWrV   SCRIPT_ACQUIRE_MOVING_WINDOWrW   SCRIPT_CLEAR_KEYSrZ   SCRIPT_INCR_EXPIREr[   SCRIPT_SLIDING_WINDOWrX   SCRIPT_ACQUIRE_SLIDING_WINDOWrY   r   s    r   register_scriptsRedispyBridge.register_scriptsd   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                   #    U R                  U5      n[        [        U R                  U/X#/5      I Sh  vN 5      $  N7f)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   rQ   r[   )r   keyexpiryamounts       r   incrRedispyBridge.incry   s<      $Ct33SEF;KLLMMLs   3A >
	A c                   #    U R                  U5      n[        U R                  SS9R                  U5      I Sh  vN =(       d    S5      $  N7f)z3

:param key: the key to get the counter value for
T)r!   Nr   )rj   rQ   r]   getr   rk   s     r   rq   RedispyBridge.get   sF      $,,d,;??DDIJJDs   8AAAc                   #    U R                  U5      nU R                  5       R                  U5      I Sh  vN   g N7f)z/
:param key: the key to clear rate limits for

N)rj   r]   deleterr   s     r   clearRedispyBridge.clear   s4     
 $!!#**3///s   4><>c                |   #    [        [        U R                  U R                  S5      /5      I S h  vN 5      $  N7f)N*)r   rQ   rZ   rj   r   s    r   	lua_resetRedispyBridge.lua_reset   s1     Ct22D4E4Ec4J3KLLMMLs   /<:
	<c                   #    U R                  U5      n[        R                  " 5       nU R                  U/XC-
  U/5      I Sh  vN nU(       a  [        US   5      US   4$ US4$  N"7f)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      )rj   timerV   float)r   rk   limitrl   	timestampwindows         r   get_moving_windowRedispyBridge.get_moving_window   sl      $IIK	--sei6H%5PQQ#VAY..!| Rs   AA(A&#A(c           
     |  #    U R                  U R                  U5      U R                  U5      /U/5      I S h  vN =n(       at  [        US   =(       d    S5      [        S[	        US   =(       d    S5      5      S-  [        US   =(       d    S5      [        S[	        US   =(       d    S5      5      S-  4$ g N7f)Nr   r}   r         )r           r   r   )rX   rj   rQ   maxr   )r   previous_keycurrent_keyrl   r   s        r   get_sliding_window RedispyBridge.get_sliding_window   s       22|,d.?.?.LMPVx
 
 
6 
 F1IN#AuVAY^!,-4F1IN#AuVAY^!,-4	  
s   7B<B:BB<c                   #    U R                  U5      n[        R                  " 5       nU R                  U/XRX4/5      I Sh  vN n[        U5      $  N7f)z
:param key: rate limit key to acquire an entry in
:param limit: amount of entries allowed
:param expiry: expiry of the entry

N)rj   r~   rW   bool)r   rk   r   rl   rm   r   acquireds          r   acquire_entryRedispyBridge.acquire_entry   sT      $IIK	77EIf5
 
 H~	
s   ?AAAc                   #    U R                  U5      nU R                  U5      nU R                  X/X4U/5      I S h  vN n[        U5      $  N7fr   )rj   rY   r   )r   r   r   r   rl   rm   r   s          r   acquire_sliding_window_entry*RedispyBridge.acquire_sliding_window_entry   sZ      ((6''488'%)@
 
 H~
s   ;AAAc                   #    U R                  U5      n[        U R                  5       R                  U5      I Sh  vN S5      [        R                  " 5       -   $  N7f)z+
:param key: the key to get the expiry for
Nr   )rj   r   r]   ttlr~   rr   s     r   
get_expiryRedispyBridge.get_expiry   sI     
 $,,.22377;diikII7s   9AA Ac                l   #     U R                  5       R                  5       I Sh  vN   g N!    g= f7f)z
check if storage is healthy
NTF)r]   pingr   s    r   checkRedispyBridge.check   s6     	%%',,... /	s    4!- +- 4- 14c                >  #    U R                  S5      nU R                  R                  XR                  R                  R
                  R                  R                  S9I S h  vN nSnU H(  nX0R                  R                  U5      I S h  vN -  nM*     U$  N6 N7f)Nry   )target_nodesr   )	rj   r    keysr   r+   rO   rR   	ALL_NODESru   )r   prefixr   countrk   s        r   resetRedispyBridge.reset   s     ""3'\\&&!8!8!@!@!M!M!W!W ' 
 
 C<<..s333E 

 4s$   A B"B#)BBBB)
r1   rW   rY   rZ   r[   rV   rX   r.   r    r   N)returnz-type[Exception] | tuple[type[Exception], ...])
r2   z
str | Noner"   r   r   z$dict[str, str | float | bool] | Noner3   str | float | boolr   None)r3   r   r   r   )F)r!   r   r   r   )r   r   )r}   )rk   strrl   rQ   rm   rQ   r   rQ   )rk   r   r   rQ   )rk   r   r   r   )r   z
int | None)rk   r   r   rQ   rl   rQ   r   ztuple[float, int])r   r   r   r   rl   rQ   r   ztuple[int, float, int, float])
rk   r   r   rQ   rl   rQ   rm   rQ   r   r   )r   r   r   r   r   rQ   rl   rQ   rm   rQ   r   r   )rk   r   r   r   )r   r   )__name__
__module____qualname____firstlineno__r   __annotations__propertyr   r6   rF   rT   r]   rg   rn   rq   rv   rz   r   r   r   r   r   r   r   __static_attributes__r@   r   r   r   r      s   4>:  D- -
 
 
 >	

 &
 

@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   r~   typingr   r   limits.aio.storage.redis.bridger   limits.errorsr   limits.typingr   r	   redis.commandsredisr   r@   r   r   <module>r      s+    "  & 7 , 4oK or   