Skip to content

Commit

Permalink
Do not split headers and payload into two different buffers. Doing so…
Browse files Browse the repository at this point in the history
… may result in a drop in performance if no-tcp-delay is false and client uses delayed ACKs. (#6491)

Signed-off-by: Santiago Pericasgeertsen <[email protected]>
  • Loading branch information
spericas authored Mar 28, 2023
1 parent 907dd05 commit 570893b
Showing 1 changed file with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -427,19 +427,29 @@ private void write(BufferData buffer) throws IOException {
if (closed) {
throw new IOException("Stream already closed");
}

if (!isChunked) {
if (firstByte) {
firstByte = false;
BufferData growing = BufferData.growing(256);
sendListener.headers(ctx, headers);
// write headers and payload part in one buffer to avoid TCP/ACK delay problems
BufferData growing = BufferData.growing(256 + buffer.available());
nonEntityBytes(headers, status.get(), growing, keepAlive);
// check not exceeding content-length
bytesWritten += buffer.available();
checkContentLength(buffer);
sendListener.data(ctx, buffer);
// write single buffer headers and payload part
growing.write(buffer);
responseBytesTotal += growing.available();
dataWriter.write(growing);
} else {
// if not chunked, always write
writeContent(buffer);
}

// if not chunked, always write
writeContent(buffer);
return;
}

// try chunked data optimization
if (firstByte && firstBuffer == null) {
// if somebody re-uses the byte buffer sent to us, we must copy it
Expand Down Expand Up @@ -531,14 +541,17 @@ private void writeChunked(BufferData buffer) {
dataWriter.write(toWrite);
}

private void writeContent(BufferData buffer) throws IOException {
bytesWritten += buffer.available();
private void checkContentLength(BufferData buffer) throws IOException {
if (bytesWritten > contentLength && contentLength != -1) {
throw new IOException("Content length was set to " + contentLength
+ ", but you are writing additional " + (bytesWritten - contentLength) + " "
+ "bytes");
+ ", but you are writing additional " + (bytesWritten - contentLength) + " "
+ "bytes");
}
}

private void writeContent(BufferData buffer) throws IOException {
bytesWritten += buffer.available();
checkContentLength(buffer);
sendListener.data(ctx, buffer);
responseBytesTotal += buffer.available();
dataWriter.write(buffer);
Expand Down

0 comments on commit 570893b

Please sign in to comment.