Author: remm Date: Wed May 30 20:46:02 2018 New Revision: 1832572 URL: http://svn.apache.org/viewvc?rev=1832572&view=rev Log: Rework timeout a bit, to align with the API (read and write from async channel without timeout use 0 in NIO2; Future.get(infinite) is not as efficient as get()).
Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Channel.java tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Channel.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Channel.java?rev=1832572&r1=1832571&r2=1832572&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Channel.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Channel.java Wed May 30 20:46:02 2018 @@ -146,7 +146,7 @@ public class Nio2Channel implements Asyn @Override public <A> void read(ByteBuffer dst, A attachment, CompletionHandler<Integer, ? super A> handler) { - read(dst, Integer.MAX_VALUE, TimeUnit.MILLISECONDS, attachment, handler); + read(dst, 0L, TimeUnit.MILLISECONDS, attachment, handler); } public <A> void read(ByteBuffer dst, @@ -169,7 +169,7 @@ public class Nio2Channel implements Asyn @Override public <A> void write(ByteBuffer src, A attachment, CompletionHandler<Integer, ? super A> handler) { - write(src, Integer.MAX_VALUE, TimeUnit.MILLISECONDS, attachment, handler); + write(src, 0L, TimeUnit.MILLISECONDS, attachment, handler); } public <A> void write(ByteBuffer src, long timeout, TimeUnit unit, A attachment, Modified: tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java?rev=1832572&r1=1832571&r2=1832572&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/net/Nio2Endpoint.java Wed May 30 20:46:02 2018 @@ -1117,7 +1117,12 @@ public class Nio2Endpoint extends Abstra if (block) { try { integer = getSocket().read(to); - nRead = integer.get(toNio2Timeout(getReadTimeout()), TimeUnit.MILLISECONDS).intValue(); + long timeout = getReadTimeout(); + if (timeout > 0) { + nRead = integer.get(timeout, TimeUnit.MILLISECONDS).intValue(); + } else { + nRead = integer.get().intValue(); + } } catch (ExecutionException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); @@ -1229,8 +1234,15 @@ public class Nio2Endpoint extends Abstra try { do { integer = getSocket().write(from); - if (integer.get(toNio2Timeout(getWriteTimeout()), TimeUnit.MILLISECONDS).intValue() < 0) { - throw new EOFException(sm.getString("iob.failedwrite")); + long timeout = getWriteTimeout(); + if (timeout > 0) { + if (integer.get(timeout, TimeUnit.MILLISECONDS).intValue() < 0) { + throw new EOFException(sm.getString("iob.failedwrite")); + } + } else { + if (integer.get().intValue() < 0) { + throw new EOFException(sm.getString("iob.failedwrite")); + } } } while (from.hasRemaining()); } catch (ExecutionException e) { Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java?rev=1832572&r1=1832571&r2=1832572&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java Wed May 30 20:46:02 2018 @@ -231,6 +231,7 @@ public class SecureNio2Channel extends N } SSLEngineResult handshake = null; + long timeout = endpoint.getConnectionTimeout(); while (!handshakeComplete) { switch (handshakeStatus) { @@ -255,12 +256,15 @@ public class SecureNio2Channel extends N return 0; } else { if (async) { - sc.write(netOutBuffer, Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()), + sc.write(netOutBuffer, Nio2Endpoint.toNio2Timeout(timeout), TimeUnit.MILLISECONDS, socket, handshakeWriteCompletionHandler); } else { try { - sc.write(netOutBuffer).get(Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()), - TimeUnit.MILLISECONDS); + if (timeout > 0) { + sc.write(netOutBuffer).get(timeout, TimeUnit.MILLISECONDS); + } else { + sc.write(netOutBuffer).get(); + } } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new IOException(sm.getString("channel.nio.ssl.handshakeError")); } @@ -290,12 +294,15 @@ public class SecureNio2Channel extends N if (handshakeStatus != HandshakeStatus.NEED_UNWRAP || netOutBuffer.remaining() > 0) { //should actually return OP_READ if we have NEED_UNWRAP if (async) { - sc.write(netOutBuffer, Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()), + sc.write(netOutBuffer, Nio2Endpoint.toNio2Timeout(timeout), TimeUnit.MILLISECONDS, socket, handshakeWriteCompletionHandler); } else { try { - sc.write(netOutBuffer).get(Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()), - TimeUnit.MILLISECONDS); + if (timeout > 0) { + sc.write(netOutBuffer).get(timeout, TimeUnit.MILLISECONDS); + } else { + sc.write(netOutBuffer).get(); + } } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new IOException(sm.getString("channel.nio.ssl.handshakeError")); } @@ -319,12 +326,16 @@ public class SecureNio2Channel extends N } //read more data if (async) { - sc.read(netInBuffer, Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()), + sc.read(netInBuffer, Nio2Endpoint.toNio2Timeout(timeout), TimeUnit.MILLISECONDS, socket, handshakeReadCompletionHandler); } else { try { - int read = sc.read(netInBuffer).get(Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()), - TimeUnit.MILLISECONDS).intValue(); + int read; + if (timeout > 0) { + read = sc.read(netInBuffer).get(timeout, TimeUnit.MILLISECONDS).intValue(); + } else { + read = sc.read(netInBuffer).get().intValue(); + } if (read == -1) { throw new EOFException(); } @@ -564,11 +575,17 @@ public class SecureNio2Channel extends N if (closing) return; closing = true; sslEngine.closeOutbound(); + long timeout = endpoint.getConnectionTimeout(); try { - if (!flush().get(Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()), - TimeUnit.MILLISECONDS).booleanValue()) { - throw new IOException(sm.getString("channel.nio.ssl.remainingDataDuringClose")); + if (timeout > 0) { + if (!flush().get(timeout, TimeUnit.MILLISECONDS).booleanValue()) { + throw new IOException(sm.getString("channel.nio.ssl.remainingDataDuringClose")); + } + } else { + if (!flush().get().booleanValue()) { + throw new IOException(sm.getString("channel.nio.ssl.remainingDataDuringClose")); + } } } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new IOException(sm.getString("channel.nio.ssl.remainingDataDuringClose"), e); @@ -587,9 +604,14 @@ public class SecureNio2Channel extends N netOutBuffer.flip(); //if there is data to be written try { - if (!flush().get(Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()), - TimeUnit.MILLISECONDS).booleanValue()) { - throw new IOException(sm.getString("channel.nio.ssl.remainingDataDuringClose")); + if (timeout > 0) { + if (!flush().get(timeout, TimeUnit.MILLISECONDS).booleanValue()) { + throw new IOException(sm.getString("channel.nio.ssl.remainingDataDuringClose")); + } + } else { + if (!flush().get().booleanValue()) { + throw new IOException(sm.getString("channel.nio.ssl.remainingDataDuringClose")); + } } } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new IOException(sm.getString("channel.nio.ssl.remainingDataDuringClose"), e); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org