Skip to content
Merged
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
76 changes: 76 additions & 0 deletions .github/CODEOWNERS
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
# Node.js Project Codeowners

# 1. Codeowners must always be teams, never individuals
# 2. Each codeowner team should contain at least one TSC member
# 3. PRs touching any code with a codeowner must be signed off by at least one
# person on the code owner team.

./.github/CODEOWNERS @nodejs/tsc

# net

# ./deps/cares @nodejs/net
# ./doc/api/dns.md @nodejs/net
# ./doc/api/dgram.md @nodejs/net
# ./doc/api/net.md @nodejs/net
# ./lib/dgram.js @nodejs/net
# ./lib/dns.js @nodejs/net
# ./lib/net.js @nodejs/net @nodejs/quic
# ./lib/internal/dgram.js @nodejs/net
# ./lib/internal/dns/* @nodejs/net
# ./lib/internal/net.js @nodejs/net
# ./lib/internal/socket_list.js @nodejs/net
# ./lib/internal/js_stream_socket.js @nodejs/net
# ./src/cares_wrap.h @nodejs/net
# ./src/connect_wrap.* @nodejs/net
# ./src/connection_wrap.* @nodejs/net
# ./src/node_sockaddr* @nodejs/net
# ./src/tcp_wrap.* @nodejs/net
# ./src/udp_wrap.* @nodejs/net

# tls/crypto

# ./lib/internal/crypto/* @nodejs/crypto
# ./lib/internal/tls.js @nodejs/crypto @nodejs/net
# ./lib/crypto.js @nodejs/crypto
# ./lib/tls.js @nodejs/crypto @nodejs/net
# ./src/node_crypto* @nodejs/crypto
# ./src/node_crypto_common* @nodejs/crypto @nodejs/quic

# http

# ./deps/llhttp/* @nodejs/http @nodejs/net
# ./doc/api/http.md @nodejs/http @nodejs/net
# ./doc/api/http2.md @nodejs/http @nodejs/net
# ./lib/_http_* @nodejs/http @nodejs/net
# ./lib/http.js @nodejs/http @nodejs/net
# ./lib/https.js @nodejs/crypto @nodejs/net @nodejs/http
# ./src/node_http_common* @nodejs/http @nodejs/http2 @nodejs/quic @nodejs/net
# ./src/node_http_parser.cc @nodejs/http @nodejs/net

# http2

# ./deps/nghttp2/* @nodejs/http2 @nodejs/net
# ./doc/api/http2.md @nodejs/http2 @nodejs/net
# ./lib/http2.js @nodejs/http2 @nodejs/net
# ./lib/internal/http2/* @nodejs/http2 @nodejs/net
# ./src/node_http2* @nodejs/http2 @nodejs/net
# ./src/node_mem* @nodejs/http2

# quic

./deps/ngtcp2/* @nodejs/quic
./deps/nghttp3/* @nodejs/quic
./doc/api/quic.md @nodejs/quic
./lib/internal/quic/* @nodejs/quic
./src/node_bob* @nodejs/quic
./src/quic/* @nodejs/quic

# modules

# ./doc/api/modules.md @nodejs/modules
# ./doc/api/esm.md @nodejs/modules
# ./lib/module.js @nodejs/modules
# ./lib/internal/modules/* @nodejs/modules
# ./lib/internal/bootstrap/loaders.js @nodejs/modules
# ./src/module_wrap* @nodejs/modules @nodejs/vm
2 changes: 1 addition & 1 deletion doc/api/fs.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -4440,7 +4440,7 @@ The `fs.promises` API provides an alternative set of asynchronous file system
methods that return `Promise` objects rather than using callbacks. The
API is accessible via `require('fs').promises` or `require('fs/promises')`.

### class: `FileHandle`
### Class: `FileHandle`
<!-- YAML
added: v10.0.0
-->
Expand Down
10 changes: 10 additions & 0 deletions doc/api/http.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -300,6 +300,16 @@ added: v0.3.6
By default set to `Infinity`. Determines how many concurrent sockets the agent
can have open per origin. Origin is the returned value of [`agent.getName()`][].

### `agent.maxTotalSockets`
<!-- YAML
added: REPLACEME
-->

* {number}

By default set to `Infinity`. Determines how many concurrent sockets the agent
can have open. Unlike `maxSockets`, this parameter applies across all origins.

### `agent.requests`
<!-- YAML
added: v0.5.9
Expand Down
52 changes: 48 additions & 4 deletions lib/_http_agent.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,6 +22,7 @@
'use strict';

const {
NumberIsNaN,
ObjectKeys,
ObjectSetPrototypeOf,
ObjectValues,
Expand All@@ -38,11 +39,14 @@ const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OPT_VALUE,
ERR_OUT_OF_RANGE,
},
} = require('internal/errors');
const { once } = require('internal/util');
const { validateNumber } = require('internal/validators');

const kOnKeylog = Symbol('onkeylog');
const kRequestOptions = Symbol('requestOptions');
// New Agent code.

// The largest departure from the previous implementation is that
Expand DownExpand Up@@ -90,11 +94,22 @@ function Agent(options) {
this.maxSockets = this.options.maxSockets || Agent.defaultMaxSockets;
this.maxFreeSockets = this.options.maxFreeSockets || 256;
this.scheduling = this.options.scheduling || 'fifo';
this.maxTotalSockets = this.options.maxTotalSockets;
this.totalSocketCount = 0;

if (this.scheduling !== 'fifo' && this.scheduling !== 'lifo') {
throw new ERR_INVALID_OPT_VALUE('scheduling', this.scheduling);
}

if (this.maxTotalSockets !== undefined) {
validateNumber(this.maxTotalSockets, 'maxTotalSockets');
if (this.maxTotalSockets <= 0 || NumberIsNaN(this.maxTotalSockets))
throw new ERR_OUT_OF_RANGE('maxTotalSockets', '> 0',
this.maxTotalSockets);
} else {
this.maxTotalSockets = Infinity;
}

this.on('free', (socket, options) => {
const name = this.getName(options);
debug('agent.on(free)', name);
Expand DownExpand Up@@ -133,7 +148,8 @@ function Agent(options) {
if (this.sockets[name])
count += this.sockets[name].length;

if (count > this.maxSockets ||
if (this.totalSocketCount > this.maxTotalSockets ||
count > this.maxSockets ||
freeLen >= this.maxFreeSockets ||
!this.keepSocketAlive(socket)) {
socket.destroy();
Expand DownExpand Up@@ -248,7 +264,9 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
this.reuseSocket(socket, req);
setRequestSocket(this, req, socket);
this.sockets[name].push(socket);
} else if (sockLen < this.maxSockets) {
this.totalSocketCount++;
} else if (sockLen < this.maxSockets &&
this.totalSocketCount < this.maxTotalSockets) {
debug('call onSocket', sockLen, freeLen);
// If we are under maxSockets create a new one.
this.createSocket(req, options, (err, socket) => {
Expand All@@ -263,6 +281,10 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
if (!this.requests[name]) {
this.requests[name] = [];
}

// Used to create sockets for pending requests from different origin
req[kRequestOptions] = options;

this.requests[name].push(req);
}
};
Expand All@@ -288,7 +310,8 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
this.sockets[name] = [];
}
this.sockets[name].push(s);
debug('sockets', name, this.sockets[name].length);
this.totalSocketCount++;
debug('sockets', name, this.sockets[name].length, this.totalSocketCount);
installListeners(this, s, options);
cb(null, s);
});
Expand DownExpand Up@@ -394,13 +417,33 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
// Don't leak
if (sockets[name].length === 0)
delete sockets[name];
this.totalSocketCount--;
}
}
}

let req;
if (this.requests[name] && this.requests[name].length) {
debug('removeSocket, have a request, make a socket');
const req = this.requests[name][0];
req = this.requests[name][0];
} else {
// TODO(rickyes): this logic will not be FIFO across origins.
// There might be older requests in a different origin, but
// if the origin which releases the socket has pending requests
// that will be prioritized.
for (const prop in this.requests) {
// Check whether this specific origin is already at maxSockets
if (this.sockets[prop] && this.sockets[prop].length) break;
debug('removeSocket, have a request with different origin,' +
' make a socket');
req = this.requests[prop][0];
options = req[kRequestOptions];
break;
}
}

if (req && options) {
req[kRequestOptions] = undefined;
// If we have pending requests and a socket gets closed make a new one
this.createSocket(req, options, (err, socket) => {
if (err)
Expand All@@ -409,6 +452,7 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
socket.emit('free');
});
}

};

Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) {
Expand Down
7 changes: 6 additions & 1 deletion lib/_http_client.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -49,7 +49,7 @@ const Agent = require('_http_agent');
const { Buffer } = require('buffer');
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
const { URL, urlToOptions, searchParamsSymbol } = require('internal/url');
const { kOutHeaders, kNeedDrain } = require('internal/http');
const { kOutHeaders, kNeedDrain, kClosed } = require('internal/http');
const { connResetException, codes } = require('internal/errors');
const {
ERR_HTTP_HEADERS_SENT,
Expand DownExpand Up@@ -385,6 +385,7 @@ function _destroy(req, socket, err) {
if (err) {
req.emit('error', err);
}
req[kClosed] = true;
req.emit('close');
}
}
Expand DownExpand Up@@ -427,6 +428,7 @@ function socketCloseListener() {
res.emit('error', connResetException('aborted'));
}
}
req[kClosed] = true;
req.emit('close');
if (!res.aborted && res.readable) {
res.on('end', function() {
Expand All@@ -446,6 +448,7 @@ function socketCloseListener() {
req.socket._hadError = true;
req.emit('error', connResetException('socket hang up'));
}
req[kClosed] = true;
req.emit('close');
}

Expand DownExpand Up@@ -550,6 +553,7 @@ function socketOnData(d) {

req.emit(eventName, res, socket, bodyHead);
req.destroyed = true;
req[kClosed] = true;
req.emit('close');
} else {
// Requested Upgrade or used CONNECT method, but have no handler.
Expand DownExpand Up@@ -721,6 +725,7 @@ function requestOnPrefinish() {
}

function emitFreeNT(req) {
req[kClosed] = true;
req.emit('close');
if (req.res) {
req.res.emit('close');
Expand Down
13 changes: 10 additions & 3 deletions lib/_http_outgoing.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,7 +36,7 @@ const assert = require('internal/assert');
const EE = require('events');
const Stream = require('stream');
const internalUtil = require('internal/util');
const { kOutHeaders, utcDate, kNeedDrain } = require('internal/http');
const { kOutHeaders, utcDate, kNeedDrain, kClosed } = require('internal/http');
const { Buffer } = require('buffer');
const common = require('_http_common');
const checkIsHttpToken = common._checkIsHttpToken;
Expand DownExpand Up@@ -117,6 +117,7 @@ function OutgoingMessage() {
this.finished = false;
this._headerSent = false;
this[kCorked] = 0;
this[kClosed] = false;

this.socket = null;
this._header = null;
Expand DownExpand Up@@ -663,7 +664,9 @@ function onError(msg, err, callback) {

function emitErrorNt(msg, err, callback) {
callback(err);
if (typeof msg.emit === 'function') msg.emit('error', err);
if (typeof msg.emit === 'function' && !msg[kClosed]) {
msg.emit('error', err);
}
}

function write_(msg, chunk, encoding, callback, fromEnd) {
Expand All@@ -690,7 +693,11 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
}

if (err) {
onError(msg, err, callback);
if (!msg.destroyed) {
onError(msg, err, callback);
} else {
process.nextTick(callback, err);
}
return false;
}

Expand Down
3 changes: 3 additions & 0 deletions lib/_http_server.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -49,6 +49,7 @@ const { OutgoingMessage } = require('_http_outgoing');
const {
kOutHeaders,
kNeedDrain,
kClosed,
emitStatistics
} = require('internal/http');
const {
Expand DownExpand Up@@ -212,6 +213,7 @@ function onServerResponseClose() {
// Fortunately, that requires only a single if check. :-)
if (this._httpMessage) {
this._httpMessage.destroyed = true;
this._httpMessage[kClosed] = true;
this._httpMessage.emit('close');
}
}
Expand DownExpand Up@@ -729,6 +731,7 @@ function resOnFinish(req, res, socket, state, server) {

function emitCloseNT(self) {
self.destroyed = true;
self[kClosed] = true;
self.emit('close');
}

Expand Down
13 changes: 13 additions & 0 deletions lib/internal/fs/streams.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -255,6 +255,12 @@ ReadStream.prototype._read = function(n) {
};

ReadStream.prototype._destroy = function(err, cb) {
// Usually for async IO it is safe to close a file descriptor
// even when there are pending operations. However, due to platform
// differences file IO is implemented using synchronous operations
// running in a thread pool. Therefore, file descriptors are not safe
// to close while used in a pending read or write operation. Wait for
// any pending IO (kIsPerformingIO) to complete (kIoDone).
if (this[kIsPerformingIO]) {
this.once(kIoDone, (er) => close(this, err || er, cb));
} else {
Expand DownExpand Up@@ -416,12 +422,19 @@ WriteStream.prototype._writev = function(data, cb) {
};

WriteStream.prototype._destroy = function(err, cb) {
// Usually for async IO it is safe to close a file descriptor
// even when there are pending operations. However, due to platform
// differences file IO is implemented using synchronous operations
// running in a thread pool. Therefore, file descriptors are not safe
// to close while used in a pending read or write operation. Wait for
// any pending IO (kIsPerformingIO) to complete (kIoDone).
if (this[kIsPerformingIO]) {
this.once(kIoDone, (er) => close(this, err || er, cb));
} else {
close(this, err, cb);
}
};

WriteStream.prototype.close = function(cb) {
if (cb) {
if (this.closed) {
Expand Down
1 change: 1 addition & 0 deletions lib/internal/http.js
Original file line numberDiff line numberDiff line change
Expand Up@@ -51,6 +51,7 @@ function emitStatistics(statistics) {
module.exports = {
kOutHeaders: Symbol('kOutHeaders'),
kNeedDrain: Symbol('kNeedDrain'),
kClosed: Symbol('kClosed'),
nowDate,
utcDate,
emitStatistics
Expand Down
Loading