From dc43f3d498ad1d985c0e6ca4b4b91bdb94b9ce83 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 18 Sep 2024 07:11:32 +1000 Subject: [PATCH] Optimize request buffer release #12239 (#12240) Release request buffer before handling when there is no content --------- Signed-off-by: Simone Bordet Co-authored-by: Simone Bordet --- .../org/eclipse/jetty/http/HttpParser.java | 5 +++++ .../jetty/server/internal/HttpConnection.java | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java index 93be3478cb0..e1b037cda64 100644 --- a/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java +++ b/jetty-core/jetty-http/src/main/java/org/eclipse/jetty/http/HttpParser.java @@ -422,6 +422,11 @@ public class HttpParser return _state; } + public boolean hasContent() + { + return _endOfContent != EndOfContent.NO_CONTENT; + } + public boolean inContentState() { return _state.ordinal() >= State.CONTENT.ordinal() && _state.ordinal() < State.END.ordinal(); diff --git a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpConnection.java b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpConnection.java index bb291501a4a..37fe59479e5 100644 --- a/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpConnection.java +++ b/jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/internal/HttpConnection.java @@ -398,6 +398,15 @@ public class HttpConnection extends AbstractMetaDataConnection implements Runnab if (LOG.isDebugEnabled()) LOG.debug("HANDLE {} {}", request, this); + // If the buffer is empty and no body is expected, then release the buffer + if (isRequestBufferEmpty() && !_parser.hasContent()) + { + // try parsing now to the end of the message + parseRequestBuffer(); + if (_parser.isComplete()) + releaseRequestBuffer(); + } + // Handle the request by running the task. _handling.set(true); Runnable onRequest = _onRequest; @@ -424,7 +433,15 @@ public class HttpConnection extends AbstractMetaDataConnection implements Runnab { if (LOG.isDebugEnabled()) LOG.debug("upgraded {} -> {}", this, getEndPoint().getConnection()); - releaseRequestBuffer(); + if (_requestBuffer != null) + releaseRequestBuffer(); + break; + } + + // If we have already released the request buffer, then use fill interest before allocating another + if (_requestBuffer == null) + { + fillInterested(); break; } }