Skip to content

Commit

Permalink
Don't reschedule keepalive processor immediatelly to prevent 100% CPU…
Browse files Browse the repository at this point in the history
… load
  • Loading branch information
asvetlov committed Feb 1, 2018
1 parent 169d622 commit 7557fdf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
7 changes: 5 additions & 2 deletions aiohttp/web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class RequestHandler(asyncio.streams.FlowControlMixin, asyncio.Protocol):
"""
_request_count = 0
_keepalive = False # keep transport open
KEEPALIVE_RESCHEDULE_DELAY = 1

def __init__(self, manager, *, loop=None,
keepalive_timeout=75, # NGINX default value is 75 secs
Expand Down Expand Up @@ -363,8 +364,10 @@ def _process_keepalive(self):
self.force_close(send_last_heartbeat=True)
return

self._keepalive_handle = self._loop.call_at(
next, self._process_keepalive)
# not all request handlers are done,
# reschedule itself to next second
self._keepalive_handle = self._loop.call_later(
self.KEEPALIVE_RESCHEDULE_DELAY, self._process_keepalive)

def pause_reading(self):
if not self._reading_paused:
Expand Down
7 changes: 4 additions & 3 deletions tests/test_web_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def test_connection_made(make_srv):
assert not srv._force_close


def test_connection_made_with_keepaplive(make_srv, transport):
def test_connection_made_with_tcp_keepaplive(make_srv, transport):
srv = make_srv()

sock = mock.Mock()
Expand All @@ -214,7 +214,7 @@ def test_connection_made_with_keepaplive(make_srv, transport):
socket.SO_KEEPALIVE, 1)


def test_connection_made_without_keepaplive(make_srv):
def test_connection_made_without_tcp_keepaplive(make_srv):
srv = make_srv(tcp_keepalive=False)

sock = mock.Mock()
Expand Down Expand Up @@ -583,6 +583,7 @@ def test_handle_500(srv, loop, buf, transport, request_handler):
@asyncio.coroutine
def test_keep_alive(make_srv, loop, transport, ceil):
srv = make_srv(keepalive_timeout=0.05)
srv.KEEPALIVE_RESCHEDULE_DELAY = 0.1
srv.connection_made(transport)

srv.keep_alive(True)
Expand All @@ -600,7 +601,7 @@ def test_keep_alive(make_srv, loop, transport, ceil):
assert srv._keepalive_handle is not None
assert not transport.close.called

yield from asyncio.sleep(0.1, loop=loop)
yield from asyncio.sleep(0.2, loop=loop)
assert transport.close.called
assert srv._waiters[0].cancelled

Expand Down

0 comments on commit 7557fdf

Please sign in to comment.