Skip to content

Commit

Permalink
Add to HttpConnection shutdown variants with a time unit for the time…
Browse files Browse the repository at this point in the history
…out, deprecate methods with only the timeout in milliseconds.
  • Loading branch information
vietj committed Feb 12, 2024
1 parent 1f9df5a commit 939ea22
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
59 changes: 43 additions & 16 deletions src/main/java/io/vertx/core/http/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -119,38 +120,64 @@ default HttpConnection goAway(long errorCode, int lastStreamId) {
HttpConnection shutdownHandler(@Nullable Handler<Void> 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.
*
* <ul>
* <li>HTTP/2 connections will send a go away frame immediately to signal the other side the connection will close</li>
* <li>HTTP/1.x client connection supports this feature</li>
* <li>HTTP/1.x server connections do not support this feature</li>
* </ul>
*
* @param handler the handler called when shutdown has completed
* Shutdown a 30 seconds timeout ({@code shutdown(30, TimeUnit.SECONDS)}).
*/
default void shutdown(Handler<AsyncResult<Void>> handler) {
shutdown(30000, handler);
shutdown(30, TimeUnit.SECONDS, handler);
}

/**
* Like {@link #shutdown(Handler)} but returns a {@code Future} of the asynchronous result
*/
default Future<Void> 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<AsyncResult<Void>> handler);
@Deprecated
default void shutdown(long timeout, Handler<AsyncResult<Void>> 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<Void> 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.
*
* <ul>
* <li>HTTP/2 connections will send a go away frame immediately to signal the other side the connection will close.</li>
* <li>HTTP/1.x connection will be closed.</li>
* </ul>
*
* @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<AsyncResult<Void>> handler) {
Future<Void> 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<Void> shutdown(long timeoutMs);
Future<Void> shutdown(long timeout, TimeUnit unit);

/**
* Set a close handler. The handler will get notified when the connection is closed.
Expand Down
16 changes: 6 additions & 10 deletions src/main/java/io/vertx/core/http/impl/Http1xClientConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -1284,23 +1285,18 @@ public boolean isValid() {
return expirationTimestamp == 0 || System.currentTimeMillis() <= expirationTimestamp;
}

@Override
public void shutdown(long timeout, Handler<AsyncResult<Void>> handler) {
shutdown(timeout, vertx.promise(handler));
private synchronized void shutdownNow() {
shutdownTimerID = -1L;
close();
}

@Override
public Future<Void> shutdown(long timeoutMs) {
public Future<Void> shutdown(long timeout, TimeUnit unit) {
PromiseInternal<Void> 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<Void> promise) {
synchronized (this) {
if (shutdown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -132,12 +134,7 @@ public HttpConnection shutdownHandler(@Nullable Handler<Void> handler) {
}

@Override
public void shutdown(long timeout, Handler<AsyncResult<Void>> handler) {
throw new UnsupportedOperationException("HTTP/1.x connections cannot be shutdown");
}

@Override
public Future<Void> shutdown(long timeoutMs) {
public Future<Void> shutdown(long timeout, TimeUnit unit) {
throw new UnsupportedOperationException("HTTP/1.x connections cannot be shutdown");
}

Expand Down
10 changes: 3 additions & 7 deletions src/main/java/io/vertx/core/http/impl/Http2ConnectionBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
Expand Down Expand Up @@ -363,14 +364,9 @@ public synchronized HttpConnection shutdownHandler(Handler<Void> handler) {
}

@Override
public void shutdown(long timeout, Handler<AsyncResult<Void>> handler) {
shutdown(timeout, vertx.promise(handler));
}

@Override
public Future<Void> shutdown(long timeoutMs) {
public Future<Void> shutdown(long timeout, TimeUnit unit) {
PromiseInternal<Void> promise = vertx.promise();
shutdown(timeoutMs, promise);
shutdown(unit.toMillis(timeout), promise);
return promise.future();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -885,13 +886,8 @@ public HttpConnection goAway(long errorCode, int lastStreamId, Buffer debugData)
}

@Override
public void shutdown(long timeout, Handler<AsyncResult<Void>> handler) {
current.shutdown(timeout, handler);
}

@Override
public Future<Void> shutdown(long timeoutMs) {
return current.shutdown(timeoutMs);
public Future<Void> shutdown(long timeout, TimeUnit unit) {
return current.shutdown(timeout, unit);
}

@Override
Expand Down

0 comments on commit 939ea22

Please sign in to comment.