This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/master by this push:
     new 27f06bc  Move timeout utility method to use it in two additional 
locations
27f06bc is described below

commit 27f06bca46a20cb3faab1221fca7bfd8e88bceda
Author: remm <r...@apache.org>
AuthorDate: Fri Mar 29 10:33:01 2019 +0100

    Move timeout utility method to use it in two additional locations
---
 .../apache/tomcat/util/net/AbstractEndpoint.java   |  5 ++++
 java/org/apache/tomcat/util/net/Nio2Endpoint.java  | 27 +++++++++-------------
 .../tomcat/util/net/NioBlockingSelector.java       | 12 ++--------
 .../apache/tomcat/util/net/SecureNio2Channel.java  | 12 +++++-----
 4 files changed, 24 insertions(+), 32 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/AbstractEndpoint.java 
b/java/org/apache/tomcat/util/net/AbstractEndpoint.java
index 6a6f86e..0b8b683 100644
--- a/java/org/apache/tomcat/util/net/AbstractEndpoint.java
+++ b/java/org/apache/tomcat/util/net/AbstractEndpoint.java
@@ -134,6 +134,11 @@ public abstract class AbstractEndpoint<S,U> {
     }
 
 
+    public static long toTimeout(long timeout) {
+        // Many calls can't do infinite timeout so use Long.MAX_VALUE if 
timeout is <= 0
+        return (timeout > 0) ? timeout : Long.MAX_VALUE;
+    }
+
     // ----------------------------------------------------------------- Fields
 
     /**
diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java 
b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
index 823d9bc..fd31ac1 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -532,7 +532,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                         }
                     }
                 }
-                getSocket().write(buffer, toNio2Timeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS, attachment, this);
+                getSocket().write(buffer, toTimeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS, attachment, this);
             }
 
             @Override
@@ -612,11 +612,11 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                             // Continue writing data using a gathering write
                             ByteBuffer[] array = 
nonBlockingWriteBuffer.toArray(attachment);
                             getSocket().write(array, 0, array.length,
-                                    toNio2Timeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS,
+                                    toTimeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS,
                                     array, gatheringWriteCompletionHandler);
                         } else if (attachment.hasRemaining()) {
                             // Regular write
-                            getSocket().write(attachment, 
toNio2Timeout(getWriteTimeout()),
+                            getSocket().write(attachment, 
toTimeout(getWriteTimeout()),
                                     TimeUnit.MILLISECONDS, attachment, 
writeCompletionHandler);
                         } else {
                             // All data has been written
@@ -662,7 +662,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                             // Continue writing data using a gathering write
                             ByteBuffer[] array = 
nonBlockingWriteBuffer.toArray(attachment);
                             getSocket().write(array, 0, array.length,
-                                    toNio2Timeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS,
+                                    toTimeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS,
                                     array, gatheringWriteCompletionHandler);
                         } else {
                             // All data has been written
@@ -989,7 +989,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                 return CompletionState.ERROR;
             }
             if (timeout == -1) {
-                timeout = toNio2Timeout(getReadTimeout());
+                timeout = toTimeout(getReadTimeout());
             }
             // Disable any regular read notifications caused by 
registerReadInterest
             readNotify = true;
@@ -1058,7 +1058,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                 return CompletionState.ERROR;
             }
             if (timeout == -1) {
-                timeout = toNio2Timeout(getWriteTimeout());
+                timeout = toTimeout(getWriteTimeout());
             }
             // Disable any regular write notifications caused by 
registerWriteInterest
             writeNotify = true;
@@ -1233,7 +1233,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                 }
             } else {
                 Nio2Endpoint.startInline();
-                getSocket().read(to, toNio2Timeout(getReadTimeout()), 
TimeUnit.MILLISECONDS, to,
+                getSocket().read(to, toTimeout(getReadTimeout()), 
TimeUnit.MILLISECONDS, to,
                         readCompletionHandler);
                 Nio2Endpoint.endInline();
                 if (readPending.availablePermits() == 1) {
@@ -1374,7 +1374,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
             // Before doing a blocking flush, make sure that any pending non
             // blocking write has completed.
             try {
-                if (writePending.tryAcquire(toNio2Timeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS)) {
+                if (writePending.tryAcquire(toTimeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS)) {
                     writePending.release();
                 } else {
                     throw new SocketTimeoutException();
@@ -1401,13 +1401,13 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                     if (!nonBlockingWriteBuffer.isEmpty()) {
                         ByteBuffer[] array = 
nonBlockingWriteBuffer.toArray(socketBufferHandler.getWriteBuffer());
                         Nio2Endpoint.startInline();
-                        getSocket().write(array, 0, array.length, 
toNio2Timeout(getWriteTimeout()),
+                        getSocket().write(array, 0, array.length, 
toTimeout(getWriteTimeout()),
                                 TimeUnit.MILLISECONDS, array, 
gatheringWriteCompletionHandler);
                         Nio2Endpoint.endInline();
                     } else if 
(socketBufferHandler.getWriteBuffer().hasRemaining()) {
                         // Regular write
                         Nio2Endpoint.startInline();
-                        
getSocket().write(socketBufferHandler.getWriteBuffer(), 
toNio2Timeout(getWriteTimeout()),
+                        
getSocket().write(socketBufferHandler.getWriteBuffer(), 
toTimeout(getWriteTimeout()),
                                 TimeUnit.MILLISECONDS, 
socketBufferHandler.getWriteBuffer(),
                                 writeCompletionHandler);
                         Nio2Endpoint.endInline();
@@ -1570,7 +1570,7 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                 data.length -= nRead;
                 getSocket().getBufHandler().configureWriteBufferForRead();
                 Nio2Endpoint.startInline();
-                getSocket().write(buffer, toNio2Timeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS,
+                getSocket().write(buffer, toTimeout(getWriteTimeout()), 
TimeUnit.MILLISECONDS,
                         data, sendfileHandler);
                 Nio2Endpoint.endInline();
                 if (data.doneInline) {
@@ -1710,11 +1710,6 @@ public class Nio2Endpoint extends 
AbstractJsseEndpoint<Nio2Channel,AsynchronousS
         }
     }
 
-    public static long toNio2Timeout(long timeout) {
-        // NIO2 can't do infinite timeout so use Long.MAX_VALUE if timeout is 
<= 0
-        return (timeout > 0) ? timeout : Long.MAX_VALUE;
-    }
-
     public static void startInline() {
         inlineCompletion.set(Boolean.TRUE);
     }
diff --git a/java/org/apache/tomcat/util/net/NioBlockingSelector.java 
b/java/org/apache/tomcat/util/net/NioBlockingSelector.java
index 322b172..6702557 100644
--- a/java/org/apache/tomcat/util/net/NioBlockingSelector.java
+++ b/java/org/apache/tomcat/util/net/NioBlockingSelector.java
@@ -114,11 +114,7 @@ public class NioBlockingSelector {
                 try {
                     if ( att.getWriteLatch()==null || 
att.getWriteLatch().getCount()==0) att.startWriteLatch(1);
                     poller.add(att,SelectionKey.OP_WRITE,reference);
-                    if (writeTimeout < 0) {
-                        
att.awaitWriteLatch(Long.MAX_VALUE,TimeUnit.MILLISECONDS);
-                    } else {
-                        
att.awaitWriteLatch(writeTimeout,TimeUnit.MILLISECONDS);
-                    }
+                    
att.awaitWriteLatch(AbstractEndpoint.toTimeout(writeTimeout),TimeUnit.MILLISECONDS);
                 } catch (InterruptedException ignore) {
                     // Ignore
                 }
@@ -184,11 +180,7 @@ public class NioBlockingSelector {
                 try {
                     if ( att.getReadLatch()==null || 
att.getReadLatch().getCount()==0) att.startReadLatch(1);
                     poller.add(att,SelectionKey.OP_READ, reference);
-                    if (readTimeout < 0) {
-                        att.awaitReadLatch(Long.MAX_VALUE, 
TimeUnit.MILLISECONDS);
-                    } else {
-                        att.awaitReadLatch(readTimeout, TimeUnit.MILLISECONDS);
-                    }
+                    
att.awaitReadLatch(AbstractEndpoint.toTimeout(readTimeout), 
TimeUnit.MILLISECONDS);
                 } catch (InterruptedException ignore) {
                     // Ignore
                 }
diff --git a/java/org/apache/tomcat/util/net/SecureNio2Channel.java 
b/java/org/apache/tomcat/util/net/SecureNio2Channel.java
index 6eae95d..cb670ae 100644
--- a/java/org/apache/tomcat/util/net/SecureNio2Channel.java
+++ b/java/org/apache/tomcat/util/net/SecureNio2Channel.java
@@ -256,7 +256,7 @@ public class SecureNio2Channel extends Nio2Channel  {
                         return 0;
                     } else {
                         if (async) {
-                            sc.write(netOutBuffer, 
Nio2Endpoint.toNio2Timeout(timeout),
+                            sc.write(netOutBuffer, 
AbstractEndpoint.toTimeout(timeout),
                                     TimeUnit.MILLISECONDS, socket, 
handshakeWriteCompletionHandler);
                         } else {
                             try {
@@ -294,7 +294,7 @@ public class SecureNio2Channel extends Nio2Channel  {
                     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(timeout),
+                            sc.write(netOutBuffer, 
AbstractEndpoint.toTimeout(timeout),
                                     TimeUnit.MILLISECONDS, socket, 
handshakeWriteCompletionHandler);
                         } else {
                             try {
@@ -326,7 +326,7 @@ public class SecureNio2Channel extends Nio2Channel  {
                         }
                         //read more data
                         if (async) {
-                            sc.read(netInBuffer, 
Nio2Endpoint.toNio2Timeout(timeout),
+                            sc.read(netInBuffer, 
AbstractEndpoint.toTimeout(timeout),
                                     TimeUnit.MILLISECONDS, socket, 
handshakeReadCompletionHandler);
                         } else {
                             try {
@@ -371,7 +371,7 @@ public class SecureNio2Channel extends Nio2Channel  {
         // an optimisation for the typical case so we don't create an
         // SNIExtractor only to discover there is no data to process
         if (netInBuffer.position() == 0) {
-            sc.read(netInBuffer, 
Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()),
+            sc.read(netInBuffer, 
AbstractEndpoint.toTimeout(endpoint.getConnectionTimeout()),
                     TimeUnit.MILLISECONDS, socket, 
handshakeReadCompletionHandler);
             return 1;
         }
@@ -387,7 +387,7 @@ public class SecureNio2Channel extends Nio2Channel  {
                     Integer.toString(newLimit)));
 
             netInBuffer = ByteBufferUtils.expand(netInBuffer, newLimit);
-            sc.read(netInBuffer, 
Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()),
+            sc.read(netInBuffer, 
AbstractEndpoint.toTimeout(endpoint.getConnectionTimeout()),
                     TimeUnit.MILLISECONDS, socket, 
handshakeReadCompletionHandler);
             return 1;
         }
@@ -405,7 +405,7 @@ public class SecureNio2Channel extends Nio2Channel  {
             clientRequestedCiphers = extractor.getClientRequestedCiphers();
             break;
         case NEED_READ:
-            sc.read(netInBuffer, 
Nio2Endpoint.toNio2Timeout(endpoint.getConnectionTimeout()),
+            sc.read(netInBuffer, 
AbstractEndpoint.toTimeout(endpoint.getConnectionTimeout()),
                     TimeUnit.MILLISECONDS, socket, 
handshakeReadCompletionHandler);
             return 1;
         case UNDERFLOW:


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to