Author: remm Date: Mon Oct 26 15:24:26 2015 New Revision: 1710628 URL: http://svn.apache.org/viewvc?rev=1710628&view=rev Log: Add a heuristic for NIO2 to avoid a useless SSL engine call on every read: if the previous read filled out the socket input buffer, try to unwrap first (it is most likely unwrap will produce more bytes in that case).
Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java 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=1710628&r1=1710627&r2=1710628&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/net/SecureNio2Channel.java Mon Oct 26 15:24:26 2015 @@ -65,6 +65,8 @@ public class SecureNio2Channel extends N private volatile boolean handshakeComplete; private volatile HandshakeStatus handshakeStatus; //gets set by handshake + private volatile boolean unwrapBeforeRead = false; + protected boolean closed; protected boolean closing; @@ -572,9 +574,14 @@ public class SecureNio2Channel extends N private class FutureRead implements Future<Integer> { private final ByteBuffer dst; - private final Future<Integer> integer = null; + private final Future<Integer> integer; private FutureRead(ByteBuffer dst) { this.dst = dst; + if (unwrapBeforeRead || netInBuffer.position() > 0) { + this.integer = null; + } else { + this.integer = sc.read(netInBuffer); + } } @Override public boolean cancel(boolean mayInterruptIfRunning) { @@ -654,6 +661,11 @@ public class SecureNio2Channel extends N throw new ExecutionException(new IOException(sm.getString("channel.nio.ssl.unwrapFail", unwrap.getStatus()))); } } while ((netInBuffer.position() != 0)); //continue to unwrapping as long as the input buffer has stuff + if (!dst.hasRemaining()) { + unwrapBeforeRead = true; + } else { + unwrapBeforeRead = false; + } return Integer.valueOf(read); } } @@ -820,6 +832,11 @@ public class SecureNio2Channel extends N } // continue to unwrap as long as the input buffer has stuff } while (netInBuffer.position() != 0); + if (!dst.hasRemaining()) { + unwrapBeforeRead = true; + } else { + unwrapBeforeRead = false; + } // If everything is OK, so complete handler.completed(Integer.valueOf(read), attach); } catch (Exception e) { @@ -832,7 +849,11 @@ public class SecureNio2Channel extends N handler.failed(exc, attach); } }; - readCompletionHandler.completed(Integer.valueOf(netInBuffer.position()), attachment); + if (unwrapBeforeRead || netInBuffer.position() > 0) { + readCompletionHandler.completed(Integer.valueOf(netInBuffer.position()), attachment); + } else { + sc.read(netInBuffer, timeout, unit, attachment, readCompletionHandler); + } } @Override @@ -893,6 +914,16 @@ public class SecureNio2Channel extends N throw new IOException(sm.getString("channel.nio.ssl.unwrapFail", unwrap.getStatus())); } } while ((netInBuffer.position() != 0)); //continue to unwrapping as long as the input buffer has stuff + int capacity = 0; + final int endOffset = offset + length; + for (int i = offset; i < endOffset; i++) { + capacity += dsts[i].remaining(); + } + if (capacity == 0) { + unwrapBeforeRead = true; + } else { + unwrapBeforeRead = false; + } // If everything is OK, so complete handler.completed(Long.valueOf(read), attach); } catch (Exception e) { @@ -905,7 +936,7 @@ public class SecureNio2Channel extends N handler.failed(exc, attach); } }; - if (netInBuffer.position() > 0) { + if (unwrapBeforeRead || netInBuffer.position() > 0) { readCompletionHandler.completed(Integer.valueOf(netInBuffer.position()), attachment); } else { sc.read(netInBuffer, timeout, unit, attachment, readCompletionHandler); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org