a little more on the timeout part
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 5 Aug 2009 20:55:50 +0000 (20:55 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Wed, 5 Aug 2009 20:55:50 +0000 (20:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@801409 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/connector/AsyncContextImpl.java
java/org/apache/catalina/connector/CoyoteAdapter.java
java/org/apache/coyote/Adapter.java
java/org/apache/coyote/http11/Http11NioProcessor.java
java/org/apache/tomcat/util/net/NioEndpoint.java

index d27686e..5526d49 100644 (file)
@@ -43,7 +43,7 @@ import org.apache.juli.logging.LogFactory;
 public class AsyncContextImpl implements AsyncContext {
     
     public static enum AsyncState {
-        NOT_STARTED, STARTED, DISPATCHING, DISPATCHED, COMPLETING
+        NOT_STARTED, STARTED, DISPATCHING, DISPATCHED, COMPLETING, TIMING_OUT
     }
     
     protected static Log log = LogFactory.getLog(AsyncContextImpl.class);
@@ -220,7 +220,11 @@ public class AsyncContextImpl implements AsyncContext {
     }
     
     public void doInternalDispatch() throws ServletException, IOException {
-        if (this.state.compareAndSet(AsyncState.DISPATCHING, AsyncState.DISPATCHED)) {
+        if (this.state.compareAndSet(AsyncState.TIMING_OUT, AsyncState.DISPATCHED)) {
+            for (AsyncListenerWrapper listener : listeners) {
+                listener.fireOnTimeout();
+            }
+        } else if (this.state.compareAndSet(AsyncState.DISPATCHING, AsyncState.DISPATCHED)) {
             if (this.dispatch!=null) {
                 try {
                     dispatch.run();
@@ -266,5 +270,13 @@ public class AsyncContextImpl implements AsyncContext {
             throw new IllegalStateException("Complete illegal. Invalid state:"+state.get());
         }
     }
+    
+    public AsyncState getState() {
+        return state.get();
+    }
+    
+    protected void setState(AsyncState st) {
+        state.set(st);
+    }
 
 }
index 559dd8f..bd2f643 100644 (file)
@@ -251,7 +251,7 @@ public class CoyoteAdapter
         }
     }
     
-    public boolean asyncDispatch(org.apache.coyote.Request req,org.apache.coyote.Response res) throws Exception {
+    public boolean asyncDispatch(org.apache.coyote.Request req,org.apache.coyote.Response res, SocketStatus status) throws Exception {
         Request request = (Request) req.getNote(ADAPTER_NOTES);
         Response response = (Response) res.getNote(ADAPTER_NOTES);
 
@@ -267,6 +267,12 @@ public class CoyoteAdapter
             request.setAttribute(Globals.DISPATCHER_TYPE_ATTR, DispatcherType.ASYNC);
             // Calling the container
             try {
+                if (status==SocketStatus.TIMEOUT) {
+                   AsyncContextImpl asyncConImpl = (AsyncContextImpl)request.getAsyncContext();
+                   //TODO SERVLET3 - async
+                   //configure settings for timed out
+                   asyncConImpl.setState(AsyncContextImpl.AsyncState.TIMING_OUT);
+                }
                 connector.getContainer().getPipeline().getFirst().invoke(request, response);
             }catch (RuntimeException x) {
                 success = false;
index 05b24ba..a712fcc 100644 (file)
@@ -50,6 +50,6 @@ public interface Adapter {
     public boolean event(Request req, Response res, SocketStatus status)
     throws Exception;
     
-    public boolean asyncDispatch(Request req,Response res) throws Exception;
+    public boolean asyncDispatch(Request req,Response res, SocketStatus status) throws Exception;
 
 }
index 4534db6..8e4e4d4 100644 (file)
@@ -799,7 +799,7 @@ public class Http11NioProcessor implements ActionHook {
         final NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socket.getAttachment(false);
         try {
             rp.setStage(org.apache.coyote.Constants.STAGE_SERVICE);
-            error = !adapter.asyncDispatch(request, response);
+            error = !adapter.asyncDispatch(request, response, status);
             if ( !error ) {
                 if (attach != null) {
                     attach.setComet(comet);
@@ -861,6 +861,7 @@ public class Http11NioProcessor implements ActionHook {
         error = false;
         keepAlive = true;
         comet = false;
+        async = false;
         
         long soTimeout = endpoint.getSoTimeout();
         int keepAliveTimeout = endpoint.getKeepAliveTimeout();
@@ -870,7 +871,7 @@ public class Http11NioProcessor implements ActionHook {
         boolean recycle = true;
         final KeyAttachment ka = (KeyAttachment)socket.getAttachment(false);
         
-        while (!error && keepAlive && !comet) {
+        while (!error && keepAlive && !comet && !async) {
             //always default to our soTimeout
             ka.setTimeout(soTimeout);
             // Parsing the request header
@@ -1324,9 +1325,7 @@ public class Http11NioProcessor implements ActionHook {
             NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment)socket.getAttachment(false);
             long timeout = ((Long)param).longValue();
             //if we are not piggy backing on a worker thread, set the timeout
-            RequestInfo rp = request.getRequestProcessor();
-            if ( rp.getStage() != org.apache.coyote.Constants.STAGE_SERVICE ) //async handling
-                attach.setTimeout(timeout);
+            attach.setTimeout(timeout);
         } else if (actionCode == ActionCode.ACTION_ASYNC_DISPATCH) {
             RequestInfo rp = request.getRequestProcessor();
             if ( rp.getStage() != org.apache.coyote.Constants.STAGE_SERVICE ) {//async handling
index 5d63197..514958d 100644 (file)
@@ -1710,6 +1710,13 @@ public class NioEndpoint {
                             long nextTime = now+(timeout-delta);
                             nextExpiration = (nextTime < nextExpiration)?nextTime:nextExpiration;
                         }
+                    }else if (ka.isAsync()) {
+                        long delta = now - ka.getLastAccess();
+                        long timeout = (ka.getTimeout()==-1)?((long) socketProperties.getSoTimeout()):(ka.getTimeout());
+                        boolean isTimedout = delta > timeout;
+                        if (isTimedout) {
+                            processSocket(ka.getChannel(), SocketStatus.TIMEOUT, true);
+                        }
                     }//end if
                 }catch ( CancelledKeyException ckx ) {
                     cancelledKey(key, SocketStatus.ERROR,false);