@@ -39,6 +39,17 @@ def _test_selector_event(selector, fd, event):
3939 return bool (key .events & event )
4040
4141
42+ if hasattr (socket , 'TCP_NODELAY' ):
43+ def _set_nodelay (sock ):
44+ if (sock .family in {socket .AF_INET , socket .AF_INET6 } and
45+ sock .type == socket .SOCK_STREAM and
46+ sock .proto == socket .IPPROTO_TCP ):
47+ sock .setsockopt (socket .IPPROTO_TCP , socket .TCP_NODELAY , 1 )
48+ else :
49+ def _set_nodelay (sock ):
50+ pass
51+
52+
4253class BaseSelectorEventLoop (base_events .BaseEventLoop ):
4354 """Selector event loop.
4455
@@ -560,6 +571,12 @@ def __repr__(self):
560571 def abort (self ):
561572 self ._force_close (None )
562573
574+ def set_protocol (self , protocol ):
575+ self ._protocol = protocol
576+
577+ def get_protocol (self ):
578+ return self ._protocol
579+
563580 def is_closing (self ):
564581 return self ._closing
565582
@@ -635,6 +652,11 @@ def __init__(self, loop, sock, protocol, waiter=None,
635652 self ._eof = False
636653 self ._paused = False
637654
655+ # Disable the Nagle algorithm -- small writes will be
656+ # sent without waiting for the TCP ACK. This generally
657+ # decreases the latency (in some cases significantly.)
658+ _set_nodelay (self ._sock )
659+
638660 self ._loop .call_soon (self ._protocol .connection_made , self )
639661 # only start reading when connection_made() has been called
640662 self ._loop .call_soon (self ._loop .add_reader ,
0 commit comments