From 0c30a207ac11a341294b9672744667737aac7b5c Mon Sep 17 00:00:00 2001 From: fhanik Date: Thu, 22 Jun 2006 20:28:27 +0000 Subject: [PATCH] Two fixes, process timeouts last, no need to check a timeout on a valid read. hence we do it after we process the actual operations Throw an IO exception if we reach end of stream so that the sockets can get properly closed down Speed is the same for nio and io right now, nio seems just a tad faster without keep alives git-svn-id: https://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk@416461 13f79535-47bb-0310-9956-ffa450edef68 --- .../coyote/http11/InternalNioInputBuffer.java | 3 +- java/org/apache/tomcat/util/net/NioEndpoint.java | 45 +++++++++++----------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/java/org/apache/coyote/http11/InternalNioInputBuffer.java b/java/org/apache/coyote/http11/InternalNioInputBuffer.java index 3959f1ab6..2a9575764 100644 --- a/java/org/apache/coyote/http11/InternalNioInputBuffer.java +++ b/java/org/apache/coyote/http11/InternalNioInputBuffer.java @@ -559,7 +559,8 @@ public class InternalNioInputBuffer implements InputBuffer { lastValid = pos + nRead; return true; } else if (nRead == -1) { - return false; + //return false; + throw new IOException("end of stream reached."); } timedOut = (readTimeout != -1) && ((System.currentTimeMillis()-start)>this.readTimeout); if ( !timedOut && nRead == 0 ) diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index 96659226c..a3d1a48df 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -1191,32 +1191,15 @@ public class NioEndpoint { log.error("",x); continue; } - //timeout - Set keys = selector.keys(); - long now = System.currentTimeMillis(); - for (Iterator iter = keys.iterator(); iter.hasNext(); ) { - SelectionKey key = (SelectionKey) iter.next(); - try { - if (key.interestOps() == SelectionKey.OP_READ) { - //only timeout sockets that we are waiting for a read from - KeyAttachment ka = (KeyAttachment) key.attachment(); - long delta = now - ka.getLastAccess(); - if (delta > (long) soTimeout) { - cancelledKey(key); - } - } - }catch ( CancelledKeyException ckx ) { - cancelledKey(key); - } - } + - if (keyCount == 0) continue; + //if (keyCount == 0) continue; - Iterator iterator = selector.selectedKeys().iterator(); + Iterator iterator = keyCount > 0 ? selector.selectedKeys().iterator() : null; // Walk through the collection of ready keys and dispatch // any active event. - while (iterator.hasNext()) { + while (iterator != null && iterator.hasNext()) { SelectionKey sk = (SelectionKey) iterator.next(); iterator.remove(); KeyAttachment attachment = (KeyAttachment)sk.attachment(); @@ -1255,7 +1238,25 @@ public class NioEndpoint { } }//while - + //timeout + Set keys = selector.keys(); + long now = System.currentTimeMillis(); + for (Iterator iter = keys.iterator(); iter.hasNext(); ) { + SelectionKey key = (SelectionKey) iter.next(); + try { + if (key.interestOps() == SelectionKey.OP_READ) { + //only timeout sockets that we are waiting for a read from + KeyAttachment ka = (KeyAttachment) key.attachment(); + long delta = now - ka.getLastAccess(); + if (delta > (long) soTimeout) { + cancelledKey(key); + } + } + }catch ( CancelledKeyException ckx ) { + cancelledKey(key); + } + } + } synchronized (this) { this.notifyAll(); -- 2.11.0