Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: add Http2Stream.bufferSize #23711

Closed
wants to merge 2 commits into from
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
10 changes: 10 additions & 0 deletions doc/api/http2.md
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,15 @@ added: v8.4.0
Set to `true` if the `Http2Stream` instance was aborted abnormally. When set,
the `'aborted'` event will have been emitted.

### http2stream.bufferSize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
### http2stream.bufferSize
#### http2stream.bufferSize

<!-- YAML
added: REPLACEME
-->
* {number}

This property shows the number of characters currently buffered to be written.
See [`net.Socket.bufferSize`][] for details.

#### http2stream.close(code[, callback])
<!-- YAML
added: v8.4.0
Expand Down Expand Up @@ -3418,6 +3427,7 @@ following additional properties:
[`net.Socket.prototype.ref()`]: net.html#net_socket_ref
[`net.Socket.prototype.unref()`]: net.html#net_socket_unref
[`net.connect()`]: net.html#net_net_connect
[`net.Socket.bufferSize`]: net.html#net_socket_buffersize
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Oct 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: should go after [`net.Socket`]: net.html#net_class_net_socket (in ASCII sort order).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Done.

[`request.socket.getPeerCertificate()`]: tls.html#tls_tlssocket_getpeercertificate_detailed
[`response.end()`]: #http2_response_end_data_encoding_callback
[`response.setHeader()`]: #http2_response_setheader_name_value
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,12 @@ class Http2Stream extends Duplex {
return `Http2Stream ${util.format(obj)}`;
}

get bufferSize() {
// `bufferSize` properties of `net.Socket` are `undefined` when
// their `_handle` are falsy. Here we avoid the behavior.
return this[kState].writeQueueSize + this.writableLength;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At best this is likely an approximation with a high degree of accuracy at any given time. It's likely good enough :-)

What do you think @addaleax?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d guess it’s good enough, yes :)

}

get endAfterHeaders() {
return this[kState].endAfterHeaders;
}
Expand Down
51 changes: 51 additions & 0 deletions test/parallel/test-http2-buffersize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';

const { mustCall, hasCrypto, skip } = require('../common');
if (!hasCrypto)
skip('missing crypto');
const assert = require('assert');
const { createServer, connect } = require('http2');
const Countdown = require('../common/countdown');

// This test ensures that `bufferSize` of Http2Session and Http2Stream work
// as expected.
{
const SOCKETS = 2;
const TIMES = 10;
const BUFFER_SIZE = 30;
const server = createServer();

// Other `bufferSize` tests for net module and tls module
// don't assert `bufferSize` of server-side sockets.
server.on('stream', mustCall((stream) => {
stream.on('data', mustCall());
stream.on('end', mustCall());
}, SOCKETS));

server.listen(0, mustCall(() => {
const authority = `http://localhost:${server.address().port}`;
const client = connect(authority);
const countdown = new Countdown(SOCKETS, () => {
client.close();
server.close();
});

client.once('connect', mustCall());

for (let j = 0; j < SOCKETS; j += 1) {
const stream = client.request({ ':method': 'POST' });
stream.on('data', () => {});
stream.on('close', mustCall(() => {
countdown.dec();
}));

for (let i = 0; i < TIMES; i += 1) {
stream.write(Buffer.allocUnsafe(BUFFER_SIZE), mustCall());
const expectedSocketBufferSize = BUFFER_SIZE * (i + 1);
assert.strictEqual(stream.bufferSize, expectedSocketBufferSize);
}
stream.end();
stream.close();
}
}));
}
72 changes: 72 additions & 0 deletions test/parallel/test-tls-streamwrap-buffersize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const fixtures = require('../common/fixtures');
const makeDuplexPair = require('../common/duplexpair');
const tls = require('tls');
const net = require('net');

// This test ensures that `bufferSize` also works for those tlsSockets
// created from `socket` of `Duplex`, with which, TLSSocket will wrap
// sockets in `StreamWrap`.
{
const iter = 10;

function createDuplex(port) {
const { clientSide, serverSide } = makeDuplexPair();

return new Promise((resolve, reject) => {
const socket = net.connect({
port,
}, common.mustCall(() => {
clientSide.pipe(socket);
socket.pipe(clientSide);
clientSide.on('close', common.mustCall(() => socket.destroy()));
socket.on('close', common.mustCall(() => clientSide.destroy()));

resolve(serverSide);
}));
});
}

const server = tls.createServer({
key: fixtures.readKey('agent2-key.pem'),
cert: fixtures.readKey('agent2-cert.pem')
}, common.mustCall((socket) => {
let str = '';
socket.setEncoding('utf-8');
socket.on('data', (chunk) => { str += chunk; });

socket.on('end', common.mustCall(() => {
assert.strictEqual(str, 'a'.repeat(iter - 1));
server.close();
}));
}));

server.listen(0, common.mustCall(() => {
const { port } = server.address();
createDuplex(port).then((socket) => {
const client = tls.connect({
socket,
rejectUnauthorized: false,
}, common.mustCall(() => {
assert.strictEqual(client.bufferSize, 0);

for (let i = 1; i < iter; i++) {
client.write('a');
assert.strictEqual(client.bufferSize, i + 1);
}

// It seems that tlsSockets created from sockets of `Duplex` emit no
// "finish" events. We use "end" event instead.
client.on('end', common.mustCall(() => {
assert.strictEqual(client.bufferSize, undefined);
}));

client.end();
}));
});
}));
}