From: Kristján Valur Jónsson Date: Sun, 7 Jun 2009 16:43:23 +0000 (+0000) Subject: http://bugs.python.org/issue6192 X-Git-Tag: v2.7a1~1014 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=afefcfd4bff7a50ba9a5fdd2b1a355f133c4b956;p=python http://bugs.python.org/issue6192 Add a feature to disable the Nagle algorithm on sockets in TCPServer --- diff --git a/Doc/library/socketserver.rst b/Doc/library/socketserver.rst index 078f21682b..bf93cce875 100644 --- a/Doc/library/socketserver.rst +++ b/Doc/library/socketserver.rst @@ -223,6 +223,15 @@ The server classes support the following class variables: desired. If :meth:`handle_request` receives no incoming requests within the timeout period, the :meth:`handle_timeout` method is called. +.. attribute:: TCPServer.disable_nagle_algorithm + + If set to True, it will set the TCP_NODELAY attribute of new requests + connections. This can help alleviate problems with latency in + request-response type applications. To avoid sending many small packets, + this option should be used only when bufferning output, such as when + setting :attr:`StreamRequestHandler.wbufsize` attribute to -1. + + .. versionadded:: 2.7 There are various server methods that can be overridden by subclasses of base server classes like :class:`TCPServer`; these methods aren't useful to external diff --git a/Lib/SocketServer.py b/Lib/SocketServer.py index f01cb5f2cc..5f8facce16 100644 --- a/Lib/SocketServer.py +++ b/Lib/SocketServer.py @@ -374,6 +374,7 @@ class TCPServer(BaseServer): - socket_type - request_queue_size (only for stream sockets) - allow_reuse_address + - disable_nagle_algorithm Instance variables: @@ -391,6 +392,8 @@ class TCPServer(BaseServer): allow_reuse_address = False + disable_nagle_algorithm = False + def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True): """Constructor. May be extended, do not override.""" BaseServer.__init__(self, server_address, RequestHandlerClass) @@ -441,7 +444,10 @@ class TCPServer(BaseServer): May be overridden. """ - return self.socket.accept() + request = self.socket.accept() + if self.disable_nagle_algorithm: + request[0].setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True) + return request def close_request(self, request): """Called to clean up an individual request."""