Set a per connection timeout
authorfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 13 Jul 2006 19:51:56 +0000 (19:51 +0000)
committerfhanik <fhanik@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 13 Jul 2006 19:51:56 +0000 (19:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@421694 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/CometProcessor.java
java/org/apache/coyote/http11/Http11NioProcessor.java
java/org/apache/tomcat/util/net/NioEndpoint.java

index aac7fb4..b329fdf 100644 (file)
@@ -117,7 +117,7 @@ public interface CometProcessor {
      * This method should not be called asynchronously, as that will have no effect.
      * @param request The HTTP servlet request instance
      * @param response The HTTP servlet response instance
-     * @param timeout The timeout in milliseconds for this connection
+     * @param timeout The timeout in milliseconds for this connection, must be a positive value, larger than 0
      * @throws IOException An IOException may be thrown to indicate an IO error, 
      *         or that the EOF has been reached on the connection
      * @throws ServletException An exception has occurred, as specified by the root
index 51e3ea6..4d44aa0 100644 (file)
@@ -743,7 +743,11 @@ public class Http11NioProcessor implements ActionHook {
             SelectionKey key = socket.keyFor(endpoint.getPoller().getSelector());\r
             if ( key != null ) {\r
                 NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment();\r
-                if ( attach!=null ) attach.setComet(comet);\r
+                if ( attach!=null ) {\r
+                    attach.setComet(comet);\r
+                    Integer comettimeout = (Integer)request.getAttribute("org.apache.tomcat.comet.timeout");\r
+                    if ( comettimeout != null ) attach.setTimeout(comettimeout.longValue());\r
+                }\r
             }\r
             \r
         } catch (InterruptedIOException e) {\r
@@ -884,7 +888,11 @@ public class Http11NioProcessor implements ActionHook {
                     SelectionKey key = socket.keyFor(endpoint.getPoller().getSelector());\r
                     if (key != null) {\r
                         NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment();\r
-                        if (attach != null) attach.setComet(comet);\r
+                        if (attach != null)  {\r
+                            attach.setComet(comet);\r
+                            Integer comettimeout = (Integer) request.getAttribute("org.apache.tomcat.comet.timeout");\r
+                            if (comettimeout != null) attach.setTimeout(comettimeout.longValue());\r
+                        }\r
                     }\r
                 } catch (InterruptedIOException e) {\r
                     error = true;\r
@@ -1397,6 +1405,8 @@ public class Http11NioProcessor implements ActionHook {
 \r
         // Advertise comet support through a request attribute\r
         request.setAttribute("org.apache.tomcat.comet.support", Boolean.TRUE);\r
+        // Advertise comet timeout support\r
+        request.setAttribute("org.apache.tomcat.comet.timeout.support", Boolean.TRUE);\r
 \r
     }\r
 \r
index 939923f..64c4f4b 100644 (file)
@@ -1158,16 +1158,17 @@ public class NioEndpoint {
                 }//while\r
 \r
                 //timeout\r
-                Set keys = selector.keys();\r
+                Set<SelectionKey> keys = selector.keys();\r
                 long now = System.currentTimeMillis();\r
-                for (Iterator iter = keys.iterator(); iter.hasNext(); ) {\r
-                    SelectionKey key = (SelectionKey) iter.next();\r
+                for (Iterator<SelectionKey> iter = keys.iterator(); iter.hasNext(); ) {\r
+                    SelectionKey key = iter.next();\r
                     try {\r
                         if (key.interestOps() == SelectionKey.OP_READ) {\r
                             //only timeout sockets that we are waiting for a read from\r
                             KeyAttachment ka = (KeyAttachment) key.attachment();\r
                             long delta = now - ka.getLastAccess();\r
-                            if (delta > (long) soTimeout) {\r
+                            boolean isTimedout = (ka.getTimeout()==-1)?(delta > (long) soTimeout):(delta>ka.getTimeout());\r
+                            if (isTimedout) {\r
                                 cancelledKey(key);\r
                             }\r
                         }\r
@@ -1197,11 +1198,14 @@ public class NioEndpoint {
         public boolean getWakeUp() { return wakeUp; }\r
         public void setWakeUp(boolean wakeUp) { this.wakeUp = wakeUp; }\r
         public Object getMutex() {return mutex;}\r
+        public void setTimeout(long timeout) {this.timeout = timeout;}\r
+        public long getTimeout() {return this.timeout;}\r
         protected Object mutex = new Object();\r
         protected boolean wakeUp = false;\r
         protected long lastAccess = System.currentTimeMillis();\r
         protected boolean currentAccess = false;\r
         protected boolean comet = false;\r
+        protected long timeout = -1;\r
 \r
     }\r
 \r