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

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


The following commit(s) were added to refs/heads/8.5.x by this push:
     new 06fc959  Avoid creating and using object caches when they are disabled.
06fc959 is described below

commit 06fc959488c1672572cf348d56c01fff970f5a30
Author: Mark Thomas <[email protected]>
AuthorDate: Thu Feb 17 18:59:35 2022 +0000

    Avoid creating and using object caches when they are disabled.
    
    Aligns 8.5.x with 9.0.x onwards.
    Prep for backporting socket close changes
---
 .../apache/tomcat/util/net/AbstractEndpoint.java   |  5 +-
 java/org/apache/tomcat/util/net/AprEndpoint.java   |  8 +-
 java/org/apache/tomcat/util/net/Nio2Endpoint.java  | 39 ++++++----
 java/org/apache/tomcat/util/net/NioEndpoint.java   | 87 ++++++++++++++--------
 webapps/docs/changelog.xml                         |  6 +-
 5 files changed, 97 insertions(+), 48 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/AbstractEndpoint.java 
b/java/org/apache/tomcat/util/net/AbstractEndpoint.java
index 27f0610..6bcf693 100644
--- a/java/org/apache/tomcat/util/net/AbstractEndpoint.java
+++ b/java/org/apache/tomcat/util/net/AbstractEndpoint.java
@@ -1146,7 +1146,10 @@ public abstract class AbstractEndpoint<S,U> {
             if (socketWrapper == null) {
                 return false;
             }
-            SocketProcessorBase<S> sc = processorCache.pop();
+            SocketProcessorBase<S> sc = null;
+            if (processorCache != null) {
+                sc = processorCache.pop();
+            }
             if (sc == null) {
                 sc = createSocketProcessor(socketWrapper, event);
             } else {
diff --git a/java/org/apache/tomcat/util/net/AprEndpoint.java 
b/java/org/apache/tomcat/util/net/AprEndpoint.java
index 75ffdf7..8b89f96 100644
--- a/java/org/apache/tomcat/util/net/AprEndpoint.java
+++ b/java/org/apache/tomcat/util/net/AprEndpoint.java
@@ -455,8 +455,10 @@ public class AprEndpoint extends 
AbstractEndpoint<Long,Long> implements SNICallB
             running = true;
             paused = false;
 
-            processorCache = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
-                    socketProperties.getProcessorCache());
+            if (socketProperties.getProcessorCache() != 0) {
+                processorCache = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                        socketProperties.getProcessorCache());
+            }
 
             // Create worker collection
             if (getExecutor() == null) {
@@ -2213,7 +2215,7 @@ public class AprEndpoint extends 
AbstractEndpoint<Long,Long> implements SNICallB
                 socketWrapper = null;
                 event = null;
                 //return to cache
-                if (running) {
+                if (running && processorCache != null) {
                     processorCache.push(this);
                 }
             }
diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java 
b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
index 4c9ecc0..da1083b 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -167,13 +167,17 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
             running = true;
             paused = false;
 
-            processorCache = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
-                    socketProperties.getProcessorCache());
-            nioChannels = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
-                    socketProperties.getBufferPool());
+            if (socketProperties.getProcessorCache() != 0) {
+                processorCache = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                        socketProperties.getProcessorCache());
+            }
+            if (socketProperties.getBufferPool() != 0) {
+                nioChannels = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                        socketProperties.getBufferPool());
+            }
 
             // Create worker collection
-            if ( getExecutor() == null ) {
+            if (getExecutor() == null) {
                 createExecutor();
             }
 
@@ -212,11 +216,17 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                     }
                 }
             });
-            Nio2Channel socket;
-            while ((socket = nioChannels.pop()) != null) {
-                socket.free();
+            if (nioChannels != null) {
+                Nio2Channel socket;
+                while ((socket = nioChannels.pop()) != null) {
+                    socket.free();
+                }
+                nioChannels = null;
+            }
+            if (processorCache != null) {
+                processorCache.clear();
+                processorCache = null;
             }
-            processorCache.clear();
         }
     }
 
@@ -304,7 +314,10 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
     protected boolean setSocketOptions(AsynchronousSocketChannel socket) {
         try {
             socketProperties.setProperties(socket);
-            Nio2Channel channel = nioChannels.pop();
+            Nio2Channel channel = null;
+            if (nioChannels != null) {
+                channel = nioChannels.pop();
+            }
             if (channel == null) {
                 SocketBufferHandler bufhandler = new SocketBufferHandler(
                         socketProperties.getAppReadBufSize(),
@@ -1654,7 +1667,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                         // Close socket and pool
                         socketWrapper.close();
                         if (running) {
-                            if (!nioChannels.push(socketWrapper.getSocket())) {
+                            if (nioChannels == null || 
!nioChannels.push(socketWrapper.getSocket())) {
                                 socketWrapper.getSocket().free();
                             }
                         }
@@ -1665,7 +1678,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                     getHandler().process(socketWrapper, 
SocketEvent.CONNECT_FAIL);
                     socketWrapper.close();
                     if (running) {
-                        if (!nioChannels.push(socketWrapper.getSocket())) {
+                        if (nioChannels == null || 
!nioChannels.push(socketWrapper.getSocket())) {
                             socketWrapper.getSocket().free();
                         }
                     }
@@ -1691,7 +1704,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                 socketWrapper = null;
                 event = null;
                 //return to cache
-                if (running) {
+                if (running && processorCache != null) {
                     processorCache.push(this);
                 }
             }
diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java 
b/java/org/apache/tomcat/util/net/NioEndpoint.java
index 1d1e34c..5b45dac 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -267,12 +267,18 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
             running = true;
             paused = false;
 
-            processorCache = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
-                    socketProperties.getProcessorCache());
-            eventCache = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
-                            socketProperties.getEventCache());
-            nioChannels = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
-                    socketProperties.getBufferPool());
+            if (socketProperties.getProcessorCache() != 0) {
+                processorCache = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                        socketProperties.getProcessorCache());
+            }
+            if (socketProperties.getEventCache() != 0) {
+                eventCache = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                        socketProperties.getEventCache());
+            }
+            if (socketProperties.getBufferPool() != 0) {
+                nioChannels = new 
SynchronizedStack<>(SynchronizedStack.DEFAULT_SIZE,
+                        socketProperties.getBufferPool());
+            }
 
             // Create worker collection
             if (getExecutor() == null) {
@@ -317,12 +323,21 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
                 
log.warn(sm.getString("endpoint.nio.stopLatchAwaitInterrupted"), e);
             }
             shutdownExecutor();
-            eventCache.clear();
-            NioChannel socket;
-            while ((socket = nioChannels.pop()) != null) {
-                socket.free();
+            if (eventCache != null) {
+                eventCache.clear();
+                eventCache = null;
+            }
+            if (nioChannels != null) {
+                NioChannel socket;
+                while ((socket = nioChannels.pop()) != null) {
+                    socket.free();
+                }
+                nioChannels = null;
+            }
+            if (processorCache != null) {
+                processorCache.clear();
+                processorCache = null;
             }
-            processorCache.clear();
         }
     }
 
@@ -333,7 +348,8 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
     @Override
     public void unbind() throws Exception {
         if (log.isDebugEnabled()) {
-            log.debug("Destroy initiated for "+new 
InetSocketAddress(getAddress(),getPort()));
+            log.debug("Destroy initiated for " +
+                    new InetSocketAddress(getAddress(),getPort()));
         }
         if (running) {
             stop();
@@ -350,7 +366,8 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
         }
         selectorPool.close();
         if (log.isDebugEnabled()) {
-            log.debug("Destroy completed for "+new 
InetSocketAddress(getAddress(),getPort()));
+            log.debug("Destroy completed for " +
+                    new InetSocketAddress(getAddress(), getPort()));
         }
     }
 
@@ -413,7 +430,10 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
             Socket sock = socket.socket();
             socketProperties.setProperties(sock);
 
-            NioChannel channel = nioChannels.pop();
+            NioChannel channel = null;
+            if (nioChannels != null) {
+                channel = nioChannels.pop();
+            }
             if (channel == null) {
                 SocketBufferHandler bufhandler = new SocketBufferHandler(
                         socketProperties.getAppReadBufSize(),
@@ -603,7 +623,7 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
                     log.debug("Socket: [" + socket + "] closed");
                 }
                 if (running) {
-                    if (!nioChannels.push(socket)) {
+                    if (nioChannels == null || !nioChannels.push(socket)) {
                         socket.free();
                     }
                 }
@@ -738,7 +758,10 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
          *                    the Poller
          */
         public void add(final NioChannel socket, final int interestOps) {
-            PollerEvent r = eventCache.pop();
+            PollerEvent r = null;
+            if (eventCache != null) {
+                r = eventCache.pop();
+            }
             if (r == null) {
                 r = new PollerEvent(socket,null,interestOps);
             } else {
@@ -765,8 +788,8 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
                 result = true;
                 try {
                     pe.run();
-                    pe.reset();
-                    if (running) {
+                    if (running && eventCache != null) {
+                        pe.reset();
                         eventCache.push(pe);
                     }
                 } catch ( Throwable x ) {
@@ -792,14 +815,17 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
             ka.setKeepAliveLeft(NioEndpoint.this.getMaxKeepAliveRequests());
             ka.setReadTimeout(getConnectionTimeout());
             ka.setWriteTimeout(getConnectionTimeout());
-            PollerEvent r = eventCache.pop();
             ka.interestOps(SelectionKey.OP_READ);//this is what OP_REGISTER 
turns into.
-            if ( r==null) {
-                r = new PollerEvent(socket,ka,OP_REGISTER);
+            PollerEvent event = null;
+            if (eventCache != null) {
+                event = eventCache.pop();
+            }
+            if (event == null) {
+                event = new PollerEvent(socket, ka, OP_REGISTER);
             } else {
-                r.reset(socket,ka,OP_REGISTER);
+                event.reset(socket, ka, OP_REGISTER);
             }
-            addEvent(r);
+            addEvent(event);
         }
 
         public NioSocketWrapper cancelledKey(SelectionKey key) {
@@ -935,10 +961,10 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
             try {
                 if (close) {
                     cancelledKey(sk);
-                } else if ( sk.isValid() && attachment != null ) {
+                } else if (sk.isValid() && attachment != null ) {
                     if (sk.isReadable() || sk.isWritable() ) {
-                        if ( attachment.getSendfileData() != null ) {
-                            processSendfile(sk,attachment, false);
+                        if (attachment.getSendfileData() != null ) {
+                            processSendfile(sk, attachment, false);
                         } else {
                             unreg(sk, attachment, sk.readyOps());
                             boolean closeSocket = false;
@@ -1059,9 +1085,9 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
                         log.debug("OP_WRITE for sendfile: " + sd.fileName);
                     }
                     if (calledByProcessor) {
-                        add(socketWrapper.getSocket(),SelectionKey.OP_WRITE);
+                        add(socketWrapper.getSocket(), SelectionKey.OP_WRITE);
                     } else {
-                        reg(sk,socketWrapper,SelectionKey.OP_WRITE);
+                        reg(sk, socketWrapper, SelectionKey.OP_WRITE);
                     }
                     return SendfileState.PENDING;
                 }
@@ -1162,7 +1188,8 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
         }
     }
 
-    // ---------------------------------------------------- Key Attachment 
Class
+    // --------------------------------------------------- Socket Wrapper Class
+
     public static class NioSocketWrapper extends SocketWrapperBase<NioChannel> 
{
 
         private final NioSelectorPool pool;
@@ -1729,7 +1756,7 @@ public class NioEndpoint extends 
AbstractJsseEndpoint<NioChannel,SocketChannel>
                 socketWrapper = null;
                 event = null;
                 //return to cache
-                if (running) {
+                if (running && processorCache != null) {
                     processorCache.push(this);
                 }
             }
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index e573352..c307798 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -178,7 +178,11 @@
         Remove <code>pollerThreadCount</code> Connector attribute for NIO.
         One poller thread is sufficient. (remm/markt)
       </update>
-    </changelog>
+      <fix>
+        Avoid creating and using object caches when they are disabled.
+        (remm/markt)
+      </fix>
+  </changelog>
   </subsection>
   <subsection name="Jasper">
     <changelog>

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to