
    ph                         S SK r S SKrS SKJrJr  S SKJrJr  S SKJrJ	r	  S SK
Jr  S SKJr  SrSrS	r " S
 S\5      r " S S\5      rg)    N)ABCabstractmethod)datetime	timedelta)ListType)Optional)Statei  g?   c                   d    \ rS rSr\S\S\SS4S j5       r\S\SS4S j5       r\S
S j5       r	S	r
g)FailureDetector   	exceptioncmdreturnNc                     g)z:Register a failure that occurred during command execution.N selfr   r   s      X/home/james-whalen/.local/lib/python3.13/site-packages/redis/multidb/failure_detector.pyregister_failure FailureDetector.register_failure        	    c                     g)zRegister a command execution.Nr   r   r   s     r   register_command_execution*FailureDetector.register_command_execution   r   r   c                     g)z*Set the command executor for this failure.Nr   r   command_executors     r   set_command_executor$FailureDetector.set_command_executor   r   r   r   r   N)__name__
__module____qualname____firstlineno__r   	Exceptiontupler   r   r"   __static_attributes__r   r   r   r   r      s_    ) % D   e     r   r   c                       \ rS rSrSr\\\S4S\S\	S\	S\
\\\         SS4
S	 jjrS
\S\SS4S jrSS jrS\SS4S jrS rSS jrSrg)CommandFailureDetector!   z]
Detects a failure based on a threshold of failed commands during a specific period of time.
Nmin_num_failuresfailure_rate_thresholdfailure_detection_windowerror_typesr   c                    SU l         Xl        X l        X0l        X@l        SU l        [        R                  " 5       U l        U R                  [        U R                  S9-   U l
        SU l        [        R                  " 5       U l        g)a2  
Initialize a new CommandFailureDetector instance.

Args:
    min_num_failures: Minimal count of failures required for failover
    failure_rate_threshold: Percentage of failures required for failover
    failure_detection_window: Time interval for executing health checks.
    error_types: Optional list of exception types to trigger failover. If None, all exceptions are counted.

The detector tracks command failures within a sliding time window. When the number of failures
exceeds the threshold within the specified duration, it triggers failure detection.
Nr   seconds)_command_executor_min_num_failures_failure_rate_threshold_failure_detection_window_error_types_commands_executedr   now_start_timer   	_end_time_failures_count	threadingRLock_lock)r   r/   r0   r1   r2   s        r   __init__CommandFailureDetector.__init__&   st    & "&!1'=$)A&''(%-\\^#'#3#3i227
 $
 %&__&
r   r   r   c                    U R                      U R                  (       a/  [        U5      U R                  ;   a  U =R                  S-  sl        OU =R                  S-  sl        U R	                  5         S S S 5        g ! , (       d  f       g = fN   )rB   r:   typer?   _check_thresholdr   s      r   r   'CommandFailureDetector.register_failureF   s]    ZZ  	?d&7&77((A-($$)$!!# ZZs   A&A<<
B
c                     Xl         g N)r6   r    s     r   r"   +CommandFailureDetector.set_command_executorP   s    !1r   c                    U R                      U R                  [        R                  " 5       s=:  a  U R                  :  d  O  U R                  5         U =R                  S-  sl        S S S 5        g ! , (       d  f       g = frF   )rB   r=   r   r<   r>   _resetr;   r   s     r   r   1CommandFailureDetector.register_command_executionS   sJ    ZZ##hllnEt~~E##q(#	 ZZs   AA22
B c                 8   U R                   U R                  :  a  U R                   [        R                  " U R                  U R
                  -  5      :  aD  [        R                  U R                  R                  R                  l        U R                  5         g g g rL   )r?   r7   mathceilr;   r8   CBStateOPENr6   active_databasecircuitstaterO   r   s    r   rI   'CommandFailureDetector._check_thresholdZ   ss    4#9#99d>R>RIId--0L0LLM?
 DK<<D""22::@KKM	?
9r   c                     U R                      [        R                  " 5       U l        U R                  [	        U R
                  S9-   U l        SU l        SU l        S S S 5        g ! , (       d  f       g = f)Nr4   r   )	rB   r   r<   r=   r   r9   r>   r?   r;   rY   s    r   rO   CommandFailureDetector._reseta   sU    ZZ'||~D!--	661 DN $%D &'D# ZZs   AA$$
A2)
r6   r;   r>   r:   r9   r8   r?   rB   r7   r=   r$   )r%   r&   r'   r(   __doc__DEFAULT_MIN_NUM_FAILURESDEFAULT_FAILURE_RATE_THRESHOLD!DEFAULT_FAILURES_DETECTION_WINDOWintfloatr	   r   r   r)   rC   r*   r   r"   r   rI   rO   r+   r   r   r   r-   r-   !   s     !9(F*K7;'' !&' #(	'
 d4	?34' 
'@$) $% $D $2)e ) )(r   r-   )rR   r@   abcr   r   r   r   typingr   r   typing_extensionsr	   redis.multidb.circuitr
   rT   r^   r_   r`   r   r-   r   r   r   <module>rg      sF      # (  & 2 !$ $% !c "G(_ G(r   