]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #27456: asyncio: Set TCP_NODELAY by default.
authorYury Selivanov <yury@magic.io>
Mon, 12 Sep 2016 01:39:31 +0000 (21:39 -0400)
committerYury Selivanov <yury@magic.io>
Mon, 12 Sep 2016 01:39:31 +0000 (21:39 -0400)
Lib/asyncio/selector_events.py
Misc/NEWS

index c57f509a12b444d1b70f9c3a4e0e1ace072b6f9a..c91ab04f3c08f61b7295abcb7f5c3dcc7429d15d 100644 (file)
@@ -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,
index d1c36f0fbf7cec3ab33b28381200c9c5b4b63fdd..73286b7eab6908bd9ad9828ec885806c89653c6a 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -254,6 +254,8 @@ Library
 - Issue #21201: Improves readability of multiprocessing error message.  Thanks
   to Wojciech Walczak for patch.
 
+- Issue #27456: asyncio: Set TCP_NODELAY by default.
+
 IDLE
 ----