Skip to content

Commit

Permalink
tcp_nodelay feature (default: False)
Browse files Browse the repository at this point in the history
  • Loading branch information
thefab committed Jun 3, 2015
1 parent f2690d8 commit 7bd0b1c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ def close(self, *args, **kwargs):
def getsockopt(self, *args, **kwargs):
return self.__socket.getsockopt(*args, **kwargs)

def setsockopt(self, *args, **kwargs):
return self.__socket.setsockopt(*args, **kwargs)

def recv(self, *args, **kwargs):
return self.__socket.recv(*args, **kwargs)

Expand Down Expand Up @@ -124,6 +127,12 @@ def _read_cb(self, data):
else:
break

@tornado.testing.gen_test
def test_init_with_tcp_nodelay(self):
c = Connection(self._read_cb, self._close_cb, tcp_nodelay=True)
yield c.connect()
c.disconnect()

@tornado.testing.gen_test
def test_write(self):
yield self._test_basic_write()
Expand Down
7 changes: 6 additions & 1 deletion tornadis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Connection(object):
read_page_size (int): page size for reading.
write_page_size (int): page size for writing.
connect_timeout (int): timeout (in seconds) for connecting.
tcp_nodelay (boolean): set TCP_NODELAY on socket.
"""

def __init__(self, read_callback, close_callback,
Expand All @@ -52,7 +53,7 @@ def __init__(self, read_callback, close_callback,
read_page_size=tornadis.DEFAULT_READ_PAGE_SIZE,
write_page_size=tornadis.DEFAULT_WRITE_PAGE_SIZE,
connect_timeout=tornadis.DEFAULT_CONNECT_TIMEOUT,
ioloop=None):
tcp_nodelay=False, ioloop=None):
"""Constructor.
Args:
Expand All @@ -63,6 +64,7 @@ def __init__(self, read_callback, close_callback,
read_page_size (int): page size for reading.
write_page_size (int): page size for writing.
connect_timeout (int): timeout (in seconds) for connecting.
tcp_nodelay (boolean): set TCP_NODELAY on socket.
ioloop (IOLoop): the tornado ioloop to use.
"""
self.host = host
Expand All @@ -77,6 +79,7 @@ def __init__(self, read_callback, close_callback,
self.read_page_size = read_page_size
self.write_page_size = write_page_size
self.connect_timeout = connect_timeout
self.tcp_nodelay = tcp_nodelay
self._write_buffer = WriteBuffer()
self._listened_events = 0

Expand All @@ -100,6 +103,8 @@ def connect(self):
raise tornado.gen.Return(True)
self.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.__socket.setblocking(0)
if self.tcp_nodelay:
self.__socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.__periodic_callback.start()
try:
LOG.debug("connecting to %s:%i...", self.host, self.port)
Expand Down

0 comments on commit 7bd0b1c

Please sign in to comment.