From 64ec6568001dc64240d663ca106047d8ae197cda Mon Sep 17 00:00:00 2001 From: fhanik Date: Thu, 13 Jul 2006 19:51:56 +0000 Subject: [PATCH] Set a per connection timeout 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 | 2 +- java/org/apache/coyote/http11/Http11NioProcessor.java | 14 ++++++++++++-- java/org/apache/tomcat/util/net/NioEndpoint.java | 12 ++++++++---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/java/org/apache/catalina/CometProcessor.java b/java/org/apache/catalina/CometProcessor.java index aac7fb4a1..b329fdf95 100644 --- a/java/org/apache/catalina/CometProcessor.java +++ b/java/org/apache/catalina/CometProcessor.java @@ -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 diff --git a/java/org/apache/coyote/http11/Http11NioProcessor.java b/java/org/apache/coyote/http11/Http11NioProcessor.java index 51e3ea6ea..4d44aa033 100644 --- a/java/org/apache/coyote/http11/Http11NioProcessor.java +++ b/java/org/apache/coyote/http11/Http11NioProcessor.java @@ -743,7 +743,11 @@ public class Http11NioProcessor implements ActionHook { SelectionKey key = socket.keyFor(endpoint.getPoller().getSelector()); if ( key != null ) { NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment(); - if ( attach!=null ) attach.setComet(comet); + if ( attach!=null ) { + attach.setComet(comet); + Integer comettimeout = (Integer)request.getAttribute("org.apache.tomcat.comet.timeout"); + if ( comettimeout != null ) attach.setTimeout(comettimeout.longValue()); + } } } catch (InterruptedIOException e) { @@ -884,7 +888,11 @@ public class Http11NioProcessor implements ActionHook { SelectionKey key = socket.keyFor(endpoint.getPoller().getSelector()); if (key != null) { NioEndpoint.KeyAttachment attach = (NioEndpoint.KeyAttachment) key.attachment(); - if (attach != null) attach.setComet(comet); + if (attach != null) { + attach.setComet(comet); + Integer comettimeout = (Integer) request.getAttribute("org.apache.tomcat.comet.timeout"); + if (comettimeout != null) attach.setTimeout(comettimeout.longValue()); + } } } catch (InterruptedIOException e) { error = true; @@ -1397,6 +1405,8 @@ public class Http11NioProcessor implements ActionHook { // Advertise comet support through a request attribute request.setAttribute("org.apache.tomcat.comet.support", Boolean.TRUE); + // Advertise comet timeout support + request.setAttribute("org.apache.tomcat.comet.timeout.support", Boolean.TRUE); } diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java index 939923fc6..64c4f4bc0 100644 --- a/java/org/apache/tomcat/util/net/NioEndpoint.java +++ b/java/org/apache/tomcat/util/net/NioEndpoint.java @@ -1158,16 +1158,17 @@ public class NioEndpoint { }//while //timeout - Set keys = selector.keys(); + Set keys = selector.keys(); long now = System.currentTimeMillis(); - for (Iterator iter = keys.iterator(); iter.hasNext(); ) { - SelectionKey key = (SelectionKey) iter.next(); + for (Iterator iter = keys.iterator(); iter.hasNext(); ) { + SelectionKey key = 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) { + boolean isTimedout = (ka.getTimeout()==-1)?(delta > (long) soTimeout):(delta>ka.getTimeout()); + if (isTimedout) { cancelledKey(key); } } @@ -1197,11 +1198,14 @@ public class NioEndpoint { public boolean getWakeUp() { return wakeUp; } public void setWakeUp(boolean wakeUp) { this.wakeUp = wakeUp; } public Object getMutex() {return mutex;} + public void setTimeout(long timeout) {this.timeout = timeout;} + public long getTimeout() {return this.timeout;} protected Object mutex = new Object(); protected boolean wakeUp = false; protected long lastAccess = System.currentTimeMillis(); protected boolean currentAccess = false; protected boolean comet = false; + protected long timeout = -1; } -- 2.11.0