Skip to content

Commit

Permalink
Add connection.setNoDelay() to disable Nagle algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Sep 23, 2009
1 parent 2df13c7 commit e0ec003
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 3 deletions.
9 changes: 9 additions & 0 deletions deps/evcom/evcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,15 @@ evcom_stream_reset_timeout (evcom_stream *stream, float timeout)
}
}

void
evcom_stream_set_no_delay (evcom_stream *stream, int no_delay)
{
if (DUPLEX(stream)) {
int flags = no_delay ? 1 : 0;
setsockopt(stream->recvfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
}
}

void
evcom_stream_attach (EV_P_ evcom_stream *stream)
{
Expand Down
1 change: 1 addition & 0 deletions deps/evcom/evcom.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ void evcom_stream_detach (evcom_stream *);
void evcom_stream_read_resume (evcom_stream *);
void evcom_stream_read_pause (evcom_stream *);
void evcom_stream_reset_timeout (evcom_stream *, float timeout);
void evcom_stream_set_no_delay (evcom_stream *, int no_delay);
void evcom_stream_write (evcom_stream *, const char *str, size_t len);
/* Once the write buffer is drained, evcom_stream_close will shutdown the
* writing end of the stream and will close the read end once the server
Expand Down
12 changes: 11 additions & 1 deletion doc/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,16 @@ <h4 id="_tt_node_tcp_connection_tt"><tt>node.tcp.Connection</tt></h4>
</p>
<div class="paragraph"><p>If <tt>timeout</tt> is 0, then the idle timeout is disabled.</p></div>
</dd>
<dt class="hdlist1">
<tt>connection.setNoDelay(noDelay=true)</tt>
</dt>
<dd>
<p>
Disables the Nagle algorithm. By default TCP connections use the Nagle
algorithm, they buffer data before sending it off. Setting <tt>noDelay</tt> will
immediately fire off data each time <tt>connection.send()</tt> is called.
</p>
</dd>
</dl></div>
<h3 id="_dns">DNS</h3><div style="clear:left"></div>
<div class="paragraph"><p>Here is an example of which resolves <tt>"www.google.com"</tt> then reverse
Expand Down Expand Up @@ -1922,7 +1932,7 @@ <h2 id="_extension_api">Extension API</h2>
<div id="footer">
<div id="footer-text">
Version 0.1.11<br />
Last updated 2009-09-21 12:26:35 CEST
Last updated 2009-09-23 15:35:53 CEST
</div>
</div>
</body>
Expand Down
4 changes: 4 additions & 0 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,10 @@ of 60 seconds (60000 ms).
+
If +timeout+ is 0, then the idle timeout is disabled.

+connection.setNoDelay(noDelay=true)+::
Disables the Nagle algorithm. By default TCP connections use the Nagle
algorithm, they buffer data before sending it off. Setting +noDelay+ will
immediately fire off data each time +connection.send()+ is called.

=== DNS

Expand Down
13 changes: 11 additions & 2 deletions doc/node.1
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.\" Title: node
.\" Author:
.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
.\" Date: 09/21/2009
.\" Date: 09/23/2009
.\" Manual:
.\" Source:
.\"
.TH "NODE" "1" "09/21/2009" "" ""
.TH "NODE" "1" "09/23/2009" "" ""
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
Expand Down Expand Up @@ -1639,6 +1639,15 @@ If
timeout
is 0, then the idle timeout is disabled\.
.RE
.PP
connection\.setNoDelay(noDelay=true)
.RS 4
Disables the Nagle algorithm\. By default TCP connections use the Nagle algorithm, they buffer data before sending it off\. Setting
noDelay
will immediately fire off data each time
connection\.send()
is called\.
.RE
.RE
.SS "DNS"
Here is an example of which resolves "www\.google\.com" then reverse resolves the IP addresses which are returned\.
Expand Down
15 changes: 15 additions & 0 deletions src/net.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void Connection::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor_template, "readPause", ReadPause);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "readResume", ReadResume);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setTimeout", SetTimeout);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setNoDelay", SetNoDelay);

constructor_template->PrototypeTemplate()->SetAccessor(
READY_STATE_SYMBOL,
Expand Down Expand Up @@ -332,6 +333,20 @@ Handle<Value> Connection::ForceClose(const Arguments& args) {
return Undefined();
}

Handle<Value> Connection::SetNoDelay(const Arguments& args) {
HandleScope scope;
Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());

bool no_delay = true;
if (args.Length() > 0) {
no_delay = args[0]->IsTrue();
}

connection->SetNoDelay(no_delay);

return Undefined();
}

Handle<Value> Connection::Send(const Arguments& args) {
HandleScope scope;
Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());
Expand Down
5 changes: 5 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Connection : public EventEmitter {
static v8::Handle<v8::Value> ReadPause(const v8::Arguments& args);
static v8::Handle<v8::Value> ReadResume(const v8::Arguments& args);
static v8::Handle<v8::Value> SetTimeout(const v8::Arguments& args);
static v8::Handle<v8::Value> SetNoDelay(const v8::Arguments& args);

static v8::Handle<v8::Value> ReadyStateGetter(v8::Local<v8::String> _,
const v8::AccessorInfo& info);
Expand Down Expand Up @@ -70,6 +71,10 @@ class Connection : public EventEmitter {
evcom_stream_reset_timeout(&stream_, timeout);
}

void SetNoDelay(bool no_delay) {
evcom_stream_set_no_delay(&stream_, no_delay);
}

virtual void OnConnect();
virtual void OnReceive(const void *buf, size_t len);
virtual void OnEOF();
Expand Down
2 changes: 2 additions & 0 deletions test/mjsunit/test-tcp-pingpong.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ function pingPongTest (port, host, on_complete) {
assertEquals(socket.remoteAddress, "127.0.0.1");

socket.setEncoding("utf8");
socket.setNoDelay();
socket.timeout = 0;

socket.addListener("receive", function (data) {
puts("server got: " + JSON.stringify(data));
assertEquals("open", socket.readyState);
assertTrue(count <= N);
if (/PING/.exec(data)) {
Expand Down

0 comments on commit e0ec003

Please sign in to comment.