From 69e7416c0fb4ec31ff7bc81f0385d109ca629b11 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 24 Jan 2016 17:57:36 +0200 Subject: [PATCH] Use CORK/NODELAY in client API --- aiohttp/client.py | 1 + aiohttp/client_reqrep.py | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/aiohttp/client.py b/aiohttp/client.py index aac20f2c99a..aba999118ab 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -322,6 +322,7 @@ def _ws_connect(self, url, *, break reader = resp.connection.reader.set_parser(WebSocketParser) + resp.connection.writer.set_tcp_nodelay(True) writer = WebSocketWriter(resp.connection.writer, use_mask=True) except Exception: resp.close() diff --git a/aiohttp/client_reqrep.py b/aiohttp/client_reqrep.py index 551c296000a..1889510b819 100644 --- a/aiohttp/client_reqrep.py +++ b/aiohttp/client_reqrep.py @@ -373,6 +373,7 @@ def write_bytes(self, request, reader): try: if asyncio.iscoroutine(self.body): + request.transport.set_tcp_nodelay(True) exc = None value = None stream = self.body @@ -407,12 +408,14 @@ def write_bytes(self, request, reader): type(result)) elif isinstance(self.body, asyncio.StreamReader): + request.transport.set_tcp_nodelay(True) chunk = yield from self.body.read(streams.DEFAULT_LIMIT) while chunk: yield from request.write(chunk, drain=True) chunk = yield from self.body.read(streams.DEFAULT_LIMIT) elif isinstance(self.body, streams.DataQueue): + request.transport.set_tcp_nodelay(True) while True: try: chunk = yield from self.body.read() @@ -427,6 +430,7 @@ def write_bytes(self, request, reader): while chunk: request.write(chunk) chunk = self.body.read(self.chunked) + request.transport.set_tcp_nodelay(True) else: if isinstance(self.body, (bytes, bytearray)): @@ -434,6 +438,8 @@ def write_bytes(self, request, reader): for chunk in self.body: request.write(chunk) + request.transport.set_tcp_nodelay(True) + except Exception as exc: new_exc = aiohttp.ClientRequestError( 'Can not write request body for %s' % self.url) @@ -441,6 +447,7 @@ def write_bytes(self, request, reader): new_exc.__cause__ = exc reader.set_exception(new_exc) else: + assert request.transport.tcp_nodelay try: ret = request.write_eof() # NB: in asyncio 3.4.1+ StreamWriter.drain() is coroutine @@ -458,6 +465,7 @@ def write_bytes(self, request, reader): self._writer = None def send(self, writer, reader): + writer.set_tcp_cork(True) request = aiohttp.Request(writer, self.method, self.path, self.version) if self.compress: