summaryrefslogtreecommitdiffstats
path: root/Lib/asyncio
diff options
context:
space:
mode:
authorYury Selivanov <yury@magic.io>2016-09-12 01:39:31 (GMT)
committerYury Selivanov <yury@magic.io>2016-09-12 01:39:31 (GMT)
commit44c19eccf9fd8258f8d939fe30b79b644b2c235a (patch)
tree659ed5c7ad935625240587fe85556e7065020111 /Lib/asyncio
parenta05a6ef1ca781e2f98fb4332284aca649f24f75d (diff)
downloadcpython-44c19eccf9fd8258f8d939fe30b79b644b2c235a.zip
cpython-44c19eccf9fd8258f8d939fe30b79b644b2c235a.tar.gz
cpython-44c19eccf9fd8258f8d939fe30b79b644b2c235a.tar.bz2
Issue #27456: asyncio: Set TCP_NODELAY by default.
Diffstat (limited to 'Lib/asyncio')
-rw-r--r--Lib/asyncio/selector_events.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index c57f509..c91ab04 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -39,6 +39,17 @@ def _test_selector_event(selector, fd, 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):
"""Selector event loop.
@@ -640,6 +651,11 @@ class _SelectorSocketTransport(_SelectorTransport):
self._eof = 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)
# only start reading when connection_made() has been called
self._loop.call_soon(self._loop.add_reader,