
    ɯei                        d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	  ee
      Z	 d dlmZ dZ G d	 d
e      ZddZ G d d      Z G d d      Zy# e$ r	 d dlmZ Y 3w xY w)    )annotationsN)	getLogger)TracebackType)CallableIterator)_Timer)Timeri@8  c                  8     e Zd ZdZ	 	 	 	 	 	 d fdZddZ xZS )HeartBeatTimerz_A thread which executes a function every client_session_keep_alive_heartbeat_frequency seconds.c                8    |}t         |   ||       d| _        y )NT)super__init__daemon)self-client_session_keep_alive_heartbeat_frequencyfinterval	__class__s       _/var/www/html/glpi_dashboard/venv/lib/python3.12/site-packages/snowflake/connector/time_util.pyr   zHeartBeatTimer.__init__   s!     A1%    c                h   | j                   j                         sl| j                   j                  | j                         | j                   j                         s	 | j	                          | j                   j                         sky y # t
        $ r }t        j                  d|       Y d }~Ad }~ww xY w)Nzfailed to heartbeat: %s)finishedis_setwaitr   function	Exceptionloggerdebug)r   es     r   runzHeartBeatTimer.run   s|    --&&(MMt}}-=='')?MMO	 --&&(
 ! ?LL!:A>>?s   B 	B1B,,B1)r   intr   r   returnNoner"   r#   )__name__
__module____qualname____doc__r   r    __classcell__)r   s   @r   r   r      s(    i=@EM	?r   r   c                 B    t        t        j                         dz        S )z)Returns the current time in milliseconds.  )r!   time r   r   get_time_millisr.   (   s    tyy{T!""r   c                  @    e Zd ZdZddZddZ	 	 	 	 	 	 	 	 d	dZd
dZy)TimerContextManagera=  Context manager class to easily measure execution of a code block.

    Once the context manager finishes, the class should be cast into an int to retrieve
    result.

    Example:

        with TimerContextManager() as measured_time:
            pass
        download_metric = measured_time.get_timing_millis()
    c                     d | _         d | _        y N)_start_endr   s    r   r   zTimerContextManager.__init__:   s    "& $	r   c                $    t               | _        | S r2   )r.   r3   r5   s    r   	__enter__zTimerContextManager.__enter__>   s    %'r   c                "    t               | _        y r2   )r.   r4   )r   exc_typeexc_valexc_tbs       r   __exit__zTimerContextManager.__exit__B   s     $%	r   c                z    | j                   | j                  t        d      | j                  | j                   z
  S )z$Get measured timing in milliseconds.z<Trying to get timing before TimerContextManager has finished)r3   r4   r   r5   s    r   get_timing_millisz%TimerContextManager.get_timing_millisJ   s:    ;;$))"3N  yy4;;&&r   Nr$   )r"   r0   )r9   ztype[BaseException] | Noner:   zBaseException | Noner;   zTracebackType | Noner"   r#   r"   r!   )r%   r&   r'   r(   r   r7   r<   r>   r-   r   r   r0   r0   -   sA    
%&,& && %	&
 
&'r   r0   c                      e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 ddZedd       Zedd       Zedd       Zedd       Z	edd       Z
dd	Zdd
ZddZy)TimeoutBackoffCtxz:Base context for handling timeouts and backoffs on retriesNc                t    || _         || _        || _        d| _        | j	                         | _        d | _        y Nr   )_backoff_generator_max_retry_attempts_timeout_current_retry_count_advance_backoff_current_sleep_time_start_time_millis)r   max_retry_attemptstimeoutbackoff_generators       r   r   zTimeoutBackoffCtx.__init__V   s<     #4#5 $%!#'#8#8#: "&r   c                    | j                   S r2   )rF   r5   s    r   rL   zTimeoutBackoffCtx.timeoutf   s    }}r   c                ,    t        | j                        S r2   )r!   rG   r5   s    r   current_retry_countz%TimeoutBackoffCtx.current_retry_countj   s    4,,--r   c                ,    t        | j                        S r2   )r!   rI   r5   s    r   current_sleep_timez$TimeoutBackoffCtx.current_sleep_timen   s    4++,,r   c                    | j                   t        d      | j                  t        d      | j                  dz  }t               | j                   z
  }||z
  S )NzKStart time not recorded in remaining_time_millis, call set_start_time firstz(Timeout is None in remaining_time_millisr+   )rJ   	TypeErrorrF   r.   )r   timeout_milliselapsed_time_milliss      r   remaining_time_millisz'TimeoutBackoffCtx.remaining_time_millisr   sd    ""*]  == FGG--/$2I2II 333r   c                    | j                   !| j                  t        j                  d       | j                   | j                  dk  nd}| j
                  | j                  | j
                  k\  nd}| xr | S )z$Decides whether to retry connection.z=Timeout set in TimeoutBackoffCtx, but start time not recordedr   F)rF   rJ   r   warningrW   rE   rG   )r   	timed_outretry_attempts_exceededs      r   should_retryzTimeoutBackoffCtx.should_retry   s     ==$)@)@)HNNO
 /3mm.GD&&*U 	
 ''3 %%)A)AA 	 
 }<%<!<<r   c                H    | j                   t        | j                         S dS rC   )rD   nextr5   s    r   rH   z"TimeoutBackoffCtx._advance_backoff   s(    -1-D-D-PD(()	
VW	
r   c                "    t               | _        y r2   )r.   rJ   r5   s    r   set_start_timez TimeoutBackoffCtx.set_start_time   s    "1"3r   c                    | xj                   dz  c_         | j                         | _        t        j	                  d| j                           t        j	                  d| j                   d       y)z4Updates retry count and sleep time for another retry   zUpdate retry count to zUpdate sleep time to z secondsN)rG   rH   rI   r   r   r5   s    r   	incrementzTimeoutBackoffCtx.increment   s\    !!Q&!#'#8#8#: -d.G.G-HIJ,T-E-E,FhOPr   )NNN)rK   
int | NonerL   rd   rM   zIterator | Noner"   r#   )r"   rd   r?   )r"   boolr$   )r%   r&   r'   r(   r   propertyrL   rP   rR   rW   r\   rH   r`   rc   r-   r   r   rA   rA   S   s    D *."-1	'&' ' +	'
 
'    . . - - 4 4 = ="

4Qr   rA   r?   )
__future__r   r,   loggingr   typesr   typingr   r   r%   r   	threadingr   r	   ImportError"DEFAULT_MASTER_VALIDITY_IN_SECONDSr   r.   r0   rA   r-   r   r   <module>rn      sm    "    %	8	 ) &1 "?U ?*#
#' #'LLQ LQM    s   A A"!A"