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   ú]/root/parts/websockify/install/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    