
    ,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y)    N)ABCabstractmethod)datetime	timedelta)ListType)Optional)Statei  g?   c                   V    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y)	FailureDetector	exceptioncmdreturnNc                      y)z:Register a failure that occurred during command execution.N selfr   r   s      c/var/www/html/langgraph-service/venv/lib/python3.12/site-packages/redis/multidb/failure_detector.pyregister_failurez FailureDetector.register_failure        	    c                      y)zRegister a command execution.Nr   r   r   s     r   register_command_executionz*FailureDetector.register_command_execution   r   r   c                      y)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y)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                         | _        y)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&''(%-\\^#'#3#3i227
 $
 %&__&
r   r   r   c                    | j                   5  | j                  r-t        |      | j                  v r+| xj                  dz  c_        n| xj                  dz  c_        | j	                          d d d        y # 1 sw Y   y xY wN   )r;   r3   typer8   _check_thresholdr   s      r   r   z'CommandFailureDetector.register_failureF   sg    ZZ 	$  	?d&7&77((A-($$)$!!#	$ 	$ 	$s   AA55A>c                     || _         y 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 | j                          | xj                  dz  c_        d d d        y # 1 sw Y   y xY wr>   )r;   r6   r   r5   r7   _resetr4   r   s     r   r   z1CommandFailureDetector.register_command_executionS   sU    ZZ 	)##hllnEt~~E##q(#		) 	) 	)s   AA//A8c                 2   | j                   | j                  k\  r~| j                   t        j                  | j                  | j
                  z        k\  rDt        j                  | j                  j                  j                  _        | j                          y y y rC   )r8   r0   mathceilr4   r1   CBStateOPENr/   active_databasecircuitstaterE   r   s    r   rA   z'CommandFailureDetector._check_thresholdZ   ss    4#9#99d>R>RIId--0L0LLM?
 DK<<D""22::@KKM	?
9r   c                     | j                   5  t        j                         | _        | j                  t	        | j
                        z   | _        d| _        d| _        d d d        y # 1 sw Y   y xY w)Nr-   r   )	r;   r   r5   r6   r   r2   r7   r8   r4   rN   s    r   rE   zCommandFailureDetector._reseta   s`    ZZ 	('||~D!--	661 DN $%D &'D#	( 	( 	(s   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)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      sF      # (  & 2 !$ $% !c "G(_ G(r   