Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id AB7EF200CB3 for ; Mon, 26 Jun 2017 22:53:53 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id AA186160BDE; Mon, 26 Jun 2017 20:53:53 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 026B0160BDA for ; Mon, 26 Jun 2017 22:53:51 +0200 (CEST) Received: (qmail 69978 invoked by uid 500); 26 Jun 2017 20:53:51 -0000 Mailing-List: contact commits-help@hc.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "HttpComponents Project" Delivered-To: mailing list commits@hc.apache.org Received: (qmail 69969 invoked by uid 99); 26 Jun 2017 20:53:50 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 26 Jun 2017 20:53:50 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D23B3DFE22; Mon, 26 Jun 2017 20:53:50 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: olegk@apache.org To: commits@hc.apache.org Message-Id: <271d04a0f2f4495ba3d1b52133d7f4de@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: httpcomponents-core git commit: HTTPCORE-454: use Timeout class to represent timeout values Date: Mon, 26 Jun 2017 20:53:50 +0000 (UTC) archived-at: Mon, 26 Jun 2017 20:53:53 -0000 Repository: httpcomponents-core Updated Branches: refs/heads/master 8f1b4f6d1 -> d733e0ee9 HTTPCORE-454: use Timeout class to represent timeout values Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/repo Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/commit/d733e0ee Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/tree/d733e0ee Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-core/diff/d733e0ee Branch: refs/heads/master Commit: d733e0ee9571286842e0c098df7c3044d2158d58 Parents: 8f1b4f6 Author: Oleg Kalnichevski Authored: Wed Jun 21 10:09:27 2017 +0200 Committer: Oleg Kalnichevski Committed: Mon Jun 26 22:47:43 2017 +0200 ---------------------------------------------------------------------- .../framework/ClassicTestClientAdapter.java | 6 +-- .../nio/Http1ServerAndRequesterTest.java | 3 +- .../nio/Http2ProtocolNegotiationTest.java | 4 +- .../nio/Http2ServerAndRequesterTest.java | 3 +- .../examples/ClassicGetExecutionExample.java | 4 +- .../examples/ClassicPostExecutionExample.java | 4 +- ...ClassicPostWithTrailersExecutionExample.java | 4 +- .../examples/ClassicReverseProxyExample.java | 3 +- .../hc/core5/http/config/SocketConfig.java | 19 ++++---- .../impl/bootstrap/AsyncRequesterBootstrap.java | 6 +-- .../http/impl/bootstrap/HttpRequester.java | 9 ++-- .../http/impl/bootstrap/RequesterBootstrap.java | 6 +-- .../org/apache/hc/core5/pool/LeaseRequest.java | 15 ++---- .../org/apache/hc/core5/pool/PoolEntry.java | 4 +- .../apache/hc/core5/pool/StrictConnPool.java | 7 +-- .../hc/core5/reactor/IOReactorConfig.java | 19 ++++---- .../org/apache/hc/core5/util/TimeValue.java | 23 +++++++-- .../java/org/apache/hc/core5/util/Timeout.java | 49 ++++++-------------- .../hc/core5/pool/TestStrictConnPool.java | 13 +++--- .../org/apache/hc/core5/util/TestTimeout.java | 23 +-------- 20 files changed, 97 insertions(+), 127 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java ---------------------------------------------------------------------- diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java index af26940..e2cf0a7 100644 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java +++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/framework/ClassicTestClientAdapter.java @@ -45,7 +45,7 @@ import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.message.BasicClassicHttpRequest; import org.apache.hc.core5.http.protocol.HttpCoreContext; import org.apache.hc.core5.testing.classic.ClassicTestClient; -import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; public class ClassicTestClientAdapter extends ClientPOJOAdapter { @@ -68,9 +68,9 @@ public class ClassicTestClientAdapter extends ClientPOJOAdapter { throw new HttpException("Request method should be set."); } - final TimeValue timeout; + final Timeout timeout; if (request.containsKey(TIMEOUT)) { - timeout = TimeValue.ofMillis((long) request.get(TIMEOUT)); + timeout = Timeout.ofMillis((long) request.get(TIMEOUT)); } else { timeout = null; } http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java ---------------------------------------------------------------------- diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java index 52932b9..9fb4ea5 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http1ServerAndRequesterTest.java @@ -62,6 +62,7 @@ import org.apache.hc.core5.reactor.ExceptionEvent; import org.apache.hc.core5.reactor.IOReactorConfig; import org.apache.hc.core5.reactor.ListenerEndpoint; import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hamcrest.CoreMatchers; @@ -72,7 +73,7 @@ import org.junit.rules.ExternalResource; public class Http1ServerAndRequesterTest { - private static final TimeValue TIMEOUT = TimeValue.ofSeconds(30); + private static final Timeout TIMEOUT = Timeout.ofSeconds(30); private final Logger log = LogManager.getLogger(getClass()); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ProtocolNegotiationTest.java ---------------------------------------------------------------------- diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ProtocolNegotiationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ProtocolNegotiationTest.java index faf8868..32d062e 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ProtocolNegotiationTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ProtocolNegotiationTest.java @@ -59,7 +59,7 @@ import org.apache.hc.core5.reactor.ExceptionEvent; import org.apache.hc.core5.reactor.IOReactorConfig; import org.apache.hc.core5.reactor.ListenerEndpoint; import org.apache.hc.core5.testing.SSLTestContexts; -import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hamcrest.CoreMatchers; @@ -73,7 +73,7 @@ import org.junit.rules.ExternalResource; public class Http2ProtocolNegotiationTest { - private static final TimeValue TIMEOUT = TimeValue.ofSeconds(30); + private static final Timeout TIMEOUT = Timeout.ofSeconds(30); private final Logger log = LogManager.getLogger(getClass()); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java ---------------------------------------------------------------------- diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java index b87fdb8..559932e 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/Http2ServerAndRequesterTest.java @@ -57,6 +57,7 @@ import org.apache.hc.core5.reactor.ExceptionEvent; import org.apache.hc.core5.reactor.IOReactorConfig; import org.apache.hc.core5.reactor.ListenerEndpoint; import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hamcrest.CoreMatchers; @@ -81,7 +82,7 @@ public class Http2ServerAndRequesterTest { }); } - private static final TimeValue TIMEOUT = TimeValue.ofSeconds(30); + private static final Timeout TIMEOUT = Timeout.ofSeconds(30); private final HttpVersionPolicy versionPolicy; http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java index abb910e..71a8867 100644 --- a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java +++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java @@ -44,7 +44,7 @@ import org.apache.hc.core5.http.message.BasicClassicHttpRequest; import org.apache.hc.core5.http.message.RequestLine; import org.apache.hc.core5.http.message.StatusLine; import org.apache.hc.core5.http.protocol.HttpCoreContext; -import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; /** * Example of GET requests execution using classic I/O. @@ -89,7 +89,7 @@ public class ClassicGetExecutionExample { for (int i = 0; i < requestUris.length; i++) { String requestUri = requestUris[i]; ClassicHttpRequest request = new BasicClassicHttpRequest("GET", target, requestUri); - try (ClassicHttpResponse response = httpRequester.execute(target, request, TimeValue.ofSeconds(5), coreContext)) { + try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), coreContext)) { System.out.println(requestUri + "->" + response.getCode()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java index 61e3716..0d95e84 100644 --- a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java +++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java @@ -51,7 +51,7 @@ import org.apache.hc.core5.http.message.BasicClassicHttpRequest; import org.apache.hc.core5.http.message.RequestLine; import org.apache.hc.core5.http.message.StatusLine; import org.apache.hc.core5.http.protocol.HttpCoreContext; -import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; /** * Example of POST requests execution using classic I/O. @@ -109,7 +109,7 @@ public class ClassicPostExecutionExample { for (int i = 0; i < requestBodies.length; i++) { ClassicHttpRequest request = new BasicClassicHttpRequest("POST", target,requestUri); request.setEntity(requestBodies[i]); - try (ClassicHttpResponse response = httpRequester.execute(target, request, TimeValue.ofSeconds(5), coreContext)) { + try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), coreContext)) { System.out.println(requestUri + "->" + response.getCode()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java index a800a2b..112b517 100644 --- a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java +++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicPostWithTrailersExecutionExample.java @@ -43,7 +43,7 @@ import org.apache.hc.core5.http.io.entity.StringEntity; import org.apache.hc.core5.http.message.BasicClassicHttpRequest; import org.apache.hc.core5.http.message.BasicHeader; import org.apache.hc.core5.http.protocol.HttpCoreContext; -import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; /** * Example of POST request with trailers execution using classic I/O. @@ -67,7 +67,7 @@ public class ClassicPostWithTrailersExecutionExample { .build(); System.out.println(">> Request URI: " + request.getUri()); - try (ClassicHttpResponse response = httpRequester.execute(target, request, TimeValue.ofSeconds(5), coreContext)) { + try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), coreContext)) { System.out.println(requestUri + "->" + response.getCode()); System.out.println(EntityUtils.toString(response.getEntity())); System.out.println("=============="); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java index ca4723f..961c2d9 100644 --- a/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java +++ b/httpcore5/src/examples/org/apache/hc/core5/http/examples/ClassicReverseProxyExample.java @@ -63,6 +63,7 @@ import org.apache.hc.core5.pool.ConnPoolListener; import org.apache.hc.core5.pool.ConnPoolStats; import org.apache.hc.core5.pool.PoolStats; import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; /** * Example of embedded HTTP/1.1 reverse proxy using classic I/O. @@ -220,7 +221,7 @@ public class ClassicReverseProxyExample { } } final ClassicHttpResponse incomingResponse = requester.execute( - targetHost, outgoingRequest, TimeValue.ofMinutes(1), clientContext); + targetHost, outgoingRequest, Timeout.ofMinutes(1), clientContext); outgoingResponse.setCode(incomingResponse.getCode()); for (Iterator
it = incomingResponse.headerIterator(); it.hasNext(); ) { Header header = it.next(); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java b/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java index 91d60da..855ff4b 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/config/SocketConfig.java @@ -33,6 +33,7 @@ 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; +import org.apache.hc.core5.util.Timeout; /** * Socket configuration. @@ -44,7 +45,7 @@ public class SocketConfig { public static final SocketConfig DEFAULT = new Builder().build(); - private final TimeValue soTimeout; + private final Timeout soTimeout; private final boolean soReuseAddress; private final TimeValue soLinger; private final boolean soKeepAlive; @@ -54,7 +55,7 @@ public class SocketConfig { private final int backlogSize; SocketConfig( - final TimeValue soTimeout, + final Timeout soTimeout, final boolean soReuseAddress, final TimeValue soLinger, final boolean soKeepAlive, @@ -82,7 +83,7 @@ public class SocketConfig { * @return the default socket timeout value for blocking I/O operations. * @see java.net.SocketOptions#SO_TIMEOUT */ - public TimeValue getSoTimeout() { + public Timeout getSoTimeout() { return soTimeout; } @@ -219,7 +220,7 @@ public class SocketConfig { public static class Builder { - private TimeValue soTimeout; + private Timeout soTimeout; private boolean soReuseAddress; private TimeValue soLinger; private boolean soKeepAlive; @@ -229,7 +230,7 @@ public class SocketConfig { private int backlogSize; Builder() { - this.soTimeout = TimeValue.ZERO_MILLISECONDS; + this.soTimeout = Timeout.ZERO_MILLISECONDS; this.soReuseAddress = false; this.soLinger = TimeValue.NEG_ONE_SECONDS; this.soKeepAlive = false; @@ -240,11 +241,11 @@ public class SocketConfig { } public Builder setSoTimeout(final int soTimeout, final TimeUnit timeUnit) { - this.soTimeout = TimeValue.of(soTimeout, timeUnit); + this.soTimeout = Timeout.of(soTimeout, timeUnit); return this; } - public Builder setSoTimeout(final TimeValue soTimeout) { + public Builder setSoTimeout(final Timeout soTimeout) { this.soTimeout = soTimeout; return this; } @@ -255,7 +256,7 @@ public class SocketConfig { } public Builder setSoLinger(final int soLinger, final TimeUnit timeUnit) { - this.soLinger = TimeValue.of(soLinger, timeUnit); + this.soLinger = Timeout.of(soLinger, timeUnit); return this; } @@ -300,7 +301,7 @@ public class SocketConfig { public SocketConfig build() { return new SocketConfig( - soTimeout != null ? soTimeout : TimeValue.ZERO_MILLISECONDS, + Timeout.defaultsToDisabled(soTimeout), soReuseAddress, soLinger != null ? soLinger : TimeValue.NEG_ONE_SECONDS, soKeepAlive, tcpNoDelay, sndBufSize, rcvBufSize, backlogSize); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequesterBootstrap.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequesterBootstrap.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequesterBootstrap.java index 6e85d0c..55c6bb9 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequesterBootstrap.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/AsyncRequesterBootstrap.java @@ -45,7 +45,7 @@ import org.apache.hc.core5.reactor.IOEventHandlerFactory; import org.apache.hc.core5.reactor.IOReactorConfig; import org.apache.hc.core5.reactor.IOSession; import org.apache.hc.core5.reactor.IOSessionListener; -import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; /** * @since 5.0 @@ -59,7 +59,7 @@ public class AsyncRequesterBootstrap { private ConnectionReuseStrategy connStrategy; private int defaultMaxPerRoute; private int maxTotal; - private TimeValue timeToLive; + private Timeout timeToLive; private ConnPoolPolicy connPoolPolicy; private TlsStrategy tlsStrategy; private Decorator ioSessionDecorator; @@ -124,7 +124,7 @@ public class AsyncRequesterBootstrap { return this; } - public final AsyncRequesterBootstrap setTimeToLive(final TimeValue timeToLive) { + public final AsyncRequesterBootstrap setTimeToLive(final Timeout timeToLive) { this.timeToLive = timeToLive; return this; } http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java index e158573..3a82785 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java @@ -67,7 +67,7 @@ import org.apache.hc.core5.pool.ConnPoolControl; 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; +import org.apache.hc.core5.util.Timeout; /** * @since 5.0 @@ -188,14 +188,13 @@ public class HttpRequester implements GracefullyCloseable { public ClassicHttpResponse execute( final HttpHost targetHost, final ClassicHttpRequest request, - final TimeValue connectTimeout, + final Timeout connectTimeout, final HttpContext context) throws HttpException, IOException { Args.notNull(targetHost, "HTTP host"); Args.notNull(request, "HTTP request"); final Future> leaseFuture = connPool.lease(targetHost, null, null); final PoolEntry poolEntry; - final TimeValue timeout = connectTimeout != null ? connectTimeout : TimeValue.ZERO_MILLISECONDS; - + final Timeout timeout = Timeout.defaultsToDisabled(connectTimeout); try { poolEntry = leaseFuture.get(timeout.getDuration(), timeout.getTimeUnit()); } catch (final InterruptedException ex) { @@ -300,7 +299,7 @@ public class HttpRequester implements GracefullyCloseable { public T execute( final HttpHost targetHost, final ClassicHttpRequest request, - final TimeValue connectTimeout, + final Timeout connectTimeout, final HttpContext context, final ResponseHandler responseHandler) throws HttpException, IOException { try (final ClassicHttpResponse response = execute(targetHost, request, connectTimeout, context)) { http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java index 435659d..4110d0e 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequesterBootstrap.java @@ -44,7 +44,7 @@ import org.apache.hc.core5.http.protocol.HttpProcessor; import org.apache.hc.core5.pool.ConnPoolListener; import org.apache.hc.core5.pool.ConnPoolPolicy; import org.apache.hc.core5.pool.StrictConnPool; -import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; /** * @since 5.0 @@ -58,7 +58,7 @@ public class RequesterBootstrap { private SSLSocketFactory sslSocketFactory; private int defaultMaxPerRoute; private int maxTotal; - private TimeValue timeToLive; + private Timeout timeToLive; private ConnPoolPolicy connPoolPolicy; private Http1StreamListener streamListener; private ConnPoolListener connPoolListener; @@ -114,7 +114,7 @@ public class RequesterBootstrap { return this; } - public final RequesterBootstrap setTimeToLive(final TimeValue timeToLive) { + public final RequesterBootstrap setTimeToLive(final Timeout timeToLive) { this.timeToLive = timeToLive; return this; } http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/pool/LeaseRequest.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/LeaseRequest.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/LeaseRequest.java index ec52d37..42977fb 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/pool/LeaseRequest.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/LeaseRequest.java @@ -32,7 +32,7 @@ import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.ThreadingBehavior; import org.apache.hc.core5.concurrent.BasicFuture; import org.apache.hc.core5.io.GracefullyCloseable; -import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; @Contract(threading = ThreadingBehavior.SAFE_CONDITIONAL) class LeaseRequest { @@ -45,15 +45,6 @@ class LeaseRequest { private volatile PoolEntry result; private volatile Exception ex; - static long calculateDeadline(final long currentTimeMillis, final TimeValue timeValue) { - final long time = TimeValue.isPositive(timeValue) ? currentTimeMillis + timeValue.toMillis() : Long.MAX_VALUE; - if (time < 0 && TimeValue.isPositive(timeValue)) { - return Long.MAX_VALUE; - } else { - return time; - } - } - /** * Constructor * @@ -65,12 +56,12 @@ class LeaseRequest { public LeaseRequest( final T route, final Object state, - final TimeValue requestTimeout, + final Timeout requestTimeout, final BasicFuture> future) { super(); this.route = route; this.state = state; - this.deadline = calculateDeadline(System.currentTimeMillis(), requestTimeout); + this.deadline = Timeout.calculateDeadline(System.currentTimeMillis(), requestTimeout); this.future = future; this.completed = new AtomicBoolean(false); } http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java index 4572bb4..3656de0 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/PoolEntry.java @@ -119,7 +119,7 @@ public final class PoolEntry { if (this.connRef.compareAndSet(null, conn)) { this.created = currentTimeMillis(); this.updated = this.created; - this.validityDeadline = LeaseRequest.calculateDeadline(this.created, this.timeToLive); + this.validityDeadline = TimeValue.calculateDeadline(this.created, this.timeToLive); this.expiry = this.validityDeadline; this.state = null; } else { @@ -148,7 +148,7 @@ public final class PoolEntry { public void updateExpiry(final TimeValue expiryTime) { Args.notNull(expiryTime, "Expiry time"); final long currentTime = System.currentTimeMillis(); - final long newExpiry = LeaseRequest.calculateDeadline(currentTime, expiryTime); + final long newExpiry = TimeValue.calculateDeadline(currentTime, expiryTime); this.expiry = Math.min(newExpiry, getValidityDeadline()); this.updated = currentTime; } http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java index d8efa59..32faa13 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java @@ -51,6 +51,7 @@ import org.apache.hc.core5.util.Args; import org.apache.hc.core5.util.Asserts; import org.apache.hc.core5.util.LangUtils; import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; /** * Connection pool with strict max connection limit guarantees. @@ -148,7 +149,7 @@ public class StrictConnPool implements Control public Future> lease( final T route, final Object state, - final TimeValue requestTimeout, + final Timeout requestTimeout, final FutureCallback> callback) { Args.notNull(route, "Route"); Args.notNull(requestTimeout, "Request timeout"); @@ -173,11 +174,11 @@ public class StrictConnPool implements Control @Override public Future> lease(final T route, final Object state, final FutureCallback> callback) { - return lease(route, state, TimeValue.NEG_ONE_MILLISECONDS, callback); + return lease(route, state, Timeout.DISABLED, callback); } public Future> lease(final T route, final Object state) { - return lease(route, state, TimeValue.NEG_ONE_MILLISECONDS, null); + return lease(route, state, Timeout.DISABLED, null); } @Override http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java index d4f3ac8..ba1d4ea 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/IOReactorConfig.java @@ -33,6 +33,7 @@ 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; +import org.apache.hc.core5.util.Timeout; /** * I/O reactor configuration parameters. @@ -48,7 +49,7 @@ public final class IOReactorConfig { private final long selectInterval; private final int ioThreadCount; - private final TimeValue soTimeout; + private final Timeout soTimeout; private final boolean soReuseAddress; private final TimeValue soLinger; private final boolean soKeepAlive; @@ -60,7 +61,7 @@ public final class IOReactorConfig { IOReactorConfig( final long selectInterval, final int ioThreadCount, - final TimeValue soTimeout, + final Timeout soTimeout, final boolean soReuseAddress, final TimeValue soLinger, final boolean soKeepAlive, @@ -107,7 +108,7 @@ public final class IOReactorConfig { * * @see java.net.SocketOptions#SO_TIMEOUT */ - public TimeValue getSoTimeout() { + public Timeout getSoTimeout() { return soTimeout; } @@ -217,7 +218,7 @@ public final class IOReactorConfig { private long selectInterval; private int ioThreadCount; - private TimeValue soTimeout; + private Timeout soTimeout; private boolean soReuseAddress; private TimeValue soLinger; private boolean soKeepAlive; @@ -229,7 +230,7 @@ public final class IOReactorConfig { Builder() { this.selectInterval = 1000; this.ioThreadCount = AVAIL_PROCS; - this.soTimeout = TimeValue.ZERO_MILLISECONDS; + this.soTimeout = Timeout.ZERO_MILLISECONDS; this.soReuseAddress = false; this.soLinger = TimeValue.NEG_ONE_SECONDS; this.soKeepAlive = false; @@ -250,11 +251,11 @@ public final class IOReactorConfig { } public Builder setSoTimeout(final int soTimeout, final TimeUnit timeUnit) { - this.soTimeout = TimeValue.of(soTimeout, timeUnit);; + this.soTimeout = Timeout.of(soTimeout, timeUnit);; return this; } - public Builder setSoTimeout(final TimeValue soTimeout) { + public Builder setSoTimeout(final Timeout soTimeout) { this.soTimeout = soTimeout; return this; } @@ -302,9 +303,9 @@ public final class IOReactorConfig { public IOReactorConfig build() { return new IOReactorConfig( selectInterval, ioThreadCount, - soTimeout != null ? soTimeout : TimeValue.ZERO_MILLISECONDS, + Timeout.defaultsToDisabled(soTimeout), soReuseAddress, - soLinger != null ? soLinger : TimeValue.NEG_ONE_SECONDS, + TimeValue.defaultsToNegativeOneMillisecond(soLinger), soKeepAlive, tcpNoDelay, sndBufSize, rcvBufSize, backlogSize); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java b/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java index a8646ca..33a8e7a 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java @@ -79,7 +79,7 @@ public class TimeValue { * may be {@code null} * @return {@code timeValue} or {@code defaultValue} */ - public static TimeValue defaultsTo(final TimeValue timeValue, final TimeValue defaultValue) { + public static T defaultsTo(final T timeValue, final T defaultValue) { return timeValue != null ? timeValue : defaultValue; } @@ -187,6 +187,23 @@ public class TimeValue { TimeUnit.valueOf(split[1].trim().toUpperCase(Locale.ROOT))); } + /** + * Calculates the deadline with the current time in milliseconds and the given time value. + * Non-positive time value represents an indefinite timeout without a deadline. + * + * @param currentTimeMillis current time + * @param timeValue time value + * @return deadline in milliseconds + */ + public static long calculateDeadline(final long currentTimeMillis, final TimeValue timeValue) { + if (TimeValue.isPositive(timeValue)) { + final long deadline = currentTimeMillis + timeValue.toMillis(); + return deadline >= 0 ? deadline : Long.MAX_VALUE; + } else { + return Long.MAX_VALUE; + } + } + private final long duration; private final TimeUnit timeUnit; @@ -224,7 +241,7 @@ public class TimeValue { @Override public int hashCode() { int hash = LangUtils.HASH_SEED; - hash = LangUtils.hashCode(hash, Long.valueOf(duration)); + hash = LangUtils.hashCode(hash, duration); hash = LangUtils.hashCode(hash, timeUnit); return hash; } @@ -279,7 +296,7 @@ public class TimeValue { @Override public String toString() { - return String.format("%,d %s", Long.valueOf(duration), timeUnit); + return String.format("%,d %s", duration, timeUnit); } public Timeout toTimeout() { http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java b/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java index 888a25b..6c1e00b 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/util/Timeout.java @@ -34,7 +34,7 @@ import org.apache.hc.core5.annotation.Contract; import org.apache.hc.core5.annotation.ThreadingBehavior; /** - * Represents a timeout value as a {@code long} time and {@link TimeUnit}. + * Represents a timeout value as a non-negative {@code long} time and {@link TimeUnit}. * * @since 5.0 */ @@ -46,17 +46,19 @@ public class Timeout extends TimeValue { /** * A disabled timeout represented as 0 {@code MILLISECONDS}. */ - public static final Timeout DISABLED = new Timeout(0, TimeUnit.MILLISECONDS); + public static final Timeout DISABLED = ZERO_MILLISECONDS; /** - * An undefined timeout represented as -1 {@code MILLISECONDS}. - */ - public static final Timeout UNDEFINED_MILLISECONDS = new Timeout(INT_UNDEFINED, TimeUnit.MILLISECONDS); - - /** - * An undefined timeout represented as -1 {@code SECONDS}. + * Returns the given {@code timeout} if it is not {@code null}, if {@code null} then returns + * {@link #DISABLED}. + * + * @param timeout + * may be {@code null} + * @return {@code timeValue} or {@link #DISABLED} */ - public static final Timeout UNDEFINED_SECONDS = new Timeout(INT_UNDEFINED, TimeUnit.SECONDS); + public static Timeout defaultsToDisabled(final Timeout timeout) { + return defaultsTo(timeout, DISABLED); + } /** * Creates a Timeout. @@ -161,15 +163,8 @@ public class Timeout extends TimeValue { return TimeValue.parse(value).toTimeout(); } - private static long validateDuration(final long duration) { - if (duration < INT_UNDEFINED) { - throw new IllegalArgumentException("Duration may not be less than " + INT_UNDEFINED); - } - return duration; - } - Timeout(final long duration, final TimeUnit timeUnit) { - super(validateDuration(duration), Args.notNull(timeUnit, "timeUnit")); + super(Args.notNegative(duration, "Duration"), Args.notNull(timeUnit, "timeUnit")); } /** @@ -187,25 +182,7 @@ public class Timeout extends TimeValue { * @return Whether this timeout is disabled. */ public boolean isEnabled() { - return !isDisabled() && !(isUndefinedMilliseconds() || isUndefinedSeconds()); - } - - /** - * Whether this timeout is undefined. - * - * @return Whether this timeout is undefined. - */ - public boolean isUndefinedMilliseconds() { - return getDuration() == INT_UNDEFINED && getTimeUnit() == TimeUnit.MILLISECONDS; - } - - /** - * Whether this timeout is undefined. - * - * @return Whether this timeout is undefined. - */ - public boolean isUndefinedSeconds() { - return getDuration() == INT_UNDEFINED && getTimeUnit() == TimeUnit.SECONDS; + return !isDisabled(); } } http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/test/java/org/apache/hc/core5/pool/TestStrictConnPool.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/test/java/org/apache/hc/core5/pool/TestStrictConnPool.java b/httpcore5/src/test/java/org/apache/hc/core5/pool/TestStrictConnPool.java index 4435479..732513e 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/pool/TestStrictConnPool.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/pool/TestStrictConnPool.java @@ -33,6 +33,7 @@ import java.util.concurrent.TimeUnit; import org.apache.hc.core5.http.HttpConnection; import org.apache.hc.core5.io.ShutdownType; import org.apache.hc.core5.util.TimeValue; +import org.apache.hc.core5.util.Timeout; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; @@ -108,7 +109,7 @@ public class TestStrictConnPool { public void testLeaseIllegal() throws Exception { final StrictConnPool pool = new StrictConnPool<>(2, 10); try { - pool.lease(null, null, TimeValue.ZERO_MILLISECONDS, null); + pool.lease(null, null, Timeout.ZERO_MILLISECONDS, null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException expected) { } @@ -491,9 +492,9 @@ public class TestStrictConnPool { final StrictConnPool pool = new StrictConnPool<>(1, 1); - final Future> future1 = pool.lease("somehost", null, TimeValue.of(0, TimeUnit.MILLISECONDS), null); - final Future> future2 = pool.lease("somehost", null, TimeValue.of(0, TimeUnit.MILLISECONDS), null); - final Future> future3 = pool.lease("somehost", null, TimeValue.of(10, TimeUnit.MILLISECONDS), null); + final Future> future1 = pool.lease("somehost", null, Timeout.ofMillis(0), null); + final Future> future2 = pool.lease("somehost", null, Timeout.ofMillis(0), null); + final Future> future3 = pool.lease("somehost", null, Timeout.ofMillis(10), null); Assert.assertTrue(future1.isDone()); final PoolEntry entry1 = future1.get(); @@ -514,14 +515,14 @@ public class TestStrictConnPool { public void testLeaseRequestCanceled() throws Exception { final StrictConnPool pool = new StrictConnPool<>(1, 1); - final Future> future1 = pool.lease("somehost", null, TimeValue.of(0, TimeUnit.MILLISECONDS), null); + final Future> future1 = pool.lease("somehost", null, Timeout.ofMillis(0), null); Assert.assertTrue(future1.isDone()); final PoolEntry entry1 = future1.get(); Assert.assertNotNull(entry1); entry1.assignConnection(Mockito.mock(HttpConnection.class)); - final Future> future2 = pool.lease("somehost", null, TimeValue.of(0, TimeUnit.MILLISECONDS), null); + final Future> future2 = pool.lease("somehost", null, Timeout.ofMillis(0), null); future2.cancel(true); pool.release(entry1, true); http://git-wip-us.apache.org/repos/asf/httpcomponents-core/blob/d733e0ee/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java ---------------------------------------------------------------------- diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java index 25d62a5..336ff7b 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/util/TestTimeout.java @@ -89,8 +89,6 @@ public class TestTimeout { public void testDisabled() { Assert.assertTrue(Timeout.DISABLED.isDisabled()); Assert.assertFalse(Timeout.DISABLED.isEnabled()); - Assert.assertFalse(Timeout.DISABLED.isUndefinedMilliseconds()); - Assert.assertFalse(Timeout.DISABLED.isUndefinedSeconds()); } private void testFactory(final TimeUnit timeUnit) { @@ -142,30 +140,11 @@ public class TestTimeout { test(Long.MAX_VALUE); } - @Test + @Test(expected = IllegalArgumentException.class) public void testNegative1() { test(-1); } - @Test(expected = IllegalArgumentException.class) - public void testNegative2() { - test(-2); - } - - @Test - public void testUndefinedMilliseconds() { - Assert.assertTrue(Timeout.UNDEFINED_MILLISECONDS.isUndefinedMilliseconds()); - Assert.assertFalse(Timeout.UNDEFINED_MILLISECONDS.isEnabled()); - Assert.assertFalse(Timeout.UNDEFINED_MILLISECONDS.isDisabled()); - } - - @Test - public void testUndefinedSeconds() { - Assert.assertTrue(Timeout.UNDEFINED_SECONDS.isUndefinedSeconds()); - Assert.assertFalse(Timeout.UNDEFINED_SECONDS.isEnabled()); - Assert.assertFalse(Timeout.UNDEFINED_SECONDS.isDisabled()); - } - @Test public void testToString() { Assert.assertEquals("9,223,372,036,854,775,807 SECONDS", Timeout.ofSeconds(Long.MAX_VALUE).toString());