Skip to content

Commit e0ec003

Browse files
committed
Add connection.setNoDelay() to disable Nagle algorithm.
1 parent 2df13c7 commit e0ec003

File tree

8 files changed

+58
-3
lines changed

8 files changed

+58
-3
lines changed

deps/evcom/evcom.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,15 @@ evcom_stream_reset_timeout (evcom_stream *stream, float timeout)
12051205
}
12061206
}
12071207

1208+
void
1209+
evcom_stream_set_no_delay (evcom_stream *stream, int no_delay)
1210+
{
1211+
if (DUPLEX(stream)) {
1212+
int flags = no_delay ? 1 : 0;
1213+
setsockopt(stream->recvfd, IPPROTO_TCP, TCP_NODELAY, (void *)&flags, sizeof(flags));
1214+
}
1215+
}
1216+
12081217
void
12091218
evcom_stream_attach (EV_P_ evcom_stream *stream)
12101219
{

deps/evcom/evcom.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ void evcom_stream_detach (evcom_stream *);
192192
void evcom_stream_read_resume (evcom_stream *);
193193
void evcom_stream_read_pause (evcom_stream *);
194194
void evcom_stream_reset_timeout (evcom_stream *, float timeout);
195+
void evcom_stream_set_no_delay (evcom_stream *, int no_delay);
195196
void evcom_stream_write (evcom_stream *, const char *str, size_t len);
196197
/* Once the write buffer is drained, evcom_stream_close will shutdown the
197198
* writing end of the stream and will close the read end once the server

doc/api.html

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,16 @@ <h4 id="_tt_node_tcp_connection_tt"><tt>node.tcp.Connection</tt></h4>
17121712
</p>
17131713
<div class="paragraph"><p>If <tt>timeout</tt> is 0, then the idle timeout is disabled.</p></div>
17141714
</dd>
1715+
<dt class="hdlist1">
1716+
<tt>connection.setNoDelay(noDelay=true)</tt>
1717+
</dt>
1718+
<dd>
1719+
<p>
1720+
Disables the Nagle algorithm. By default TCP connections use the Nagle
1721+
algorithm, they buffer data before sending it off. Setting <tt>noDelay</tt> will
1722+
immediately fire off data each time <tt>connection.send()</tt> is called.
1723+
</p>
1724+
</dd>
17151725
</dl></div>
17161726
<h3 id="_dns">DNS</h3><div style="clear:left"></div>
17171727
<div class="paragraph"><p>Here is an example of which resolves <tt>"www.google.com"</tt> then reverse
@@ -1922,7 +1932,7 @@ <h2 id="_extension_api">Extension API</h2>
19221932
<div id="footer">
19231933
<div id="footer-text">
19241934
Version 0.1.11<br />
1925-
Last updated 2009-09-21 12:26:35 CEST
1935+
Last updated 2009-09-23 15:35:53 CEST
19261936
</div>
19271937
</div>
19281938
</body>

doc/api.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,10 @@ of 60 seconds (60000 ms).
10811081
+
10821082
If +timeout+ is 0, then the idle timeout is disabled.
10831083

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

10851089
=== DNS
10861090

doc/node.1

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.\" Title: node
22
.\" Author:
33
.\" Generator: DocBook XSL Stylesheets v1.73.2 <http://docbook.sf.net/>
4-
.\" Date: 09/21/2009
4+
.\" Date: 09/23/2009
55
.\" Manual:
66
.\" Source:
77
.\"
8-
.TH "NODE" "1" "09/21/2009" "" ""
8+
.TH "NODE" "1" "09/23/2009" "" ""
99
.\" disable hyphenation
1010
.nh
1111
.\" disable justification (adjust text to left margin only)
@@ -1639,6 +1639,15 @@ If
16391639
timeout
16401640
is 0, then the idle timeout is disabled\.
16411641
.RE
1642+
.PP
1643+
connection\.setNoDelay(noDelay=true)
1644+
.RS 4
1645+
Disables the Nagle algorithm\. By default TCP connections use the Nagle algorithm, they buffer data before sending it off\. Setting
1646+
noDelay
1647+
will immediately fire off data each time
1648+
connection\.send()
1649+
is called\.
1650+
.RE
16421651
.RE
16431652
.SS "DNS"
16441653
Here is an example of which resolves "www\.google\.com" then reverse resolves the IP addresses which are returned\.

src/net.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void Connection::Initialize(v8::Handle<v8::Object> target) {
6464
NODE_SET_PROTOTYPE_METHOD(constructor_template, "readPause", ReadPause);
6565
NODE_SET_PROTOTYPE_METHOD(constructor_template, "readResume", ReadResume);
6666
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setTimeout", SetTimeout);
67+
NODE_SET_PROTOTYPE_METHOD(constructor_template, "setNoDelay", SetNoDelay);
6768

6869
constructor_template->PrototypeTemplate()->SetAccessor(
6970
READY_STATE_SYMBOL,
@@ -332,6 +333,20 @@ Handle<Value> Connection::ForceClose(const Arguments& args) {
332333
return Undefined();
333334
}
334335

336+
Handle<Value> Connection::SetNoDelay(const Arguments& args) {
337+
HandleScope scope;
338+
Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());
339+
340+
bool no_delay = true;
341+
if (args.Length() > 0) {
342+
no_delay = args[0]->IsTrue();
343+
}
344+
345+
connection->SetNoDelay(no_delay);
346+
347+
return Undefined();
348+
}
349+
335350
Handle<Value> Connection::Send(const Arguments& args) {
336351
HandleScope scope;
337352
Connection *connection = ObjectWrap::Unwrap<Connection>(args.Holder());

src/net.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Connection : public EventEmitter {
2828
static v8::Handle<v8::Value> ReadPause(const v8::Arguments& args);
2929
static v8::Handle<v8::Value> ReadResume(const v8::Arguments& args);
3030
static v8::Handle<v8::Value> SetTimeout(const v8::Arguments& args);
31+
static v8::Handle<v8::Value> SetNoDelay(const v8::Arguments& args);
3132

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

74+
void SetNoDelay(bool no_delay) {
75+
evcom_stream_set_no_delay(&stream_, no_delay);
76+
}
77+
7378
virtual void OnConnect();
7479
virtual void OnReceive(const void *buf, size_t len);
7580
virtual void OnEOF();

test/mjsunit/test-tcp-pingpong.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ function pingPongTest (port, host, on_complete) {
1717
assertEquals(socket.remoteAddress, "127.0.0.1");
1818

1919
socket.setEncoding("utf8");
20+
socket.setNoDelay();
2021
socket.timeout = 0;
2122

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

0 commit comments

Comments
 (0)