Skip to content

Commit

Permalink
Added interval and count options to setKeepAlive.
Browse files Browse the repository at this point in the history
The OS defaults for these are too extreme if you want to run a
chat server and know if someone disconnects. Changes go along
with libuv changes.
  • Loading branch information
James Hartig committed Mar 5, 2013
1 parent f0f87d8 commit af16bc6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
15 changes: 9 additions & 6 deletions doc/api/net.markdown
Expand Up @@ -388,16 +388,19 @@ algorithm, they buffer data before sending it off. Setting `true` for
`noDelay` will immediately fire off data each time `socket.write()` is called.
`noDelay` defaults to `true`.

### socket.setKeepAlive([enable], [initialDelay])
### socket.setKeepAlive([enable], [initialDelay], [probeInterval], [failureCount])

Enable/disable keep-alive functionality, and optionally set the initial
delay before the first keepalive probe is sent on an idle socket.
Enable/disable keep-alive functionality, and optionally set keep-alive options.
Options default to `0`, which leaves the value unchanged from the default
(or previous) setting.
`enable` defaults to `false`.

Set `initialDelay` (in milliseconds) to set the delay between the last
data packet received and the first keepalive probe. Setting 0 for
initialDelay will leave the value unchanged from the default
(or previous) setting. Defaults to `0`.
data packet received and the first keepalive probe.
Set `probeInterval` (in milliseconds) to set the interval between the
keep-alive probes sent after the initial probe.
Set `failureCount` to set the maximum number of probes that can fail before
the OS closes the connection. Ignored on Windows.

### socket.address()

Expand Down
4 changes: 2 additions & 2 deletions lib/net.js
Expand Up @@ -294,9 +294,9 @@ Socket.prototype.setNoDelay = function(enable) {
};


Socket.prototype.setKeepAlive = function(setting, msecs) {
Socket.prototype.setKeepAlive = function(setting, msecs, interval, count) {
if (this._handle && this._handle.setKeepAlive)
this._handle.setKeepAlive(setting, ~~(msecs / 1000));
this._handle.setKeepAlive(setting, ~~(msecs / 1000), ~~(interval / 1000), ~~count);
};


Expand Down
4 changes: 3 additions & 1 deletion src/tcp_wrap.cc
Expand Up @@ -230,8 +230,10 @@ Handle<Value> TCPWrap::SetKeepAlive(const Arguments& args) {

int enable = args[0]->Int32Value();
unsigned int delay = args[1]->Uint32Value();
unsigned int interval = args[2]->Uint32Value();
unsigned int count = args[3]->Uint32Value();

int r = uv_tcp_keepalive(&wrap->handle_, enable, delay);
int r = uv_tcp_keepalive(&wrap->handle_, enable, delay, interval, count);
if (r)
SetErrno(uv_last_error(uv_default_loop()));

Expand Down

0 comments on commit af16bc6

Please sign in to comment.