Skip to content

Commit

Permalink
net: set default highwatermark at socket creation time
Browse files Browse the repository at this point in the history
  • Loading branch information
lukiano committed Jul 24, 2023
1 parent b68fa59 commit b0572e0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -3249,7 +3249,7 @@ 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} Use an insecure HTTP parser that accepts
invalid HTTP headers when `true`. 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 { getDefaultHighWaterMark, 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);
}

0 comments on commit b0572e0

Please sign in to comment.