
    <i                         d dl Z d dlZd dlmZmZ d dlmZmZ d dlmZm	Z	 d dl
mZ d dlmZ dZdZd	Z G d
 de          Z G d de          ZdS )    N)ABCabstractmethod)datetime	timedelta)ListType)Optional)Statei  g?   c                   j    e Zd Zedededdfd            Zededdfd            Zedd            ZdS )	FailureDetector	exceptioncmdreturnNc                     dS )z:Register a failure that occurred during command execution.N selfr   r   s      /Users/kimhansen/Desktop/03 Workspace/ceo-agents/chl-effectiveness/mcp-servers/whoop/.venv/lib/python3.11/site-packages/redis/multidb/failure_detector.pyregister_failurez FailureDetector.register_failure   	     	    c                     dS )zRegister a command execution.Nr   r   r   s     r   register_command_executionz*FailureDetector.register_command_execution   r   r   c                     dS )z*Set the command executor for this failure.Nr   r   command_executors     r   set_command_executorz$FailureDetector.set_command_executor   r   r   r   N)	__name__
__module____qualname__r   	Exceptiontupler   r   r   r   r   r   r   r      s        ) % D    ^ e     ^    ^  r   r   c                       e Zd ZdZeeedfdededede	e
ee                           ddf
dZd	ed
eddfdZddZd
eddfdZd ZddZdS )CommandFailureDetectorze
    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                    d| _         || _        || _        || _        || _        d| _        t          j                    | _        | j        t          | j                  z   | _
        d| _        t          j                    | _        dS )az  
        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(   r)   r*   r+   s        r   __init__zCommandFailureDetector.__init__&   s    & "&!1'=$)A&''(%-\^^#'#3i27
 7
 7
 $
 %&_&&


r   r   r   c                     | j         5  | j        r't          |          | j        v r| xj        dz  c_        n| xj        dz  c_        |                                  d d d            d S # 1 swxY w Y   d S N   )r;   r3   typer8   _check_thresholdr   s      r   r   z'CommandFailureDetector.register_failureF   s    Z 	$ 	$  *	??d&777((A-(($$)$$!!###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   AA((A,/A,c                     || _         d S N)r/   r   s     r   r   z+CommandFailureDetector.set_command_executorP   s    !1r   c                     | j         5  | j        t          j                    cxk     r| j        k     sn |                                  | xj        dz  c_        d d d            d S # 1 swxY w Y   d S r>   )r;   r6   r   r5   r7   _resetr4   r   s     r   r   z1CommandFailureDetector.register_command_executionS   s    Z 	) 	)#hlnnEEEEt~EEEE##q(##		) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)s   AA$$A(+A(c                     | j         | j        k    r`| j         t          j        | j        | j        z            k    r8t          j        | j        j	        j
        _        |                                  d S d S d S rC   )r8   r0   mathceilr4   r1   CBStateOPENr/   active_databasecircuitstaterE   r   s    r   rA   z'CommandFailureDetector._check_thresholdZ   st    4#999d>RId-0LLMM?
 ?
 DK<D"2:@KKMMMMM	 :9 ?
 ?
r   c                     | j         5  t          j                    | _        | j        t	          | j                  z   | _        d| _        d| _        d d d            d S # 1 swxY w Y   d S )Nr-   r   )	r;   r   r5   r6   r   r2   r7   r8   r4   rN   s    r   rE   zCommandFailureDetector._reseta   s    Z 	( 	('|~~D!-	61 1 1 DN $%D &'D#	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   A	AA"%A"r    )r!   r"   r#   __doc__DEFAULT_MIN_NUM_FAILURESDEFAULT_FAILURE_RATE_THRESHOLD!DEFAULT_FAILURES_DETECTION_WINDOWintfloatr	   r   r   r$   r<   r%   r   r   r   rA   rE   r   r   r   r'   r'   !   s         !9(F*K7;' '' !&' #(	'
 d4	?34' 
' ' ' '@$) $% $D $ $ $ $2 2 2 2)e ) ) ) ) )  ( ( ( ( ( (r   r'   )rG   r9   abcr   r   r   r   typingr   r   typing_extensionsr	   redis.multidb.circuitr
   rI   rQ   rR   rS   r   r'   r   r   r   <module>rZ      s         # # # # # # # # ( ( ( ( ( ( ( (         & & & & & & 2 2 2 2 2 2 !$ $% !    c   "G( G( G( G( G(_ G( G( G( G( G(r   