Skip to content

Commit

Permalink
add a grace period of 5 seconds to ping timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jan 9, 2019
1 parent c41bb5d commit b5f15e3
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 8 deletions.
5 changes: 4 additions & 1 deletion engineio/asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,11 @@ async def _write_loop(self):
pushed to the send queue.
"""
while self.state == 'connected':
# to simplify the timeout handling, use the maximum of the
# ping interval and ping timeout as timeout, with an extra 5
# seconds grace period
timeout = max(self.ping_interval, self.ping_timeout) + 5
packets = None
timeout = max(self.ping_interval, self.ping_timeout)
try:
packets = [await asyncio.wait_for(self.queue.get(), timeout)]
except (self.queue.Empty, asyncio.TimeoutError,
Expand Down
2 changes: 1 addition & 1 deletion engineio/asyncio_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def check_ping_timeout(self):
"""
if self.closed:
raise exceptions.SocketIsClosedError()
if time.time() - self.last_ping > self.server.ping_timeout:
if time.time() - self.last_ping > self.server.ping_interval + 5:
self.server.logger.info('%s: Client is gone, closing socket',
self.sid)
# Passing abort=False here will cause close() to write a
Expand Down
5 changes: 4 additions & 1 deletion engineio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,11 @@ def _write_loop(self):
pushed to the send queue.
"""
while self.state == 'connected':
# to simplify the timeout handling, use the maximum of the
# ping interval and ping timeout as timeout, with an extra 5
# seconds grace period
timeout = max(self.ping_interval, self.ping_timeout) + 5
packets = None
timeout = max(self.ping_interval, self.ping_timeout)
try:
packets = [self.queue.get(timeout=timeout)]
except self.queue.Empty:
Expand Down
2 changes: 1 addition & 1 deletion engineio/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def check_ping_timeout(self):
"""
if self.closed:
raise exceptions.SocketIsClosedError()
if time.time() - self.last_ping > self.server.ping_timeout:
if time.time() - self.last_ping > self.server.ping_interval + 5:
self.server.logger.info('%s: Client is gone, closing socket',
self.sid)
# Passing abort=False here will cause close() to write a
Expand Down
2 changes: 1 addition & 1 deletion tests/asyncio/test_asyncio_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_invalid_packet(self):

def test_timeout(self):
mock_server = self._get_mock_server()
mock_server.ping_interval = -0.1
mock_server.ping_interval = -6
s = asyncio_socket.AsyncSocket(mock_server, 'sid')
s.last_ping = time.time() - 1
s.close = AsyncMock()
Expand Down
4 changes: 2 additions & 2 deletions tests/common/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ def test_write_loop_no_packets(self):
c.queue.get.return_value = None
c._write_loop()
c.queue.task_done.assert_called_once_with()
c.queue.get.assert_called_once_with(timeout=2)
c.queue.get.assert_called_once_with(timeout=7)

def test_write_loop_empty_queue(self):
c = client.Client()
Expand All @@ -894,7 +894,7 @@ def test_write_loop_empty_queue(self):
c.queue.Empty = RuntimeError
c.queue.get.side_effect = RuntimeError
c._write_loop()
c.queue.get.assert_called_once_with(timeout=2)
c.queue.get.assert_called_once_with(timeout=7)

def test_write_loop_polling_one_packet(self):
c = client.Client()
Expand Down
2 changes: 1 addition & 1 deletion tests/common/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_invalid_packet(self):

def test_timeout(self):
mock_server = self._get_mock_server()
mock_server.ping_interval = -0.1
mock_server.ping_interval = -6
s = socket.Socket(mock_server, 'sid')
s.last_ping = time.time() - 1
s.close = mock.MagicMock()
Expand Down

0 comments on commit b5f15e3

Please sign in to comment.