Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added more options to setKeepAlive

  • Loading branch information...
commit c3647709917ee6d08c75b7bda9a648b095983ec0 1 parent f44d0b9
@fastest963 authored
View
4 deps/uv/include/uv.h
@@ -484,9 +484,11 @@ UV_EXTERN int uv_tcp_nodelay(uv_tcp_t* handle, int enable);
/* Enable/disable TCP keep-alive.
*
* `ms` is the initial delay in seconds, ignored when `enable` is zero.
+ * interval is the keep alive probe interval after the initial delay
+ * count is the number of keep-alives that fail before thinking the socket is dead
*/
UV_EXTERN int uv_tcp_keepalive(uv_tcp_t* handle, int enable,
- unsigned int delay);
+ unsigned int delay, unsigned int interval, unsigned int count);
/*
* This setting applies to Windows only.
View
2  deps/uv/src/unix/internal.h
@@ -171,7 +171,7 @@ int uv__connect(uv_connect_t* req, uv_stream_t* stream, struct sockaddr* addr,
/* tcp */
int uv_tcp_listen(uv_tcp_t* tcp, int backlog, uv_connection_cb cb);
int uv__tcp_nodelay(uv_tcp_t* handle, int enable);
-int uv__tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay);
+int uv__tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay, unsigned int interval, unsigned int count);
/* pipe */
int uv_pipe_listen(uv_pipe_t* handle, int backlog, uv_connection_cb cb);
View
2  deps/uv/src/unix/stream.c
@@ -102,7 +102,7 @@ int uv__stream_open(uv_stream_t* stream, int fd, int flags) {
/* TODO Use delay the user passed in. */
if ((stream->flags & UV_TCP_KEEPALIVE) &&
- uv__tcp_keepalive((uv_tcp_t*)stream, 1, 60)) {
+ uv__tcp_keepalive((uv_tcp_t*)stream, 1, 60, 60, 8)) {
return -1;
}
}
View
35 deps/uv/src/unix/tcp.c
@@ -255,7 +255,8 @@ int uv__tcp_nodelay(uv_tcp_t* handle, int enable) {
}
-int uv__tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay) {
+int uv__tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay,
+ unsigned int interval, unsigned int count) {
if (setsockopt(handle->fd,
SOL_SOCKET,
SO_KEEPALIVE,
@@ -287,25 +288,47 @@ int uv__tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay) {
}
#endif
+#ifdef TCP_KEEPINTVL
+ if (enable && interval && setsockopt(handle->fd,
+ IPPROTO_TCP,
+ TCP_KEEPINTVL,
+ &interval,
+ sizeof interval) == -1) {
+ uv__set_sys_error(handle->loop, errno);
+ return -1;
+ }
+#endif
+
+#ifdef TCP_KEEPCNT
+ if (enable && count && setsockopt(handle->fd,
+ IPPROTO_TCP,
+ TCP_KEEPCNT,
+ &count,
+ sizeof count) == -1) {
+ uv__set_sys_error(handle->loop, errno);
+ return -1;
+ }
+#endif
+
return 0;
}
-
int uv_tcp_nodelay(uv_tcp_t* handle, int enable) {
if (handle->fd != -1 && uv__tcp_nodelay(handle, enable))
return -1;
- if (enable)
+ if (enable) {
+
handle->flags |= UV_TCP_NODELAY;
- else
+ } else
handle->flags &= ~UV_TCP_NODELAY;
return 0;
}
-int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay) {
- if (handle->fd != -1 && uv__tcp_keepalive(handle, enable, delay))
+int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay, unsigned int interval, unsigned int count) {
+ if (handle->fd != -1 && uv__tcp_keepalive(handle, enable, delay, interval, count))
return -1;
if (enable)
View
12 deps/uv/src/win/tcp.c
@@ -59,7 +59,8 @@ static int uv__tcp_nodelay(uv_tcp_t* handle, SOCKET socket, int enable) {
}
-static int uv__tcp_keepalive(uv_tcp_t* handle, SOCKET socket, int enable, unsigned int delay) {
+static int uv__tcp_keepalive(uv_tcp_t* handle, SOCKET socket, int enable, unsigned int delay,
+ unsigned int interval, unsigned int count) {
if (setsockopt(socket,
SOL_SOCKET,
SO_KEEPALIVE,
@@ -78,6 +79,9 @@ static int uv__tcp_keepalive(uv_tcp_t* handle, SOCKET socket, int enable, unsign
return -1;
}
+ /* interval and count cannot be applied without registry
+ changes and system reboot */
+
return 0;
}
@@ -132,7 +136,7 @@ static int uv_tcp_set_socket(uv_loop_t* loop, uv_tcp_t* handle,
/* TODO: Use stored delay. */
if ((handle->flags & UV_HANDLE_TCP_KEEPALIVE) &&
- uv__tcp_keepalive(handle, socket, 1, 60)) {
+ uv__tcp_keepalive(handle, socket, 1, 60, 60, 8)) {
return -1;
}
@@ -1058,9 +1062,9 @@ int uv_tcp_nodelay(uv_tcp_t* handle, int enable) {
}
-int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay) {
+int uv_tcp_keepalive(uv_tcp_t* handle, int enable, unsigned int delay, unsigned int interval, unsigned int count) {
if (handle->socket != INVALID_SOCKET &&
- uv__tcp_keepalive(handle, handle->socket, enable, delay)) {
+ uv__tcp_keepalive(handle, handle->socket, enable, interval, delay)) {
return -1;
}
View
2  deps/uv/test/test-tcp-flags.c
@@ -39,7 +39,7 @@ TEST_IMPL(tcp_flags) {
r = uv_tcp_nodelay(&handle, 1);
ASSERT(r == 0);
- r = uv_tcp_keepalive(&handle, 1, 60);
+ r = uv_tcp_keepalive(&handle, 1, 60, 60, 8);
ASSERT(r == 0);
uv_close((uv_handle_t*)&handle, NULL);
View
19 doc/api/net.markdown
@@ -339,14 +339,17 @@ 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 `socket.write()` is called.
-#### socket.setKeepAlive(enable=false, [initialDelay])
-
-Enable/disable keep-alive functionality, and optionally set the initial
-delay before the first keepalive probe is sent on an idle socket.
-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.
+#### socket.setKeepAlive(enable=false, [initialDelay], [probeInterval], [failureCount])
+
+Enable/disable keep-alive functionality, and optionally override default
+keep-alive options. Set `initialDelay` (in milliseconds) to set the delay
+between the last 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.
+probeInterval and failureCount do not work on Windows OS. Setting 0 for
+initialDelay, probeInterval, or failureCount will leave the value unchanged
+from the default (or previous) setting.
#### socket.address()
View
4 lib/net.js
@@ -158,9 +158,9 @@ Socket.prototype.setNoDelay = function() {
};
-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);
};
View
4 src/tcp_wrap.cc
@@ -267,8 +267,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()));
Please sign in to comment.
Something went wrong with that request. Please try again.