
    )Jf                     b    d Z ddlZddlZddlmZ  ej        e          Z G d de          Z	dS )zHandle AMQP Heartbeats    N)framec                       e Zd ZdZdZd Zed             Zed             Zd Z	d Z
d Zd	 Zd
 Zed             Zed             Zd Zd Zd Zd ZdS )HeartbeatCheckerzSends heartbeats to the broker. The provided timeout is used to
    determine if the connection is stale - no received heartbeats or
    other activity will close the connection. See the parameter list for more
    details.

    z@No activity or too many missed heartbeats in the last %i secondsc                    |dk     rt          d|          || _        || _        t          |          dz  | _        |dz   | _        t                              d| j        | j        | j                   d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        |                                  |                                  dS )a  Create an object that will check for activity on the provided
        connection as well as receive heartbeat frames from the broker. The
        timeout parameter defines a window within which this activity must
        happen. If not, the connection is considered dead and closed.

        The value passed for timeout is also used to calculate an interval
        at which a heartbeat frame is sent to the broker. The interval is
        equal to the timeout value divided by two.

        :param pika.connection.Connection: Connection object
        :param int timeout: Connection idle timeout. If no activity occurs on the
                            connection nor heartbeat frames received during the
                            timeout window the connection will be closed. The
                            interval used to send heartbeats is calculated from
                            this value by dividing it by two.

           ztimeout must >= 0, but got       z0timeout: %f send_interval: %f check_interval: %fr   N)
ValueError_connection_timeoutfloat_send_interval_check_intervalLOGGERdebug_bytes_received_bytes_sent_heartbeat_frames_received_heartbeat_frames_sent_idle_byte_intervals_send_timer_check_timer_start_send_timer_start_check_timer)self
connectiontimeouts      Q/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/pika/heartbeat.py__init__zHeartbeatChecker.__init__   s    $ Q;;*IJJJ%  #Gnnq0  '{G]D$79M	O 	O 	O  !*+'&'#$%!    !!!!!    c                     | j         j        S )zaReturn the number of bytes received by the connection bytes object.

        :rtype int

        )r   bytes_receivedr   s    r   bytes_received_on_connectionz-HeartbeatChecker.bytes_received_on_connectionR   s     ..r    c                     | j         dk    S )zsReturns true if the byte count hasn't changed in enough intervals
        to trip the max idle threshold.

        r   )r   r#   s    r   connection_is_idlez#HeartbeatChecker.connection_is_idle[   s     (1,,r    c                 Z    t                               d           | xj        dz  c_        dS )z#Called when a heartbeat is receivedzReceived heartbeat framer   N)r   r   r   r#   s    r   receivedzHeartbeatChecker.receivedc   s/    /000''1,''''r    c                     t                               d           |                                  |                                  dS )zAInvoked by a timer to send a heartbeat when we need to.

        Sending heartbeat frameN)r   r   _send_heartbeat_framer   r#   s    r   _send_heartbeatz HeartbeatChecker._send_heartbeath   sA     	.///""$$$     r    c                     | j         rd| _        n| xj        dz  c_        t                              d| j        | j        | j                   | j        r|                                  dS |                                  dS )zInvoked by a timer to check for broker heartbeats. Checks to see
        if we've missed any heartbeats and disconnect our connection if it's
        been idle too long.

        r   r   z8Received %i heartbeat frames, sent %i, idle intervals %iN)	_has_received_datar   r   r   r   r   r&   _close_connectionr   r#   s    r   _check_heartbeatz!HeartbeatChecker._check_heartbeatp   s     " 	+()D%% %%*%% !%!@')B	D 	D 	D
 " 	""$$$F!!!!!r    c                 &   | j         r@t                              d           | j                            | j                    d| _         | j        rBt                              d           | j                            | j                   d| _        dS dS )zStop the heartbeat checkerz/Removing timer for next heartbeat send intervalNz0Removing timer for next heartbeat check interval)r   r   r   r   _adapter_remove_timeoutr   r#   s    r   stopzHeartbeatChecker.stop   s     	$LLJKKK44T5EFFF#D 	%LLKLLL44T5FGGG $D	% 	%r    c                     t                               d| j                   t          j        | j        z  }| j                            t          j	        
                    |                     dS )z;Close the connection with the AMQP Connection-Forced value.z+Connection is idle, %i stale byte intervalsN)r   infor   r   _STALE_CONNECTIONr   r   _terminate_streampika
exceptionsAMQPHeartbeatTimeout)r   texts     r   r/   z"HeartbeatChecker._close_connection   sj    A-	/ 	/ 	/1DMA
 	**O0066	8 	8 	8 	8 	8r    c                 "    | j         | j        k    S )zQReturns True if the connection has received data.

        :rtype: bool

        )r   r$   r#   s    r   r.   z#HeartbeatChecker._has_received_data   s     #t'HHHr    c                  (    t          j                    S )zLReturn a new heartbeat frame.

        :rtype pika.frame.Heartbeat

        )r   	Heartbeat r    r   _new_heartbeat_framez%HeartbeatChecker._new_heartbeat_frame   s        r    c                     t                               d           | j                            |                                            | xj        dz  c_        dS )z3Send a heartbeat frame on the connection.

        r*   r   N)r   r   r   _send_framer@   r   r#   s    r   r+   z&HeartbeatChecker._send_heartbeat_frame   s[     	.///$$%%''	) 	) 	)##q(####r    c                 Z    | j                             | j        | j                  | _        dS )z!Start a new heartbeat send timer.N)r   _adapter_call_laterr   r,   r   r#   s    r   r   z"HeartbeatChecker._start_send_timer   s/    +?? " "r    c                     |                                   | j                            | j        | j                  | _        dS )z"Start a new heartbeat check timer.N)_update_countersr   rD   r   r0   r   r#   s    r   r   z#HeartbeatChecker._start_check_timer   sC     	 ,@@ !# #r    c                 J    | j         j        | _        | j         j        | _        dS )zlUpdate the internal counters for bytes sent and received and the
        number of frames received

        N)r   
bytes_sentr   r"   r   r#   s    r   rF   z!HeartbeatChecker._update_counters   s&    
  +6#/>r    N)__name__
__module____qualname____doc__r6   r   propertyr$   r&   r(   r,   r0   r3   r/   r.   staticmethodr@   r+   r   r   rF   r?   r    r   r   r   
   s-         [=" =" ="~ / / X/ - - X-- - -
! ! !" " ".	% 	% 	%
8 
8 
8 I I XI ! ! \!) ) )" " "
# 
# 
#? ? ? ? ?r    r   )
rL   loggingpika.exceptionsr8   r   	getLoggerrI   r   objectr   r?   r    r   <module>rS      s                		8	$	$G? G? G? G? G?v G? G? G? G? G?r    