Skip to content

Commit d2cff34

Browse files
committed
Idle timeout changes
- setTimeout should active the timeout too. (test-net-set-timeout tests this.) - 'timeout' event is not automatically followed by an 'error' event. That is the user is now responsible for destroying the stream if there is an idle timeout.
1 parent f7ff548 commit d2cff34

4 files changed

Lines changed: 57 additions & 15 deletions

File tree

doc/api.markdown

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,8 +2124,11 @@ call `stream.end()` when this event is emitted.
21242124

21252125
`function () { }`
21262126

2127-
Emitted if the stream times out from inactivity. The
2128-
`'close'` event will be emitted immediately following this event.
2127+
Emitted if the stream times out from inactivity. This is only to notify that
2128+
the stream has been idle. The user must manually close the connection.
2129+
2130+
See also: `stream.setTimeout()`
2131+
21292132

21302133
### Event: 'drain'
21312134

@@ -2233,10 +2236,13 @@ Resumes reading after a call to `pause()`.
22332236
### stream.setTimeout(timeout)
22342237

22352238
Sets the stream to timeout after `timeout` milliseconds of inactivity on
2236-
the stream. By default all `net.Stream` objects have a timeout of 60
2237-
seconds (60000 ms).
2239+
the stream. By default `net.Stream` do not have a timeout.
2240+
2241+
When an idle timeout is triggered the stream will receive a `'timeout'`
2242+
event but the connection will not be severed. The user must manually `end()`
2243+
or `destroy()` the stream.
22382244

2239-
If `timeout` is 0, then the idle timeout is disabled.
2245+
If `timeout` is 0, then the existing idle timeout is disabled.
22402246

22412247
### stream.setNoDelay(noDelay=true)
22422248

lib/net.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@ var timeout = new (function () {
146146
remove(first);
147147
assert(first != peek(list));
148148
first.emit('timeout');
149-
first.destroy(new Error('idle timeout'));
150149
}
151150
}
152151
debug(msecs + ' list empty');
@@ -816,7 +815,10 @@ Stream.prototype.setKeepAlive = function (enable, time) {
816815
};
817816

818817
Stream.prototype.setTimeout = function (msecs) {
819-
timeout.enroll(this, msecs);
818+
if (msecs > 0) {
819+
timeout.enroll(this, msecs);
820+
if (this.fd) { timeout.active(this); }
821+
}
820822
};
821823

822824

test/pummel/test-tcp-timeout.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@ starttime = null;
55
timeouttime = null;
66
timeout = 1000;
77

8-
gotError = false
9-
108
var echo_server = net.createServer(function (socket) {
119
socket.setTimeout(timeout);
1210

1311
socket.addListener("timeout", function () {
1412
puts("server timeout");
1513
timeouttime = new Date;
1614
p(timeouttime);
15+
socket.destroy();
1716
});
1817

1918
socket.addListener("error", function (e) {
20-
assert.ok(e instanceof Error);
21-
gotError = true;
19+
throw new Error("Server side socket should not get error. We disconnect willingly.");
2220
})
2321

2422
socket.addListener("data", function (d) {
@@ -59,8 +57,7 @@ client.addListener("data", function (chunk) {
5957
});
6058

6159
client.addListener("timeout", function () {
62-
puts("client timeout - this shouldn't happen");
63-
assert.ok(false);
60+
throw new Error("client timeout - this shouldn't happen");
6461
});
6562

6663
client.addListener("end", function () {
@@ -84,6 +81,4 @@ process.addListener("exit", function () {
8481

8582
// Allow for 800 milliseconds more
8683
assert.ok(diff < timeout + 800);
87-
88-
assert.ok(gotError);
8984
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require("../common");
2+
var sys = require('sys'),
3+
http = require('http');
4+
5+
server = http.createServer(function (req, res) {
6+
sys.puts('got request. setting 1 second timeout');
7+
req.connection.setTimeout(500);
8+
9+
req.connection.addListener('timeout', function(){
10+
sys.debug("TIMEOUT");
11+
12+
var body="timeout\n";
13+
res.writeHead(200, {
14+
'Content-Type': 'text/plain',
15+
'Content-Length': body.length,
16+
'Connection':'close'
17+
});
18+
res.end(body);
19+
req.connection.end();
20+
server.close();
21+
});
22+
});
23+
server.listen(8000);
24+
25+
26+
server.addListener('listening', function () {
27+
sys.puts('Server running at http://127.0.0.1:8000/');
28+
29+
errorTimer =setTimeout(function () {
30+
throw new Error('Timeout was not sucessful');
31+
}, 2000);
32+
33+
http.cat('http://localhost:8000/', 'utf8', function (err, content) {
34+
clearTimeout(errorTimer);
35+
if (err) throw err;
36+
sys.puts('HTTP REQUEST COMPLETE (this is good)');
37+
sys.puts(content);
38+
});
39+
});

0 commit comments

Comments
 (0)