Ë
    §ŒjÌ  ã                   óŒ   — 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
e«      Z G d„ de«      Zy)é    N)ÚABCÚabstractmethod)ÚdatetimeÚ	timedelta)ÚListÚOptionalÚType)Ú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      úh/var/www/html/Fitness-lenito-AI-main/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Ñ#3´iØ×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_failureD   s\   € Ø�Z‹ZØ× Ò Ü˜	“? d×&7Ñ&7Ñ7Ø×(Ò(¨AÑ-Ö(à×$Ò$¨Ñ)Õ$à×!Ñ!Ô#÷ �Z‰Zús   �AA5Á5A>c                 ó   — || _         y ©N)r/   r   s     r   r   z+CommandFailureDetector.set_command_executorN   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_executionQ   sJ   € Ø�Z‹ZØ×#Ñ#¤h§l¡l£nÔE°t·~±~ÔEØ—‘”à×#Ò# qÑ(Õ#÷	 �Z‰Zú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_thresholdX   ss   € Ø×Ñ 4×#9Ñ#9Ò9¸d×>RÑ>RÜ�I‰I�d×-Ñ-°×0LÑ0LÑLÓMò?
ô DKÇ<Á<ˆD×"Ñ"×2Ñ2×:Ñ:Ô@Ø�K‰K�Mð	?
Ð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._reset_   sU   € Ø�Z‹ZÜ'Ÿ|™|›~ˆDÔØ!×-Ñ-´	Ø×6Ñ6ô1ñ ˆDŒNð $%ˆDÔ Ø&'ˆDÔ#÷ �Z‰Zú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Ø*KØ7;ñ'àð'ð !&ð'ð #(ð	'ð
 ˜d 4¨	¡?Ñ3Ñ4ð'ð 
ó'ð@$¨)ð $¸%ð $ÀDó $ó2ð)¨eð )¸ó )òô(r   r'   )rG   r9   Úabcr   r   r   r   Útypingr   r   r	   Úredis.multidb.circuitr
   rI   rQ   rR   rS   r   r'   r   r   r   Ú<module>rY      sF   ðÛ Û ß #ß (ß 'Ñ 'å 2àÐ Ø!$Ð Ø$%Ð !ô�cô ô"G(˜_õ G(r   