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

http: buffer writes while no socket #29019

Closed
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
4 changes: 3 additions & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const { Object, ObjectPrototype } = primordials;

const { getDefaultHighWaterMark } = require('internal/streams/state');
lpinca marked this conversation as resolved.
Show resolved Hide resolved
const assert = require('internal/assert');
const Stream = require('stream');
const internalUtil = require('internal/util');
Expand Down Expand Up @@ -51,6 +52,7 @@ const {
} = require('internal/errors');
const { validateString } = require('internal/validators');

const HIGH_WATER_MARK = getDefaultHighWaterMark();
const { CRLF, debug } = common;

const kIsCorked = Symbol('isCorked');
Expand Down Expand Up @@ -277,7 +279,7 @@ function _writeRaw(data, encoding, callback) {
this.outputData.push({ data, encoding, callback });
this.outputSize += data.length;
this._onPendingData(data.length);
return false;
return this.outputSize < HIGH_WATER_MARK;
}


Expand Down
9 changes: 7 additions & 2 deletions lib/internal/streams/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ function highWaterMarkFrom(options, isDuplex, duplexKey) {
isDuplex ? options[duplexKey] : null;
}

function getDefaultHighWaterMark(objectMode) {
return objectMode ? 16 : 16 * 1024;
}

function getHighWaterMark(state, options, duplexKey, isDuplex) {
const hwm = highWaterMarkFrom(options, isDuplex, duplexKey);
if (hwm != null) {
Expand All @@ -20,9 +24,10 @@ function getHighWaterMark(state, options, duplexKey, isDuplex) {
}

// Default value
return state.objectMode ? 16 : 16 * 1024;
return getDefaultHighWaterMark(state.objectMode);
}

module.exports = {
getHighWaterMark
getHighWaterMark,
getDefaultHighWaterMark
};
19 changes: 19 additions & 0 deletions test/parallel/test-http-outgoing-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Flags: --expose-internals
'use strict';
require('../common');
const assert = require('assert');
const { getDefaultHighWaterMark } = require('internal/streams/state');
ronag marked this conversation as resolved.
Show resolved Hide resolved

const http = require('http');
const OutgoingMessage = http.OutgoingMessage;

const msg = new OutgoingMessage();
msg._implicitHeader = function() {};

// Writes should be buffered until highwatermark
// even when no socket is assigned.

assert.strictEqual(msg.write('asd'), true);
while (msg.write('asd'));
const highwatermark = msg.writableHighWaterMark || getDefaultHighWaterMark();
assert(msg.outputSize >= highwatermark);