Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions aiohttp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ def __init__(self, *, loop=None,
self._keep_alive_period = keep_alive # number of seconds to keep alive
self._timeout = timeout # slow request timeout
self._loop = loop if loop is not None else asyncio.get_event_loop()
self._tcp_nodelay = False

self.logger = log or logger
self.debug = debug
Expand Down Expand Up @@ -291,15 +292,17 @@ def start(self):
reader.unset_parser()

if self._request_handler:
if self._keep_alive and self._keep_alive_period:
self.log_debug(
'Start keep-alive timer for %s sec.',
self._keep_alive_period)
self._keep_alive_handle = self._loop.call_later(
self._keep_alive_period, self.transport.close)
elif self._keep_alive and self._keep_alive_on:
# do nothing, rely on kernel or upstream server
pass
if self._keep_alive:
self.set_tcp_nodelay(True)
if self._keep_alive_period:
self.log_debug(
'Start keep-alive timer for %s sec.',
self._keep_alive_period)
self._keep_alive_handle = self._loop.call_later(
self._keep_alive_period, self.transport.close)
elif self._keep_alive_on:
# do nothing, rely on kernel or upstream server
pass
else:
self.log_debug('Close client connection.')
self._request_handler = None
Expand Down Expand Up @@ -388,3 +391,19 @@ def handle_request(self, message, payload):
self.log_access(message, None, response, self._loop.time() - now)

return drain

@property
def tcp_nodelay(self):
return self._tcp_nodelay

def set_tcp_nodelay(self, nodelay):
if nodelay == self._tcp_nodelay:
return
self._tcp_nodelay = nodelay
if self.transport is None:
# transport is closed
return
sock = self.transport.get_extra_info('socket')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also it will be nice to cache socket reference in order to speedup setting and removing nodelay

if sock.family not in (socket.AF_INET, socket.AF_INET6):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transports need not have a socket, i.e. get_extra_info('socket') might return a None.
So, should probably do a if sock and sock.family not in (socket.AF_INET, socket.AF_INET6) to be on the safe side.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace 1 with 1 if nodelay else 0

8 changes: 8 additions & 0 deletions tests/test_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,3 +559,11 @@ def test_keep_alive_timeout_default(self):
def test_keep_alive_timeout_nondefault(self):
srv = server.ServerHttpProtocol(loop=self.loop, keep_alive=10)
self.assertEqual(10, srv.keep_alive_timeout)

def test_tcp_nodelay(self):
srv = server.ServerHttpProtocol(loop=self.loop)
self.assertFalse(srv.tcp_nodelay)
srv.set_tcp_nodelay(True)
self.assertTrue(srv.tcp_nodelay)
srv.set_tcp_nodelay(False)
self.assertFalse(srv.tcp_nodelay)