You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by gg...@apache.org on 2017/03/31 19:54:40 UTC

svn commit: r1789741 - in /httpcomponents/httpcore/trunk: httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ httpcore5/src/main/java/org/apache/hc/core5/http/confi...

Author: ggregory
Date: Fri Mar 31 19:54:39 2017
New Revision: 1789741

URL: http://svn.apache.org/viewvc?rev=1789741&view=rev
Log:
In this first use case of TimeValue, the SocketConfig class has been updated to use TimeValue for connectTimeout (was int millis), soTimeout (was int millis), and soLinger (was int seconds). The only part that I am not 100% happy about is how to deal with TimeValue long values and the JRE IO APIs that take int values. See TimeValue.asBoundInt(long).

Modified:
    httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java
    httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java
    httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java

Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java?rev=1789741&r1=1789740&r2=1789741&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/classic/ClassicTestServer.java Fri Mar 31 19:54:39 2017
@@ -99,7 +99,7 @@ public class ClassicTestServer {
         Asserts.check(this.server == null, "Server already running");
         this.server = ServerBootstrap.bootstrap()
                 .setSocketConfig(SocketConfig.custom()
-                        .setSoTimeout(this.timeout)
+                        .setSoTimeout(this.timeout, TimeUnit.MILLISECONDS)
                         .build())
                 .setConnectionFactory(new LoggingConnFactory())
                 .setExceptionListener(new SimpleExceptionListener())

Modified: httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java?rev=1789741&r1=1789740&r2=1789741&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/TestingFramework.java Fri Mar 31 19:54:39 2017
@@ -225,7 +225,7 @@ public class TestingFramework {
          * with the requestHandler.
          */
         final SocketConfig socketConfig = SocketConfig.custom()
-                                          .setSoTimeout(15000)
+                                          .setSoTimeout(15000, TimeUnit.MILLISECONDS)
                                           .build();
 
         final ServerBootstrap serverBootstrap = ServerBootstrap.bootstrap()

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java?rev=1789741&r1=1789740&r2=1789741&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java Fri Mar 31 19:54:39 2017
@@ -27,9 +27,12 @@
 
 package org.apache.hc.core5.http.config;
 
+import java.util.concurrent.TimeUnit;
+
 import org.apache.hc.core5.annotation.Contract;
 import org.apache.hc.core5.annotation.ThreadingBehavior;
 import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.TimeValue;
 
 /**
  * Socket configuration.
@@ -41,10 +44,10 @@ public class SocketConfig {
 
     public static final SocketConfig DEFAULT = new Builder().build();
 
-    private final int connectTimeout;
-    private final int soTimeout;
+    private final TimeValue connectTimeout;
+    private final TimeValue soTimeout;
     private final boolean soReuseAddress;
-    private final int soLinger;
+    private final TimeValue soLinger;
     private final boolean soKeepAlive;
     private final boolean tcpNoDelay;
     private final int sndBufSize;
@@ -52,10 +55,10 @@ public class SocketConfig {
     private final int backlogSize;
 
     SocketConfig(
-            final int connectTimeout,
-            final int soTimeout,
+            final TimeValue connectTimeout,
+            final TimeValue soTimeout,
             final boolean soReuseAddress,
-            final int soLinger,
+            final TimeValue soLinger,
             final boolean soKeepAlive,
             final boolean tcpNoDelay,
             final int sndBufSize,
@@ -83,7 +86,7 @@ public class SocketConfig {
      *
      * @since 5.0
      */
-    public int getConnectTimeout() {
+    public TimeValue getConnectTimeout() {
         return connectTimeout;
     }
 
@@ -96,7 +99,7 @@ public class SocketConfig {
      * @return the default socket timeout value for blocking I/O operations.
      * @see java.net.SocketOptions#SO_TIMEOUT
      */
-    public int getSoTimeout() {
+    public TimeValue getSoTimeout() {
         return soTimeout;
     }
 
@@ -124,7 +127,7 @@ public class SocketConfig {
      * @return the default value of the {@link java.net.SocketOptions#SO_LINGER} parameter.
      * @see java.net.SocketOptions#SO_LINGER
      */
-    public int getSoLinger() {
+    public TimeValue getSoLinger() {
         return soLinger;
     }
 
@@ -235,10 +238,10 @@ public class SocketConfig {
 
     public static class Builder {
 
-        private int connectTimeout;
-        private int soTimeout;
+        private TimeValue connectTimeout;
+        private TimeValue soTimeout;
         private boolean soReuseAddress;
-        private int soLinger;
+        private TimeValue soLinger;
         private boolean soKeepAlive;
         private boolean tcpNoDelay;
         private int sndBufSize;
@@ -246,19 +249,32 @@ public class SocketConfig {
         private int backlogSize;
 
         Builder() {
-            this.soLinger = -1;
+            this.soLinger = new TimeValue(-1, TimeUnit.SECONDS);
             this.tcpNoDelay = true;
         }
 
         /**
          * @since 5.0
          */
-        public Builder setConnectTimeout(final int connectTimeout) {
+        public Builder setConnectTimeout(final TimeValue connectTimeout) {
             this.connectTimeout = connectTimeout;
             return this;
         }
 
-        public Builder setSoTimeout(final int soTimeout) {
+        /**
+         * @since 5.0
+         */
+        public Builder setConnectTimeout(final int connectTimeout, final TimeUnit timeUnit) {
+            this.connectTimeout = new TimeValue(connectTimeout, timeUnit);
+            return this;
+        }
+
+        public Builder setSoTimeout(final int soTimeout, final TimeUnit timeUnit) {
+            this.soTimeout = new TimeValue(soTimeout, timeUnit);
+            return this;
+        }
+
+        public Builder setSoTimeout(final TimeValue soTimeout) {
             this.soTimeout = soTimeout;
             return this;
         }
@@ -268,7 +284,12 @@ public class SocketConfig {
             return this;
         }
 
-        public Builder setSoLinger(final int soLinger) {
+        public Builder setSoLinger(final int soLinger, final TimeUnit timeUnit) {
+            this.soLinger = new TimeValue(soLinger, timeUnit);
+            return this;
+        }
+
+        public Builder setSoLinger(final TimeValue soLinger) {
             this.soLinger = soLinger;
             return this;
         }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java?rev=1789741&r1=1789740&r2=1789741&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java Fri Mar 31 19:54:39 2017
@@ -65,6 +65,7 @@ import org.apache.hc.core5.pool.ConnPool
 import org.apache.hc.core5.pool.ControlledConnPool;
 import org.apache.hc.core5.pool.PoolEntry;
 import org.apache.hc.core5.util.Args;
+import org.apache.hc.core5.util.TimeValue;
 
 /**
  * @since 5.0
@@ -175,7 +176,7 @@ public class HttpRequester implements Au
         final Future<PoolEntry<HttpHost, HttpClientConnection>> leaseFuture = connPool.lease(targetHost, null, null);
         final PoolEntry<HttpHost, HttpClientConnection> poolEntry;
         try {
-            poolEntry = leaseFuture.get(socketConfig.getConnectTimeout(), TimeUnit.MILLISECONDS);
+            poolEntry = leaseFuture.get(socketConfig.getConnectTimeout().toMillis(), TimeUnit.MILLISECONDS);
         } catch (final InterruptedException ex) {
             throw new InterruptedIOException(ex.getMessage());
         } catch (final ExecutionException ex) {
@@ -202,7 +203,7 @@ public class HttpRequester implements Au
                 final Socket socket = createSocket(targetHost);
                 connection = connectFactory.createConnection(socket);
                 poolEntry.assignConnection(connection);
-                socket.connect(toEndpoint(targetHost), socketConfig.getConnectTimeout());
+                socket.connect(toEndpoint(targetHost), TimeValue.asBoundInt(socketConfig.getConnectTimeout().toMillis()));
             }
             final ClassicHttpResponse response = execute(connection, request, context);
             final HttpEntity entity = response.getEntity();

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java?rev=1789741&r1=1789740&r2=1789741&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java Fri Mar 31 19:54:39 2017
@@ -37,6 +37,7 @@ import org.apache.hc.core5.http.config.S
 import org.apache.hc.core5.http.impl.io.HttpService;
 import org.apache.hc.core5.http.io.HttpConnectionFactory;
 import org.apache.hc.core5.http.io.HttpServerConnection;
+import org.apache.hc.core5.util.TimeValue;
 
 /**
  * @since 4.4
@@ -72,7 +73,7 @@ class RequestListener implements Runnabl
         try {
             while (!isTerminated() && !Thread.interrupted()) {
                 final Socket socket = this.serversocket.accept();
-                socket.setSoTimeout(this.socketConfig.getSoTimeout());
+                socket.setSoTimeout(TimeValue.asBoundInt(this.socketConfig.getSoTimeout().toMillis()));
                 socket.setKeepAlive(this.socketConfig.isSoKeepAlive());
                 socket.setTcpNoDelay(this.socketConfig.isTcpNoDelay());
                 if (this.socketConfig.getRcvBufSize() > 0) {
@@ -81,8 +82,8 @@ class RequestListener implements Runnabl
                 if (this.socketConfig.getSndBufSize() > 0) {
                     socket.setSendBufferSize(this.socketConfig.getSndBufSize());
                 }
-                if (this.socketConfig.getSoLinger() >= 0) {
-                    socket.setSoLinger(true, this.socketConfig.getSoLinger());
+                if (this.socketConfig.getSoLinger().toSeconds() >= 0) {
+                    socket.setSoLinger(true, TimeValue.asBoundInt(this.socketConfig.getSoLinger().toSeconds()));
                 }
                 final HttpServerConnection conn = this.connectionFactory.createConnection(socket);
                 final Worker worker = new Worker(this.httpService, conn, this.exceptionListener);

Modified: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java?rev=1789741&r1=1789740&r2=1789741&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java Fri Mar 31 19:54:39 2017
@@ -36,6 +36,15 @@ import java.util.concurrent.TimeUnit;
  */
 public class TimeValue {
 
+    public static int asBoundInt(final long value) {
+        if (value > Integer.MAX_VALUE) {
+            return Integer.MAX_VALUE;
+        } else if (value < Integer.MIN_VALUE) {
+            return Integer.MIN_VALUE;
+        }
+        return (int) value;
+    }
+
     private final long duration;
 
     private final TimeUnit timeUnit;