diff --git a/src/main/java/io/vertx/core/http/HttpConnection.java b/src/main/java/io/vertx/core/http/HttpConnection.java index c3fbf548b29..63643a6d691 100644 --- a/src/main/java/io/vertx/core/http/HttpConnection.java +++ b/src/main/java/io/vertx/core/http/HttpConnection.java @@ -23,6 +23,7 @@ import javax.security.cert.X509Certificate; import java.security.cert.Certificate; import java.util.List; +import java.util.concurrent.TimeUnit; /** * Represents an HTTP connection. @@ -119,38 +120,64 @@ default HttpConnection goAway(long errorCode, int lastStreamId) { HttpConnection shutdownHandler(@Nullable Handler handler); /** - * Initiate a graceful connection shutdown, the connection is taken out of service and closed when all current requests - * are processed, otherwise after 30 seconds the connection will be closed. Client connection are immediately removed - * from the pool. - * - * - * - * @param handler the handler called when shutdown has completed + * Shutdown a 30 seconds timeout ({@code shutdown(30, TimeUnit.SECONDS)}). */ default void shutdown(Handler> handler) { - shutdown(30000, handler); + shutdown(30, TimeUnit.SECONDS, handler); } /** * Like {@link #shutdown(Handler)} but returns a {@code Future} of the asynchronous result */ default Future shutdown() { - return shutdown(30000L); + return shutdown(30, TimeUnit.SECONDS); } /** - * Like {@link #shutdown(Handler)} but with a specific {@code timeout} in milliseconds. + * Like {@link #shutdown(long, TimeUnit, Handler)}, in milliseconds. + * + * @deprecated instead use {@link #shutdown(long, TimeUnit, Handler)} */ - void shutdown(long timeout, Handler> handler); + @Deprecated + default void shutdown(long timeout, Handler> handler) { + shutdown(timeout, TimeUnit.MILLISECONDS, handler); + } /** * Like {@link #shutdown(long, Handler)} but returns a {@code Future} of the asynchronous result + * + * @deprecated instead use {@link #shutdown(long, TimeUnit)} + */ + @Deprecated + default Future shutdown(long timeoutMs) { + return shutdown(timeoutMs, TimeUnit.MILLISECONDS); + } + + /** + * Initiate a graceful connection shutdown, the connection is taken out of service and closed when all the inflight requests + * are processed, otherwise after a {@code timeout} the connection will be closed. Client connection are immediately removed + * from the pool. + * + *
    + *
  • HTTP/2 connections will send a go away frame immediately to signal the other side the connection will close.
  • + *
  • HTTP/1.x connection will be closed.
  • + *
+ * + * @param timeout the amount of time after which all resources are forcibly closed + * @param unit the of the timeout + * @param handler the handler notified with the result + */ + default void shutdown(long timeout, TimeUnit unit, Handler> handler) { + Future fut = shutdown(timeout, unit); + if (handler != null) { + fut.onComplete(handler); + } + } + + /** + * Like {@link #shutdown(long, TimeUnit, Handler)} but returns a {@code Future} of the asynchronous result */ - Future shutdown(long timeoutMs); + Future shutdown(long timeout, TimeUnit unit); /** * Set a close handler. The handler will get notified when the connection is closed. diff --git a/src/main/java/io/vertx/core/http/impl/Http1xClientConnection.java b/src/main/java/io/vertx/core/http/impl/Http1xClientConnection.java index 5421b0459eb..021ab548b26 100644 --- a/src/main/java/io/vertx/core/http/impl/Http1xClientConnection.java +++ b/src/main/java/io/vertx/core/http/impl/Http1xClientConnection.java @@ -56,6 +56,7 @@ import java.net.URI; import java.util.*; +import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import static io.netty.handler.codec.http.websocketx.WebSocketVersion.*; @@ -1284,23 +1285,18 @@ public boolean isValid() { return expirationTimestamp == 0 || System.currentTimeMillis() <= expirationTimestamp; } - @Override - public void shutdown(long timeout, Handler> handler) { - shutdown(timeout, vertx.promise(handler)); + private synchronized void shutdownNow() { + shutdownTimerID = -1L; + close(); } @Override - public Future shutdown(long timeoutMs) { + public Future shutdown(long timeout, TimeUnit unit) { PromiseInternal promise = vertx.promise(); - shutdown(timeoutMs, promise); + shutdown(unit.toMillis(timeout), promise); return promise.future(); } - private synchronized void shutdownNow() { - shutdownTimerID = -1L; - close(); - } - private void shutdown(long timeoutMs, PromiseInternal promise) { synchronized (this) { if (shutdown) { diff --git a/src/main/java/io/vertx/core/http/impl/Http1xConnectionBase.java b/src/main/java/io/vertx/core/http/impl/Http1xConnectionBase.java index de41192dc72..53aed89ec09 100644 --- a/src/main/java/io/vertx/core/http/impl/Http1xConnectionBase.java +++ b/src/main/java/io/vertx/core/http/impl/Http1xConnectionBase.java @@ -41,6 +41,8 @@ import io.vertx.core.impl.ContextInternal; import io.vertx.core.net.impl.ConnectionBase; +import java.util.concurrent.TimeUnit; + import static io.vertx.core.net.impl.VertxHandler.safeBuffer; /** @@ -132,12 +134,7 @@ public HttpConnection shutdownHandler(@Nullable Handler handler) { } @Override - public void shutdown(long timeout, Handler> handler) { - throw new UnsupportedOperationException("HTTP/1.x connections cannot be shutdown"); - } - - @Override - public Future shutdown(long timeoutMs) { + public Future shutdown(long timeout, TimeUnit unit) { throw new UnsupportedOperationException("HTTP/1.x connections cannot be shutdown"); } diff --git a/src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java b/src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java index 7a31c834c33..e72fa16ede5 100644 --- a/src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java +++ b/src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java @@ -47,6 +47,7 @@ import java.util.ArrayList; import java.util.Map; import java.util.Objects; +import java.util.concurrent.TimeUnit; /** * @author Julien Viet @@ -363,14 +364,9 @@ public synchronized HttpConnection shutdownHandler(Handler handler) { } @Override - public void shutdown(long timeout, Handler> handler) { - shutdown(timeout, vertx.promise(handler)); - } - - @Override - public Future shutdown(long timeoutMs) { + public Future shutdown(long timeout, TimeUnit unit) { PromiseInternal promise = vertx.promise(); - shutdown(timeoutMs, promise); + shutdown(unit.toMillis(timeout), promise); return promise.future(); } diff --git a/src/main/java/io/vertx/core/http/impl/Http2UpgradeClientConnection.java b/src/main/java/io/vertx/core/http/impl/Http2UpgradeClientConnection.java index e61ed9158e1..f8ca98e5776 100644 --- a/src/main/java/io/vertx/core/http/impl/Http2UpgradeClientConnection.java +++ b/src/main/java/io/vertx/core/http/impl/Http2UpgradeClientConnection.java @@ -38,6 +38,7 @@ import java.util.ArrayDeque; import java.util.Deque; import java.util.List; +import java.util.concurrent.TimeUnit; /** * A connection that attempts to perform a protocol upgrade to H2C. The connection might use HTTP/1 or H2C @@ -885,13 +886,8 @@ public HttpConnection goAway(long errorCode, int lastStreamId, Buffer debugData) } @Override - public void shutdown(long timeout, Handler> handler) { - current.shutdown(timeout, handler); - } - - @Override - public Future shutdown(long timeoutMs) { - return current.shutdown(timeoutMs); + public Future shutdown(long timeout, TimeUnit unit) { + return current.shutdown(timeout, unit); } @Override