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

net: set default highwatermark at socket creation time #48882

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -3448,7 +3448,8 @@ changes:
* `highWaterMark` {number} Optionally overrides all `socket`s'
`readableHighWaterMark` and `writableHighWaterMark`. This affects
`highWaterMark` property of both `IncomingMessage` and `ServerResponse`.
**Default:** See [`stream.getDefaultHighWaterMark()`][].
**Default:** `undefined`. Sockets will use the default watermark value
at the time the request arrives. See [`stream.getDefaultHighWaterMark()`][].
* `insecureHTTPParser` {boolean} If set to `true`, it will use a HTTP parser
with leniency flags enabled. Using the insecure parser should be avoided.
See [`--insecure-http-parser`][] for more information.
Expand Down
8 changes: 2 additions & 6 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ const {
startPerf,
stopPerf,
} = require('internal/perf/observe');
const { getDefaultHighWaterMark } = require('internal/streams/state');

function getFlags(ipv6Only) {
return ipv6Only === true ? TCPConstants.UV_TCP_IPV6ONLY : 0;
Expand Down Expand Up @@ -1735,11 +1734,8 @@ function Server(options, connectionListener) {
if (typeof options.highWaterMark !== 'undefined') {
validateNumber(
options.highWaterMark, 'options.highWaterMark',
0,
);

if (options.highWaterMark < 0) {
options.highWaterMark = getDefaultHighWaterMark();
}
}

this._connections = 0;
Expand All @@ -1755,7 +1751,7 @@ function Server(options, connectionListener) {
this.noDelay = Boolean(options.noDelay);
this.keepAlive = Boolean(options.keepAlive);
this.keepAliveInitialDelay = ~~(options.keepAliveInitialDelay / 1000);
this.highWaterMark = options.highWaterMark ?? getDefaultHighWaterMark();
this.highWaterMark = options.highWaterMark;
}
ObjectSetPrototypeOf(Server.prototype, EventEmitter.prototype);
ObjectSetPrototypeOf(Server, EventEmitter);
Expand Down
28 changes: 22 additions & 6 deletions test/parallel/test-http-server-options-highwatermark.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const assert = require('assert');
const http = require('http');
const { kHighWaterMark } = require('_http_outgoing');

const { getDefaultHighWaterMark } = require('internal/streams/state');
const { setDefaultHighWaterMark } = require('internal/streams/state');

function listen(server) {
server.listen(0, common.mustCall(() => {
Expand All @@ -20,28 +20,44 @@ function listen(server) {
}));
}

// Test `options.highWaterMark` fails if less than zero.
{
assert.throws(() => {
http.createServer({
highWaterMark: -1,
});
}, { code: 'ERR_OUT_OF_RANGE' });
}

// Test socket watermark is set with the value in `options.highWaterMark`.
{
const waterMarkValue = 17000;
const serverWaterMarkValue = 14000;
const server = http.createServer({
highWaterMark: getDefaultHighWaterMark() * 2,
highWaterMark: serverWaterMarkValue,
}, common.mustCall((req, res) => {
assert.strictEqual(req._readableState.highWaterMark, getDefaultHighWaterMark() * 2);
assert.strictEqual(res[kHighWaterMark], getDefaultHighWaterMark() * 2);
assert.strictEqual(req._readableState.highWaterMark, serverWaterMarkValue);
assert.strictEqual(res[kHighWaterMark], serverWaterMarkValue);
res.statusCode = 200;
res.end();
}));

setDefaultHighWaterMark(false, waterMarkValue);
listen(server);
}

// Test socket watermark is the default if `highWaterMark` in `options` is unset.
{
const waterMarkValue = 13000;
const server = http.createServer(
common.mustCall((req, res) => {
assert.strictEqual(req._readableState.highWaterMark, getDefaultHighWaterMark());
assert.strictEqual(res[kHighWaterMark], getDefaultHighWaterMark());
assert.strictEqual(req._readableState.highWaterMark, waterMarkValue);
assert.strictEqual(res[kHighWaterMark], waterMarkValue);
res.statusCode = 200;
res.end();
})
);

setDefaultHighWaterMark(false, waterMarkValue);
listen(server);
}
Loading