o
    i                     @   s   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	m
Z
 d dlmZ dZdZdZG d	d
 d
eZG dd deZdS )    N)ABCabstractmethod)datetime	timedelta)ListOptionalType)Statei  g?   c                   @   sJ   e Zd ZedededdfddZededdfddZedd	d
ZdS )FailureDetector	exceptioncmdreturnNc                 C      dS )z:Register a failure that occurred during command execution.N selfr   r   r   r   R/home/ubuntu/.local/lib/python3.10/site-packages/redis/multidb/failure_detector.pyregister_failure      z FailureDetector.register_failurec                 C   r   )zRegister a command execution.Nr   r   r   r   r   r   register_command_execution   r   z*FailureDetector.register_command_executionc                 C   r   )z*Set the command executor for this failure.Nr   r   command_executorr   r   r   set_command_executor   r   z$FailureDetector.set_command_executorr   N)	__name__
__module____qualname__r   	Exceptiontupler   r   r   r   r   r   r   r      s    r   c                   @   s   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d	Zd
ededdfddZdddZdeddfddZdd Zd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                 C   sV   d| _ || _|| _|| _|| _d| _t | _| jt	| jd | _
d| _t | _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%   r   r   r   __init__$   s   

zCommandFailureDetector.__init__r   r   c                 C   sf   | j & | jrt|| jv r|  jd7  _n|  jd7  _|   W d    d S 1 s,w   Y  d S N   )r4   r,   typer1   _check_thresholdr   r   r   r   r   D   s   
"z'CommandFailureDetector.register_failurec                 C   s
   || _ d S N)r(   r   r   r   r   r   N   s   
z+CommandFailureDetector.set_command_executorc                 C   s`   | j # | jt   k r| jk sn |   |  jd7  _W d    d S 1 s)w   Y  d S r6   )r4   r/   r   r.   r0   _resetr-   r   r   r   r   r   Q   s
   "z1CommandFailureDetector.register_command_executionc                 C   sF   | j | jkr| j t| j| j kr!tj| jj	j
_|   d S d S d S r:   )r1   r)   mathceilr-   r*   CBStateOPENr(   active_databasecircuitstater;   r   r   r   r   r9   X   s   z'CommandFailureDetector._check_thresholdc                 C   sV   | j  t | _| jt| jd | _d| _d| _W d    d S 1 s$w   Y  d S )Nr&   r   )	r4   r   r.   r/   r   r+   r0   r1   r-   rC   r   r   r   r;   _   s   

"zCommandFailureDetector._resetr   )r   r   r   __doc__DEFAULT_MIN_NUM_FAILURESDEFAULT_FAILURE_RATE_THRESHOLD!DEFAULT_FAILURES_DETECTION_WINDOWintfloatr   r   r   r   r5   r    r   r   r   r9   r;   r   r   r   r   r!      s,    
 

r!   )r<   r2   abcr   r   r   r   typingr   r   r   redis.multidb.circuitr	   r>   rE   rF   rG   r   r!   r   r   r   r   <module>   s    