Author: fhanik
Date: Wed Jul 19 10:49:47 2006
New Revision: 423544

URL: http://svn.apache.org/viewvc?rev=423544&view=rev
Log:
Comet connection handling. When the response.getWriter().close() method has 
been called,
the comet connection is setup for closure instead of waiting for a timeout.
This is necessary since the servlet could have set a long timeout.
Also, improve on timeout checking. Only use the optimization for how frequently 
we need to check the keys if there has been no activity on the selector. During 
heavy activity, the optimization takes into effect.

Modified:
    tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java
    
tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java
    tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java?rev=423544&r1=423543&r2=423544&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java 
(original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java 
Wed Jul 19 10:49:47 2006
@@ -174,7 +174,14 @@
      * Comet used.
      */
     protected boolean comet = false;
-
+    
+    /**
+     * Closed flag, a Comet async thread can 
+     * signal for this Nio processor to be closed and recycled instead
+     * of waiting for a timeout.
+     * Closed by HttpServletResponse.getWriter().close()
+     */
+    protected boolean cometClose = false;
 
     /**
      * Content delimitator for the request (if false, the connection will
@@ -970,6 +977,8 @@
         inputBuffer.recycle();
         outputBuffer.recycle();
         this.socket = null;
+        this.cometClose = false;
+        this.comet = false;
     }
 
 
@@ -1034,6 +1043,17 @@
             // transactions with the client
 
             comet = false;
+            cometClose = true;
+            SelectionKey key = 
socket.keyFor(endpoint.getPoller().getSelector());
+            if ( key != null ) {
+                NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) 
key.attachment();
+                if ( attach!=null && attach.getComet()) {
+                    //we need to recycle
+                    
request.getAttributes().remove("org.apache.tomcat.comet.timeout");
+                    attach.setError(true);
+                }
+            }
+
             try {
                 outputBuffer.endRequest();
             } catch (IOException e) {

Modified: 
tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java?rev=423544&r1=423543&r2=423544&view=diff
==============================================================================
--- 
tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java 
(original)
+++ 
tomcat/tc6.0.x/trunk/java/org/apache/coyote/http11/InternalNioOutputBuffer.java 
Wed Jul 19 10:49:47 2006
@@ -571,16 +571,11 @@
 
     int total = 0;
     private synchronized void addToBB(byte[] buf, int offset, int length) 
throws IOException {
-        try {
-            if (bbuf.capacity() <= (offset + length)) {
-                flushBuffer();
-            }
-            bbuf.put(buf, offset, length);
-            total += length;
-        }catch ( Exception x ) {
-            x.printStackTrace();
+        if (bbuf.capacity() <= (offset + length)) {
+            flushBuffer();
         }
-        //System.out.println("Total:"+total);
+        bbuf.put(buf, offset, length);
+        total += length;
     }
 
 

Modified: tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java?rev=423544&r1=423543&r2=423544&view=diff
==============================================================================
--- tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java 
(original)
+++ tomcat/tc6.0.x/trunk/java/org/apache/tomcat/util/net/NioEndpoint.java Wed 
Jul 19 10:49:47 2006
@@ -312,7 +312,7 @@
     public void setPollerThreadCount(int pollerThreadCount) { 
this.pollerThreadCount = pollerThreadCount; }
     public int getPollerThreadCount() { return pollerThreadCount; }
 
-    protected long selectorTimeout = 5000;
+    protected long selectorTimeout = 1000;
     public void setSelectorTimeout(long timeout){ this.selectorTimeout = 
timeout;}
     public long getSelectorTimeout(){ return this.selectorTimeout; }
     /**
@@ -1043,9 +1043,11 @@
             addEvent(r);
         }
 
-        public void events() {
+        public boolean events() {
+            boolean result = false;
             synchronized (events) {
                 Runnable r = null;
+                result = (events.size() > 0);
                 while ( (events.size() > 0) && (r = events.removeFirst()) != 
null ) {
                     try {
                         r.run();
@@ -1055,6 +1057,7 @@
                 }
                 events.clear();
             }
+            return result;
         }
         
         public void register(final SocketChannel socket)
@@ -1103,8 +1106,9 @@
                         // Ignore
                     }
                 }
+                boolean hasEvents = false;
 
-                events();
+                hasEvents = (hasEvents | events());
                 // Time to terminate?
                 if (close) return;
 
@@ -1115,8 +1119,9 @@
                     log.error("",x);
                     continue;
                 }
-                
-            
+
+                //either we timed out or we woke up, process events first
+                if ( keyCount == 0 ) hasEvents = (hasEvents | events());
 
                 //if (keyCount == 0) continue;
 
@@ -1162,16 +1167,18 @@
                     }
                 }//while
                 //process timeouts
-                timeout();
-            }
+                timeout(keyCount,hasEvents);
+            }//while
             synchronized (this) {
                 this.notifyAll();
             }
 
         }
-        protected void timeout() {
+        protected void timeout(int keyCount, boolean hasEvents) {
             long now = System.currentTimeMillis();
-            if ( now < nextExpiration ) return;
+            //don't process timeouts too frequently, but if the selector 
simply timed out
+            //then we can check timeouts to avoid gaps
+            if ( (now < nextExpiration) && (keyCount>0 || hasEvents) ) return;
             nextExpiration = now + (long)soTimeout;
             //timeout
             Set<SelectionKey> keys = selector.keys();



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to