Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address comments
  • Loading branch information
kjin committed Dec 20, 2017
commit 34f36bb5d5bc2592d4bef052e05e9cda579d6fdf
4 changes: 2 additions & 2 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ added: v8.4.0

#### clienthttp2session.ref()
<!-- YAML
added: ??? (TODO)
added: REPLACEME
-->

Calls [`ref()`][`net.Socket.prototype.ref`] on this `ClientHttp2Session`
Expand Down Expand Up @@ -620,7 +620,7 @@ they respectively default to:

#### clienthttp2session.unref()
<!-- YAML
added: ??? (TODO)
added: REPLACEME
-->

Calls [`unref()`][`net.Socket.prototype.unref`] on this `ClientHttp2Session`
Expand Down
10 changes: 8 additions & 2 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1225,13 +1225,19 @@ class ClientHttp2Session extends Http2Session {

ref() {
if (this[kSocket]) {
this[kSocket].ref();
assert(this[kSocket]._handle);
if (typeof this[kSocket]._handle.ref === 'function') {
this[kSocket].ref();
}
}
}

unref() {
if (this[kSocket]) {
this[kSocket].unref();
assert(this[kSocket]._handle);
if (typeof this[kSocket]._handle.unref === 'function') {
this[kSocket].unref();
}
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion test/parallel/test-http2-client-session-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const http2 = require('http2');
const { PassThrough } = require('stream');

function isActiveHandle(h) {
return process._getActiveHandles().indexOf(h) !== -1;
Expand All @@ -15,15 +16,29 @@ function isActiveHandle(h) {
const server = http2.createServer();
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const client = http2.connect(`http://localhost:${port}`);

let client = http2.connect(`http://localhost:${port}`);
assert.ok(isActiveHandle(client[kSocket]));
client.unref();
assert.ok(!isActiveHandle(client[kSocket]));
client.ref();
assert.ok(isActiveHandle(client[kSocket]));
client.destroy();
assert.ok(!isActiveHandle(client[kSocket]));
// Ensure that calling these methods don't throw after session is destroyed.
assert.doesNotThrow(client.unref.bind(client));
assert.doesNotThrow(client.ref.bind(client));
assert.ok(!isActiveHandle(client[kSocket]));

server.close();

// Try with generic DuplexStream
client = http2.connect(`http://localhost:${port}`, {
createConnection: common.mustCall(() => PassThrough())
});
assert.doesNotThrow(client.unref.bind(client));
assert.doesNotThrow(client.ref.bind(client));
client.destroy();
assert.doesNotThrow(client.unref.bind(client));
assert.doesNotThrow(client.ref.bind(client));
}));