This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 4ec041a3db Reapply "Further cleanup of Stream references to aid GC"
4ec041a3db is described below

commit 4ec041a3db34db6d6870b59aee49302063cbc2d4
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Tue Jan 14 08:27:56 2025 +0000

    Reapply "Further cleanup of Stream references to aid GC"
    
    This reverts commit 34e623c1c4d2c21f60bdc0c7dd93d703461cf7d4 and
    fixes an issue when Response.reset() was called.
---
 java/org/apache/coyote/NonPipeliningProcessor.java | 26 ++++++++++++++++++++++
 java/org/apache/coyote/Request.java                |  9 ++++++++
 java/org/apache/coyote/Response.java               | 19 ++++++++++++++--
 java/org/apache/coyote/http2/StreamProcessor.java  |  3 ++-
 webapps/docs/changelog.xml                         |  5 +++++
 5 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/java/org/apache/coyote/NonPipeliningProcessor.java 
b/java/org/apache/coyote/NonPipeliningProcessor.java
new file mode 100644
index 0000000000..75a6da0830
--- /dev/null
+++ b/java/org/apache/coyote/NonPipeliningProcessor.java
@@ -0,0 +1,26 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.coyote;
+
+/**
+ * Marker interface used to indicate that the {@link Processor} does not 
implement pipe-lining of requests (e.g.
+ * HTTP/1.1 supports pipe-lining whereas HTTP/2 does not) which may enable 
some components to clear references sooner
+ * to aid GC.
+ */
+public interface NonPipeliningProcessor extends Processor {
+
+}
diff --git a/java/org/apache/coyote/Request.java 
b/java/org/apache/coyote/Request.java
index bb0fedc2c8..3b6f34262a 100644
--- a/java/org/apache/coyote/Request.java
+++ b/java/org/apache/coyote/Request.java
@@ -773,6 +773,15 @@ public final class Request {
 
         startTime = -1;
         threadId = 0;
+
+        if (hook instanceof NonPipeliningProcessor) {
+            /*
+             * No requirement to maintain state between requests so clear the 
hook (a.k.a. Processor) and the input
+             * buffer to aid GC.
+             */
+            setHook(null);
+            setInputBuffer(null);
+        }
     }
 
     // -------------------- Info --------------------
diff --git a/java/org/apache/coyote/Response.java 
b/java/org/apache/coyote/Response.java
index bd835fa826..94fe9dea2c 100644
--- a/java/org/apache/coyote/Response.java
+++ b/java/org/apache/coyote/Response.java
@@ -345,7 +345,7 @@ public final class Response {
             throw new IllegalStateException();
         }
 
-        recycle();
+        recycle(false);
     }
 
 
@@ -622,10 +622,13 @@ public final class Response {
         contentWritten += len - chunk.remaining();
     }
 
-    // --------------------
 
     public void recycle() {
+        recycle(true);
+    }
+
 
+    private void recycle(boolean responseComplete) {
         contentType = null;
         contentLanguage = null;
         locale = DEFAULT_LOCALE;
@@ -649,8 +652,20 @@ public final class Response {
 
         // update counters
         contentWritten = 0;
+
+        if (responseComplete && hook instanceof NonPipeliningProcessor) {
+            /*
+             * No requirement to maintain state between responses so clear the 
hook (a.k.a. Processor) and the output
+             * buffer to aid GC.
+             *
+             * Only clear these between responses. They need to be retained 
when the response is reset.
+             */
+            setHook(null);
+            setOutputBuffer(null);
+        }
     }
 
+
     /**
      * Bytes written by application - i.e. before compression, chunking, etc.
      *
diff --git a/java/org/apache/coyote/http2/StreamProcessor.java 
b/java/org/apache/coyote/http2/StreamProcessor.java
index cf33d5ba9a..ab8b716b19 100644
--- a/java/org/apache/coyote/http2/StreamProcessor.java
+++ b/java/org/apache/coyote/http2/StreamProcessor.java
@@ -34,6 +34,7 @@ import org.apache.coyote.ActionCode;
 import org.apache.coyote.Adapter;
 import org.apache.coyote.ContinueResponseTiming;
 import org.apache.coyote.ErrorState;
+import org.apache.coyote.NonPipeliningProcessor;
 import org.apache.coyote.Request;
 import org.apache.coyote.RequestGroupInfo;
 import org.apache.coyote.Response;
@@ -52,7 +53,7 @@ import org.apache.tomcat.util.net.SocketEvent;
 import org.apache.tomcat.util.net.SocketWrapperBase;
 import org.apache.tomcat.util.res.StringManager;
 
-class StreamProcessor extends AbstractProcessor {
+class StreamProcessor extends AbstractProcessor implements 
NonPipeliningProcessor {
 
     private static final Log log = LogFactory.getLog(StreamProcessor.class);
     private static final StringManager sm = 
StringManager.getManager(StreamProcessor.class);
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 94473dd735..444da61eda 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -153,6 +153,11 @@
         processing poller events for the NIO Connector as this may occur in
         normal usage. (markt)
       </fix>
+      <scode>
+        Clean-up references to the HTTP/2 stream once request processing has
+        completed to aid GC and reduce the size of the HTTP/2 recycled request
+        and response cache. (markt)
+      </scode>
     </changelog>
   </subsection>
 </section>


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to