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

src: refactor HasWriteQueue() away #18019

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions lib/internal/net.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
'use strict';

const Buffer = require('buffer').Buffer;
const { writeBuffer } = process.binding('fs');

// Check that the port number is not NaN when coerced to a number,
// is an integer and that it falls within the legal range of port numbers.
function isLegalPort(port) {
Expand All @@ -9,7 +12,28 @@ function isLegalPort(port) {
return +port === (+port >>> 0) && port <= 0xFFFF;
}

function makeSyncWrite(fd) {
return function(chunk, enc, cb) {
if (enc !== 'buffer')
chunk = Buffer.from(chunk, enc);

this._bytesDispatched += chunk.length;

try {
writeBuffer(fd, chunk, 0, chunk.length, null);
} catch (ex) {
// Legacy: net writes have .code === .errno, whereas writeBuffer gives the
// raw errno number in .errno.
if (typeof ex.code === 'string')
ex.errno = ex.code;
return cb(ex);
}
cb();
};
}

module.exports = {
isLegalPort,
makeSyncWrite,
normalizedArgsSymbol: Symbol('normalizedArgs')
};
16 changes: 12 additions & 4 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ const stream = require('stream');
const timers = require('timers');
const util = require('util');
const internalUtil = require('internal/util');
const { isLegalPort, normalizedArgsSymbol } = require('internal/net');
const {
isLegalPort,
normalizedArgsSymbol,
makeSyncWrite
} = require('internal/net');
const assert = require('assert');
const cares = process.binding('cares_wrap');
const {
Expand Down Expand Up @@ -220,20 +224,24 @@ function Socket(options) {
this._handle = options.handle; // private
this[async_id_symbol] = getNewAsyncId(this._handle);
} else if (options.fd !== undefined) {
this._handle = createHandle(options.fd, false);
this._handle.open(options.fd);
const fd = options.fd;
this._handle = createHandle(fd, false);
this._handle.open(fd);
this[async_id_symbol] = this._handle.getAsyncId();
// options.fd can be string (since it is user-defined),
// so changing this to === would be semver-major
// See: https://github.com/nodejs/node/pull/11513
// eslint-disable-next-line eqeqeq
if ((options.fd == 1 || options.fd == 2) &&
if ((fd == 1 || fd == 2) &&
(this._handle instanceof Pipe) &&
process.platform === 'win32') {
// Make stdout and stderr blocking on Windows
var err = this._handle.setBlocking(true);
if (err)
throw errnoException(err, 'setBlocking');

this._writev = null;
this._write = makeSyncWrite(fd);
Copy link
Member

Choose a reason for hiding this comment

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

Can we assign fd to a symbol property here and do the if logic in Socket.prototype._write to avoid the additional closure?

Copy link
Member Author

Choose a reason for hiding this comment

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

@joyeecheung I have thought about making fd a property for all net.Sockets, but I’m not sure whether that’s a good idea or not.

We could add a symbol, but for now I’d probably leave the code as it is, especially given that the worst case is that this might create 2 closures for the entire process…

}
this.readable = options.readable !== false;
this.writable = options.writable !== false;
Expand Down
3 changes: 3 additions & 0 deletions lib/tty.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const util = require('util');
const net = require('net');
const { TTY, isTTY } = process.binding('tty_wrap');
const { makeSyncWrite } = require('internal/net');
const { inherits } = util;
const errnoException = util._errnoException;
const errors = require('internal/errors');
Expand Down Expand Up @@ -91,6 +92,8 @@ function WriteStream(fd) {
// even though it was originally intended to change in v1.0.2 (Libuv 1.2.1).
// Ref: https://github.com/nodejs/node/pull/1771#issuecomment-119351671
this._handle.setBlocking(true);
this._writev = null;
this._write = makeSyncWrite(fd);

var winSize = new Array(2);
var err = this._handle.getWindowSize(winSize);
Expand Down
20 changes: 10 additions & 10 deletions src/stream_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,17 @@ int StreamBase::Writev(const FunctionCallbackInfo<Value>& args) {
offset += str_size;
bytes += str_size;
}

err = DoTryWrite(&buf_list, &count);
if (err != 0 || count == 0) {
req_wrap->Dispatched();
req_wrap->Dispose();
goto done;
}
}

err = DoWrite(req_wrap, buf_list, count, nullptr);
if (HasWriteQueue())
req_wrap_obj->Set(env->async(), True(env->isolate()));
req_wrap_obj->Set(env->async(), True(env->isolate()));

if (err)
req_wrap->Dispose();
Expand Down Expand Up @@ -254,8 +260,7 @@ int StreamBase::WriteBuffer(const FunctionCallbackInfo<Value>& args) {
}

err = DoWrite(req_wrap, bufs, count, nullptr);
if (HasWriteQueue())
req_wrap_obj->Set(env->async(), True(env->isolate()));
req_wrap_obj->Set(env->async(), True(env->isolate()));
req_wrap_obj->Set(env->buffer_string(), args[1]);

if (err)
Expand Down Expand Up @@ -381,8 +386,7 @@ int StreamBase::WriteString(const FunctionCallbackInfo<Value>& args) {
reinterpret_cast<uv_stream_t*>(send_handle));
}

if (HasWriteQueue())
req_wrap_obj->Set(env->async(), True(env->isolate()));
req_wrap_obj->Set(env->async(), True(env->isolate()));

if (err)
req_wrap->Dispose();
Expand Down Expand Up @@ -476,10 +480,6 @@ int StreamResource::DoTryWrite(uv_buf_t** bufs, size_t* count) {
return 0;
}

bool StreamResource::HasWriteQueue() {
return true;
}


const char* StreamResource::Error() const {
return nullptr;
Expand Down
1 change: 0 additions & 1 deletion src/stream_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ class StreamResource {
uv_buf_t* bufs,
size_t count,
uv_stream_t* send_handle) = 0;
virtual bool HasWriteQueue();
virtual const char* Error() const;
virtual void ClearError();

Expand Down
4 changes: 0 additions & 4 deletions src/stream_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,6 @@ int LibuvStreamWrap::DoWrite(WriteWrap* w,
}


bool LibuvStreamWrap::HasWriteQueue() {
return stream()->write_queue_size > 0;
}


void LibuvStreamWrap::AfterUvWrite(uv_write_t* req, int status) {
WriteWrap* req_wrap = WriteWrap::from_req(req);
Expand Down
1 change: 0 additions & 1 deletion src/stream_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class LibuvStreamWrap : public HandleWrap, public StreamBase {
uv_buf_t* bufs,
size_t count,
uv_stream_t* send_handle) override;
bool HasWriteQueue() override;

inline uv_stream_t* stream() const {
return stream_;
Expand Down