Skip to content

Commit

Permalink
stream: move process.binding('stream_wrap') to internalBinding
Browse files Browse the repository at this point in the history
PR-URL: #22345
Refs: #22160
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Jon Moss <me@jonathanmoss.me>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
jasnell committed Aug 19, 2018
1 parent 7108893 commit 884b23d
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 122 deletions.
114 changes: 57 additions & 57 deletions benchmark/net/tcp-raw-c2s.js
Expand Up @@ -12,14 +12,15 @@ const bench = common.createBenchmark(main, {
len: [102400, 1024 * 1024 * 16],
type: ['utf', 'asc', 'buf'],
dur: [5]
});

const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap;
const PORT = common.PORT;
}, { flags: [ '--expose-internals', '--no-warnings' ] });

function main({ dur, len, type }) {
const { internalBinding } = require('internal/test/binding');
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const { TCPConnectWrap } = process.binding('tcp_wrap');
const { WriteWrap } = internalBinding('stream_wrap');
const PORT = common.PORT;

const serverHandle = new TCP(TCPConstants.SERVER);
var err = serverHandle.bind('127.0.0.1', PORT);
if (err)
Expand Down Expand Up @@ -58,71 +59,70 @@ function main({ dur, len, type }) {
};

client(type, len);
}


function fail(err, syscall) {
throw util._errnoException(err, syscall);
}

function client(type, len) {
var chunk;
switch (type) {
case 'buf':
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
chunk = 'x'.repeat(len);
break;
default:
throw new Error(`invalid type: ${type}`);
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}

const clientHandle = new TCP(TCPConstants.SOCKET);
const connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);

if (err)
fail(err, 'connect');

clientHandle.readStart();

connectReq.oncomplete = function(err) {
if (err)
fail(err, 'connect');

while (clientHandle.writeQueueSize === 0)
write();
};

function write() {
const writeReq = new WriteWrap();
writeReq.oncomplete = afterWrite;
var err;
function client(type, len) {
var chunk;
switch (type) {
case 'buf':
err = clientHandle.writeBuffer(writeReq, chunk);
chunk = Buffer.alloc(len, 'x');
break;
case 'utf':
err = clientHandle.writeUtf8String(writeReq, chunk);
chunk = 'ü'.repeat(len / 2);
break;
case 'asc':
err = clientHandle.writeAsciiString(writeReq, chunk);
chunk = 'x'.repeat(len);
break;
default:
throw new Error(`invalid type: ${type}`);
}

if (err)
fail(err, 'write');
}
const clientHandle = new TCP(TCPConstants.SOCKET);
const connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);

function afterWrite(err, handle) {
if (err)
fail(err, 'write');
fail(err, 'connect');

clientHandle.readStart();

connectReq.oncomplete = function(err) {
if (err)
fail(err, 'connect');

while (clientHandle.writeQueueSize === 0)
write();
while (clientHandle.writeQueueSize === 0)
write();
};

function write() {
const writeReq = new WriteWrap();
writeReq.oncomplete = afterWrite;
var err;
switch (type) {
case 'buf':
err = clientHandle.writeBuffer(writeReq, chunk);
break;
case 'utf':
err = clientHandle.writeUtf8String(writeReq, chunk);
break;
case 'asc':
err = clientHandle.writeAsciiString(writeReq, chunk);
break;
}

if (err)
fail(err, 'write');
}

function afterWrite(err, handle) {
if (err)
fail(err, 'write');

while (clientHandle.writeQueueSize === 0)
write();
}
}
}
21 changes: 12 additions & 9 deletions benchmark/net/tcp-raw-pipe.js
Expand Up @@ -12,18 +12,21 @@ const bench = common.createBenchmark(main, {
len: [102400, 1024 * 1024 * 16],
type: ['utf', 'asc', 'buf'],
dur: [5]
}, {
flags: [ '--expose-internals', '--no-warnings' ]
});

function fail(err, syscall) {
throw util._errnoException(err, syscall);
}

const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap;
const PORT = common.PORT;

function main({ dur, len, type }) {
const { internalBinding } = require('internal/test/binding');
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const { TCPConnectWrap } = process.binding('tcp_wrap');
const { WriteWrap } = internalBinding('stream_wrap');
const PORT = common.PORT;

function fail(err, syscall) {
throw util._errnoException(err, syscall);
}

// Server
const serverHandle = new TCP(TCPConstants.SERVER);
var err = serverHandle.bind('127.0.0.1', PORT);
Expand Down
81 changes: 42 additions & 39 deletions benchmark/net/tcp-raw-s2c.js
Expand Up @@ -12,14 +12,17 @@ const bench = common.createBenchmark(main, {
len: [102400, 1024 * 1024 * 16],
type: ['utf', 'asc', 'buf'],
dur: [5]
}, {
flags: [ '--expose-internals', '--no-warnings' ]
});

const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap;
const PORT = common.PORT;

function main({ dur, len, type }) {
const { internalBinding } = require('internal/test/binding');
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const { TCPConnectWrap } = process.binding('tcp_wrap');
const { WriteWrap } = internalBinding('stream_wrap');
const PORT = common.PORT;

const serverHandle = new TCP(TCPConstants.SERVER);
var err = serverHandle.bind('127.0.0.1', PORT);
if (err)
Expand Down Expand Up @@ -89,42 +92,42 @@ function main({ dur, len, type }) {
};

client(dur);
}

function fail(err, syscall) {
throw util._errnoException(err, syscall);
}
function fail(err, syscall) {
throw util._errnoException(err, syscall);
}

function client(dur) {
const clientHandle = new TCP(TCPConstants.SOCKET);
const connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);

if (err)
fail(err, 'connect');

connectReq.oncomplete = function() {
var bytes = 0;
clientHandle.onread = function(nread, buffer) {
// we're not expecting to ever get an EOF from the client.
// just lots of data forever.
if (nread < 0)
fail(nread, 'read');

// don't slice the buffer. the point of this is to isolate, not
// simulate real traffic.
bytes += buffer.length;
};
function client(dur) {
const clientHandle = new TCP(TCPConstants.SOCKET);
const connectReq = new TCPConnectWrap();
const err = clientHandle.connect(connectReq, '127.0.0.1', PORT);

clientHandle.readStart();

// the meat of the benchmark is right here:
bench.start();
if (err)
fail(err, 'connect');

setTimeout(function() {
// report in Gb/sec
bench.end((bytes * 8) / (1024 * 1024 * 1024));
process.exit(0);
}, dur * 1000);
};
connectReq.oncomplete = function() {
var bytes = 0;
clientHandle.onread = function(nread, buffer) {
// we're not expecting to ever get an EOF from the client.
// just lots of data forever.
if (nread < 0)
fail(nread, 'read');

// don't slice the buffer. the point of this is to isolate, not
// simulate real traffic.
bytes += buffer.length;
};

clientHandle.readStart();

// the meat of the benchmark is right here:
bench.start();

setTimeout(function() {
// report in Gb/sec
bench.end((bytes * 8) / (1024 * 1024 * 1024));
process.exit(0);
}, dur * 1000);
};
}
}
7 changes: 6 additions & 1 deletion lib/internal/bootstrap/node.js
Expand Up @@ -344,7 +344,12 @@
// that are whitelisted for access via process.binding()... this is used
// to provide a transition path for modules that are being moved over to
// internalBinding.
const internalBindingWhitelist = new SafeSet(['uv', 'http_parser', 'v8']);
const internalBindingWhitelist =
new SafeSet([
'uv',
'http_parser',
'v8',
'stream_wrap']);
process.binding = function binding(name) {
return internalBindingWhitelist.has(name) ?
internalBinding(name) :
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/child_process.js
Expand Up @@ -24,7 +24,7 @@ const assert = require('assert');
const { internalBinding } = require('internal/bootstrap/loaders');

const { Process } = process.binding('process_wrap');
const { WriteWrap } = process.binding('stream_wrap');
const { WriteWrap } = internalBinding('stream_wrap');
const { Pipe, constants: PipeConstants } = process.binding('pipe_wrap');
const { TTY } = process.binding('tty_wrap');
const { TCP } = process.binding('tcp_wrap');
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Expand Up @@ -115,7 +115,7 @@ const { isArrayBufferView } = require('internal/util/types');

const { FileHandle } = process.binding('fs');
const binding = internalBinding('http2');
const { ShutdownWrap } = process.binding('stream_wrap');
const { ShutdownWrap } = internalBinding('stream_wrap');
const { UV_EOF } = internalBinding('uv');

const { StreamPipe } = internalBinding('stream_pipe');
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/stream_base_commons.js
Expand Up @@ -2,7 +2,8 @@

const { Buffer } = require('buffer');
const errors = require('internal/errors');
const { WriteWrap } = process.binding('stream_wrap');
const { internalBinding } = require('internal/bootstrap/loaders');
const { WriteWrap } = internalBinding('stream_wrap');

const errnoException = errors.errnoException;

Expand Down
2 changes: 1 addition & 1 deletion lib/net.js
Expand Up @@ -43,7 +43,7 @@ const {

const { Buffer } = require('buffer');
const TTYWrap = process.binding('tty_wrap');
const { ShutdownWrap } = process.binding('stream_wrap');
const { ShutdownWrap } = internalBinding('stream_wrap');
const {
TCP,
TCPConnectWrap,
Expand Down
4 changes: 2 additions & 2 deletions src/stream_wrap.cc
Expand Up @@ -374,5 +374,5 @@ void LibuvStreamWrap::AfterUvWrite(uv_write_t* req, int status) {

} // namespace node

NODE_BUILTIN_MODULE_CONTEXT_AWARE(stream_wrap,
node::LibuvStreamWrap::Initialize)
NODE_MODULE_CONTEXT_AWARE_INTERNAL(stream_wrap,
node::LibuvStreamWrap::Initialize)
Expand Up @@ -9,3 +9,4 @@ const assert = require('assert');
assert(process.binding('uv'));
assert(process.binding('http_parser'));
assert(process.binding('v8'));
assert(process.binding('stream_wrap'));
6 changes: 4 additions & 2 deletions test/parallel/test-stream-wrap.js
@@ -1,10 +1,12 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');

const { internalBinding } = require('internal/test/binding');
const StreamWrap = require('_stream_wrap');
const Duplex = require('stream').Duplex;
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
const { Duplex } = require('stream');
const { ShutdownWrap } = internalBinding('stream_wrap');

function testShutdown(callback) {
const stream = new Duplex({
Expand Down
6 changes: 4 additions & 2 deletions test/parallel/test-tcp-wrap-connect.js
@@ -1,9 +1,11 @@
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const { internalBinding } = require('internal/test/binding');
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const TCPConnectWrap = process.binding('tcp_wrap').TCPConnectWrap;
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
const { TCPConnectWrap } = process.binding('tcp_wrap');
const { ShutdownWrap } = internalBinding('stream_wrap');

function makeConnection() {
const client = new TCP(TCPConstants.SOCKET);
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-tcp-wrap-listen.js
@@ -1,9 +1,11 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');

const { internalBinding } = require('internal/test/binding');
const { TCP, constants: TCPConstants } = process.binding('tcp_wrap');
const WriteWrap = process.binding('stream_wrap').WriteWrap;
const { WriteWrap } = internalBinding('stream_wrap');

const server = new TCP(TCPConstants.SOCKET);

Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-tls-close-notify.js
Expand Up @@ -19,15 +19,17 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

// Flags: --expose-internals
'use strict';
const common = require('../common');

if (!common.hasCrypto)
common.skip('missing crypto');

const { internalBinding } = require('internal/test/binding');
const tls = require('tls');
const fixtures = require('../common/fixtures');
const { ShutdownWrap } = process.binding('stream_wrap');
const { ShutdownWrap } = internalBinding('stream_wrap');

const server = tls.createServer({
key: fixtures.readKey('agent1-key.pem'),
Expand Down

0 comments on commit 884b23d

Please sign in to comment.