Skip to content

Commit

Permalink
Merge pull request #1067 from lukebakken/pika-1055-master-tweaks
Browse files Browse the repository at this point in the history
Fixup for #1055
  • Loading branch information
michaelklishin committed Jun 7, 2018
2 parents e1f0ef5 + a6c8af8 commit a320aa8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/consume.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'-35s %(lineno) -5d: %(message)s')
LOGGER = logging.getLogger(__name__)

logging.basicConfig(level=logging.INFO, format=LOG_FORMAT)
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT)

def on_message(channel, method_frame, header_frame, body, userdata=None):
LOGGER.info('Userdata: {} Message body: {}'.format(userdata, body))
Expand Down
18 changes: 15 additions & 3 deletions pika/heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,22 @@ def __init__(self, connection, interval=DEFAULT_INTERVAL, idle_count=MAX_IDLE_CO
:param pika.connection.Connection: Connection object
:param int interval: Heartbeat check interval. Note: heartbeats will
be sent at interval / 2 frequency.
:param int idle_count: The number of heartbeat intervals without data
received that will close the current connection.
"""
self._connection = connection

# Note: see the following document:
# https://www.rabbitmq.com/heartbeats.html#heartbeats-timeout
self._interval = float(interval / 2)
self._max_idle_count = idle_count

# Note: even though we're sending heartbeats in half the specified
# interval, the broker will be sending them to us at the specified
# interval. This means we'll be checking for an idle connection
# twice as many times as the broker will send heartbeats to us,
# so we need to double the max idle count here
self._max_idle_count = idle_count * 2

# Initialize counters
self._bytes_received = 0
Expand Down Expand Up @@ -83,9 +92,12 @@ def send_and_check(self):
been idle too long.
"""
LOGGER.debug('Received %i heartbeat frames, sent %i',
LOGGER.debug('Received %i heartbeat frames, sent %i, '
'idle intervals %i, max idle count %i',
self._heartbeat_frames_received,
self._heartbeat_frames_sent)
self._heartbeat_frames_sent,
self._idle_byte_intervals,
self._max_idle_count)

if self.connection_is_idle:
self._close_connection()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/heartbeat_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_default_initialization_interval(self):
self.assertEqual(self.obj._interval, self.HALF_INTERVAL)

def test_default_initialization_max_idle_count(self):
self.assertEqual(self.obj._max_idle_count, self.obj.MAX_IDLE_COUNT)
self.assertEqual(self.obj._max_idle_count, self.obj.MAX_IDLE_COUNT * 2)

def test_constructor_assignment_connection(self):
self.assertIs(self.obj._connection, self.mock_conn)
Expand Down

0 comments on commit a320aa8

Please sign in to comment.