Skip to content

Commit 75ab197

Browse files
authored
Active keepAlive and noDelay after connection (#15)
1 parent 0dc4144 commit 75ab197

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

binding.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,10 +924,12 @@ bare_tcp_address(js_env_t *env, js_callback_info_t *info) {
924924
err = js_create_int32(env, ntohs(addr_in6->sin6_port), &result_port);
925925
assert(err == 0);
926926
} else {
927-
err = js_get_null(env, &result);
927+
err = UV_EAI_ADDRFAMILY;
928+
929+
err = js_throw_error(env, uv_err_name(err), uv_strerror(err));
928930
assert(err == 0);
929931

930-
return result;
932+
return NULL;
931933
}
932934

933935
err = js_set_named_property(env, result, "address", result_address);

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ exports.Socket = class TCPSocket extends Duplex {
1919

2020
this._allowHalfOpen = allowHalfOpen
2121

22+
this._keepAlive = 0
23+
this._keepAliveInitialDelay = 0
24+
this._noDelay = 0
25+
2226
this._localAddress = null
2327
this._remoteAddress = null
2428

@@ -171,8 +175,11 @@ exports.Socket = class TCPSocket extends Duplex {
171175
try {
172176
binding.connect(this._handle, port, host, family)
173177

174-
if (keepAlive) this.setKeepAlive(keepAlive, keepAliveInitialDelay)
175-
if (noDelay) this.setNoDelay()
178+
if (keepAlive) {
179+
this._keepAlive = keepAlive
180+
this._keepAliveInitialDelay = keepAliveInitialDelay
181+
}
182+
if (noDelay) this._noDelay = noDelay
176183
if (timeout) this.setTimeout(timeout)
177184

178185
if (onconnect) this.once('connect', onconnect)
@@ -333,6 +340,9 @@ exports.Socket = class TCPSocket extends Duplex {
333340
return
334341
}
335342

343+
if (this._keepAlive) this.setKeepAlive(this._keepAlive, this._keepAliveInitialDelay)
344+
if (this._noDelay) this.setNoDelay()
345+
336346
this._localAddress = binding.address(this._handle, true)
337347
this._remoteAddress = binding.address(this._handle, false)
338348

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ test('address getters', async (t) => {
102102

103103
const { port: serverPort } = server.address()
104104

105-
const socket = createConnection(serverPort)
105+
const socket = createConnection({ port: serverPort, noDelay: true, keepAlive: 1000 })
106106
.on('connect', () => {
107107
t.is(socket.localAddress, '127.0.0.1')
108108
t.is(socket.localFamily, 'IPv4')

0 commit comments

Comments
 (0)