You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2015/05/26 13:29:16 UTC

svn commit: r1681742 - in /tomcat/trunk/java/org/apache: coyote/http11/ tomcat/util/net/

Author: markt
Date: Tue May 26 11:29:15 2015
New Revision: 1681742

URL: http://svn.apache.org/r1681742
Log:
Various fixes to timeout handling post the 9.0.x connector refactoring
Highlights:
 - use longs for timeouts in AprEndpoint
 - APR read/write registration now uses current timeout
 - move last[Read|Write] to NIO since only NIO uses it
 - Simplify switching between keepAlive and So timeouts for HTTP

Modified:
    tomcat/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java
    tomcat/trunk/java/org/apache/coyote/http11/Http11InputBuffer.java
    tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java
    tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
    tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java

Modified: tomcat/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java?rev=1681742&r1=1681741&r2=1681742&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http11/Http11AprProtocol.java Tue May 26 11:29:15 2015
@@ -102,7 +102,6 @@ public class Http11AprProtocol extends A
             processor.recycle();
             recycledProcessors.push(processor);
             if (addToPoller && getProtocol().getEndpoint().isRunning()) {
-                socket.setReadTimeout(getProtocol().getEndpoint().getKeepAliveTimeout());
                 socket.registerReadInterest();
             }
         }

Modified: tomcat/trunk/java/org/apache/coyote/http11/Http11InputBuffer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/Http11InputBuffer.java?rev=1681742&r1=1681741&r2=1681742&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/Http11InputBuffer.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http11/Http11InputBuffer.java Tue May 26 11:29:15 2015
@@ -381,7 +381,7 @@ public class Http11InputBuffer implement
      * @return true if data is properly fed; false if no data is available
      * immediately and thread should be freed
      */
-    boolean parseRequestLine(boolean useAvailableDataOnly) throws IOException {
+    boolean parseRequestLine(boolean keptAlive) throws IOException {
 
         //check state
         if ( !parsingRequestLine ) return true;
@@ -394,15 +394,19 @@ public class Http11InputBuffer implement
 
                 // Read new bytes if needed
                 if (pos >= lastValid) {
-                    if (useAvailableDataOnly) {
-                        return false;
+                    if (keptAlive) {
+                        // Haven't read any request data yet so use the keep-alive
+                        // timeout.
+                        wrapper.setReadTimeout(wrapper.getEndpoint().getKeepAliveTimeout());
                     }
-                    // Do a simple read with a short timeout
                     if (!fill(false)) {
                         // A read is pending, so no longer in initial state
                         parsingRequestLinePhase = 1;
                         return false;
                     }
+                    // At least one byte of the request has been received.
+                    // Switch to the socket timeout.
+                    wrapper.setReadTimeout(wrapper.getEndpoint().getSoTimeout());
                 }
                 // Set the start time once we start reading data (even if it is
                 // just skipping blank lines)

Modified: tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java?rev=1681742&r1=1681741&r2=1681742&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java Tue May 26 11:29:15 2015
@@ -1148,13 +1148,7 @@ public class Http11Processor extends Abs
         // open
         openSocket = true;
         // Check to see if we have read any of the request line yet
-        if (inputBuffer.getParsingRequestLinePhase() < 1) {
-            if (keptAlive) {
-                // Haven't read the request line and have previously processed a
-                // request. Must be keep-alive. Make sure poller uses keepAlive.
-                socketWrapper.setReadTimeout(endpoint.getKeepAliveTimeout());
-            }
-        } else {
+        if (inputBuffer.getParsingRequestLinePhase() > 1) {
             // Started to read request line.
             if (request.getStartTime() < 0) {
                 request.setStartTime(System.currentTimeMillis());
@@ -1168,8 +1162,6 @@ public class Http11Processor extends Abs
             } else {
                 // Need to keep processor associated with socket
                 readComplete = false;
-                // Make sure poller uses soTimeout from here onwards
-                socketWrapper.setReadTimeout(endpoint.getSoTimeout());
             }
         }
         return true;

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java?rev=1681742&r1=1681741&r2=1681742&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/AprEndpoint.java Tue May 26 11:29:15 2015
@@ -1068,7 +1068,7 @@ public class AprEndpoint extends Abstrac
 
     public static class SocketInfo {
         public long socket;
-        public int timeout;
+        public long timeout;
         public int flags;
         public boolean read() {
             return (flags & Poll.APR_POLLIN) == Poll.APR_POLLIN;
@@ -1162,7 +1162,7 @@ public class AprEndpoint extends Abstrac
         protected int pos;
 
         protected long[] sockets;
-        protected int[] timeouts;
+        protected long[] timeouts;
         protected int[] flags;
 
         protected SocketInfo info = new SocketInfo();
@@ -1171,7 +1171,7 @@ public class AprEndpoint extends Abstrac
             this.size = 0;
             pos = 0;
             sockets = new long[size];
-            timeouts = new int[size];
+            timeouts = new long[size];
             flags = new int[size];
         }
 
@@ -1196,7 +1196,7 @@ public class AprEndpoint extends Abstrac
             pos = 0;
         }
 
-        public boolean add(long socket, int timeout, int flag) {
+        public boolean add(long socket, long timeout, int flag) {
             if (size == sockets.length) {
                 return false;
             } else {
@@ -1461,14 +1461,14 @@ public class AprEndpoint extends Abstrac
          * be removed from the poller.
          *
          * @param socket to add to the poller
-         * @param timeout to use for this connection
+         * @param timeout to use for this connection in milliseconds
          * @param flags Events to poll for (Poll.APR_POLLIN and/or
          *              Poll.APR_POLLOUT)
          */
-        private void add(long socket, int timeout, int flags) {
+        private void add(long socket, long timeout, int flags) {
             if (log.isDebugEnabled()) {
                 String msg = sm.getString("endpoint.debug.pollerAdd",
-                        Long.valueOf(socket), Integer.valueOf(timeout),
+                        Long.valueOf(socket), Long.valueOf(timeout),
                         Integer.valueOf(flags));
                 if (log.isTraceEnabled()) {
                     log.trace(msg, new Exception());
@@ -2685,7 +2685,7 @@ public class AprEndpoint extends Abstrac
                     return;
                 }
                 ((AprEndpoint) getEndpoint()).getPoller().add(
-                        getSocket().longValue(), -1, Poll.APR_POLLIN);
+                        getSocket().longValue(), getReadTimeout(), Poll.APR_POLLIN);
             }
         }
 
@@ -2698,7 +2698,7 @@ public class AprEndpoint extends Abstrac
                     return;
                 }
                 ((AprEndpoint) getEndpoint()).getPoller().add(
-                        getSocket().longValue(), -1, Poll.APR_POLLOUT);
+                        getSocket().longValue(), getWriteTimeout(), Poll.APR_POLLOUT);
             }
         }
 

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=1681742&r1=1681741&r2=1681742&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Tue May 26 11:29:15 2015
@@ -1119,6 +1119,8 @@ public class NioEndpoint extends Abstrac
         private CountDownLatch readLatch = null;
         private CountDownLatch writeLatch = null;
         private volatile SendfileData sendfileData = null;
+        private volatile long lastRead = System.currentTimeMillis();
+        private volatile long lastWrite = lastRead;
 
         public NioSocketWrapper(NioChannel channel, NioEndpoint endpoint) {
             super(channel, endpoint);
@@ -1161,6 +1163,11 @@ public class NioEndpoint extends Abstrac
         public void setSendfileData(SendfileData sf) { this.sendfileData = sf;}
         public SendfileData getSendfileData() { return this.sendfileData;}
 
+        public void updateLastWrite() { lastWrite = System.currentTimeMillis(); }
+        public long getLastWrite() { return lastWrite; }
+        public void updateLastRead() { lastRead = System.currentTimeMillis(); }
+        public long getLastRead() { return lastRead; }
+
 
         @Override
         public boolean isReadyForRead() throws IOException {
@@ -1206,6 +1213,7 @@ public class NioEndpoint extends Abstrac
 
             // Fill the read buffer as best we can.
             int nRead = fillReadBuffer(block);
+            lastRead = System.currentTimeMillis();
 
             // Full as much of the remaining byte array as possible with the
             // data that was just read
@@ -1284,6 +1292,7 @@ public class NioEndpoint extends Abstrac
                         if (getSocket().flush(true, selector, writeTimeout)) break;
                     } while (true);
                 }
+                lastWrite = System.currentTimeMillis();
             } finally {
                 if (selector != null) {
                     pool.put(selector);

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java?rev=1681742&r1=1681741&r2=1681742&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapperBase.java Tue May 26 11:29:15 2015
@@ -41,8 +41,6 @@ public abstract class SocketWrapperBase<
 
     // Volatile because I/O and setting the timeout values occurs on a different
     // thread to the thread checking the timeout.
-    private volatile long lastRead = System.currentTimeMillis();
-    private volatile long lastWrite = lastRead;
     private volatile long lastAsyncStart = 0;
     private volatile long asyncTimeout = -1;
     private volatile long readTimeout = -1;
@@ -201,9 +199,6 @@ public abstract class SocketWrapperBase<
     }
 
 
-    public void updateLastWrite() { lastWrite = System.currentTimeMillis(); }
-    public long getLastWrite() { return lastWrite; }
-    public long getLastRead() { return lastRead; }
     public IOException getError() { return error; }
     public void setError(IOException error) { this.error = error; }
     public void setKeepAliveLeft(int keepAliveLeft) { this.keepAliveLeft = keepAliveLeft;}
@@ -389,8 +384,6 @@ public abstract class SocketWrapperBase<
             return;
         }
 
-        lastWrite = System.currentTimeMillis();
-
         // While the implementations for blocking and non-blocking writes are
         // very similar they have been split into separate methods to allow
         // sub-classes to override them individually. NIO2, for example,
@@ -569,7 +562,6 @@ public abstract class SocketWrapperBase<
      *                     write
      */
     protected final void doWrite(boolean block) throws IOException {
-        lastWrite = System.currentTimeMillis();
         doWriteInternal(block);
     }
 



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