From 50ccce80142713b45555608ab8da0600a500b1d9 Mon Sep 17 00:00:00 2001 From: Vadim Semenov Date: Mon, 27 Mar 2017 12:30:07 +0300 Subject: [PATCH] Use TCP_NODELAY for improved latency According to the NSQ protocol, consumers have to send `FIN`s for every message they receive, which makes them extremely slow with low `max_in_flight` values due to TCP buffering. Adding TCP_NODELAY option on `async.AsyncConn`'s sockets makes `nsq.Reader` substantially faster. My tests show increase from ~50 to ~4.5K messages per second on localhost. --- .gitignore | 4 ++++ nsq/async.py | 1 + 2 files changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 44d229a..47ae6e7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,7 @@ dist pynsq.egg-info build docs/_build +.cache +.eggs +*.dat +*.tmp diff --git a/nsq/async.py b/nsq/async.py index c3a71cb..c4fddb7 100644 --- a/nsq/async.py +++ b/nsq/async.py @@ -225,6 +225,7 @@ def connect(self): self.stream = tornado.iostream.IOStream(self.socket, io_loop=self.io_loop) self.stream.set_close_callback(self._socket_close) + self.stream.set_nodelay(True) self.state = CONNECTING self.on(event.CONNECT, self._on_connect)