Issue #27456: asyncio: Set TCP_NODELAY by default.

This commit is contained in:
Yury Selivanov 2016-09-11 21:39:31 -04:00
parent a05a6ef1ca
commit 44c19eccf9
2 changed files with 18 additions and 0 deletions

View File

@ -39,6 +39,17 @@ def _test_selector_event(selector, fd, event):
return bool(key.events & event) return bool(key.events & event)
if hasattr(socket, 'TCP_NODELAY'):
def _set_nodelay(sock):
if (sock.family in {socket.AF_INET, socket.AF_INET6} and
sock.type == socket.SOCK_STREAM and
sock.proto == socket.IPPROTO_TCP):
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
else:
def _set_nodelay(sock):
pass
class BaseSelectorEventLoop(base_events.BaseEventLoop): class BaseSelectorEventLoop(base_events.BaseEventLoop):
"""Selector event loop. """Selector event loop.
@ -640,6 +651,11 @@ class _SelectorSocketTransport(_SelectorTransport):
self._eof = False self._eof = False
self._paused = False self._paused = False
# Disable the Nagle algorithm -- small writes will be
# sent without waiting for the TCP ACK. This generally
# decreases the latency (in some cases significantly.)
_set_nodelay(self._sock)
self._loop.call_soon(self._protocol.connection_made, self) self._loop.call_soon(self._protocol.connection_made, self)
# only start reading when connection_made() has been called # only start reading when connection_made() has been called
self._loop.call_soon(self._loop.add_reader, self._loop.call_soon(self._loop.add_reader,

View File

@ -254,6 +254,8 @@ Library
- Issue #21201: Improves readability of multiprocessing error message. Thanks - Issue #21201: Improves readability of multiprocessing error message. Thanks
to Wojciech Walczak for patch. to Wojciech Walczak for patch.
- Issue #27456: asyncio: Set TCP_NODELAY by default.
IDLE IDLE
---- ----