diff --git a/doc/api/http.md b/doc/api/http.md index ce761af7561198..2dc1c11ca4cf33 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -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. diff --git a/lib/net.js b/lib/net.js index 7f9b1e5c084d8c..ca8c1f32d41556 100644 --- a/lib/net.js +++ b/lib/net.js @@ -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; @@ -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; @@ -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); diff --git a/test/parallel/test-http-server-options-highwatermark.js b/test/parallel/test-http-server-options-highwatermark.js index 2b96b33f17c915..3380cfb62f87c7 100644 --- a/test/parallel/test-http-server-options-highwatermark.js +++ b/test/parallel/test-http-server-options-highwatermark.js @@ -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(() => { @@ -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); }