From 4cdbdaf3d1837dee6c1e50c2f44f2adf0c912d2b Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 24 Dec 2016 13:57:27 +0000 Subject: [PATCH] Disable Nagle's algorithm to try to make things more uniformly responsive. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3368 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/httpd.js | 2 ++ core/network.js | 1 + src/Socket.cpp | 15 +++++++++++++++ src/Socket.h | 3 +++ 4 files changed, 21 insertions(+) diff --git a/core/httpd.js b/core/httpd.js index 04847cb8..ff1bedbf 100644 --- a/core/httpd.js +++ b/core/httpd.js @@ -403,6 +403,8 @@ function handleConnection(client) { } } + client.noDelay = true; + client.onError(function(error) { logError(client.peerName + " - - [" + new Date() + "] " + error); }); diff --git a/core/network.js b/core/network.js index f9089e53..6f6b1fe3 100644 --- a/core/network.js +++ b/core/network.js @@ -14,6 +14,7 @@ Connection.prototype.connect = function(host, port) { let connection = this; connection.close(); connection.socket = new Socket(); + connection.socket.noDelay = true; return connection.socket.connect(host, port).then(function() { connection.buffer = ""; return Promise.all([ diff --git a/src/Socket.cpp b/src/Socket.cpp index a5891ebc..1b54ba3c 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -41,6 +41,7 @@ Socket::Socket(Task* task) { socketTemplate->SetAccessor(v8::String::NewFromUtf8(task->getIsolate(), "peerName"), getPeerName, 0, data); socketTemplate->SetAccessor(v8::String::NewFromUtf8(task->getIsolate(), "peerCertificate"), getPeerCertificate, 0, data); socketTemplate->SetAccessor(v8::String::NewFromUtf8(task->getIsolate(), "isConnected"), isConnected, 0, data); + socketTemplate->SetAccessor(v8::String::NewFromUtf8(task->getIsolate(), "noDelay"), getNoDelay, setNoDelay, data); v8::Local socketObject = socketTemplate->NewInstance(); socketObject->SetInternalField(0, v8::External::New(task->getIsolate(), this)); @@ -645,6 +646,20 @@ void Socket::isConnected(v8::Local property, const v8::PropertyCallb } } +void Socket::getNoDelay(v8::Local property, const v8::PropertyCallbackInfo& info) { + if (Socket* socket = Socket::get(info.Data())) { + info.GetReturnValue().Set(v8::Boolean::New(info.GetIsolate(), socket->_noDelay)); + } +} + +void Socket::setNoDelay(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info) { + v8::Maybe boolValue = value->BooleanValue(info.GetIsolate()->GetCurrentContext()); + if (Socket* socket = Socket::get(info.Data())) { + socket->_noDelay = boolValue.IsJust() && boolValue.FromJust(); + uv_tcp_nodelay(&socket->_socket, socket->_noDelay ? 1 : 0); + } +} + void Socket::create(const v8::FunctionCallbackInfo& args) { v8::HandleScope handleScope(args.GetIsolate()); if (Socket* socket = new Socket(Task::get(args.GetIsolate()))) { diff --git a/src/Socket.h b/src/Socket.h index fdb627bc..035e79ed 100644 --- a/src/Socket.h +++ b/src/Socket.h @@ -31,6 +31,7 @@ private: promiseid_t _closePromise = -1; int _refCount = 1; bool _connected = false; + bool _noDelay = false; std::string _peerName; enum Direction { kUndetermined, kAccept, kConnect }; @@ -61,6 +62,8 @@ private: static void getPeerName(v8::Local property, const v8::PropertyCallbackInfo& info); static void getPeerCertificate(v8::Local property, const v8::PropertyCallbackInfo& info); static void isConnected(v8::Local property, const v8::PropertyCallbackInfo& info); + static void getNoDelay(v8::Local property, const v8::PropertyCallbackInfo& info); + static void setNoDelay(v8::Local property, v8::Local value, const v8::PropertyCallbackInfo& info); static Socket* get(v8::Handle socketObject); static void onClose(uv_handle_t* handle);