Skip to content

Commit 92f614c

Browse files
committed
align with Vert.X 4.5.16 close behavior
1 parent 66aefab commit 92f614c

File tree

5 files changed

+38
-9
lines changed

5 files changed

+38
-9
lines changed

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/CloseReason.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,30 @@ public class CloseReason {
1515

1616
public static final CloseReason NORMAL = new CloseReason(WebSocketCloseStatus.NORMAL_CLOSURE.code());
1717

18+
public static final CloseReason ABNORMAL = new CloseReason(WebSocketCloseStatus.ABNORMAL_CLOSURE.code(), null, false);
19+
20+
public static final CloseReason EMPTY = new CloseReason(WebSocketCloseStatus.EMPTY.code(), null, false);
21+
1822
public static final CloseReason INTERNAL_SERVER_ERROR = new CloseReason(WebSocketCloseStatus.INTERNAL_SERVER_ERROR.code());
1923

2024
private final int code;
2125

2226
private final String message;
2327

28+
private CloseReason(int code, String message, boolean validate) {
29+
if (validate && !WebSocketCloseStatus.isValidStatusCode(code)) {
30+
throw new IllegalArgumentException("Invalid status code: " + code);
31+
}
32+
this.code = code;
33+
this.message = message;
34+
}
35+
2436
/**
2537
*
2638
* @param code The status code must comply with RFC-6455
2739
*/
2840
public CloseReason(int code) {
29-
this(code, null);
41+
this(code, null, true);
3042
}
3143

3244
/**
@@ -35,11 +47,7 @@ public CloseReason(int code) {
3547
* @param message
3648
*/
3749
public CloseReason(int code, String message) {
38-
if (!WebSocketCloseStatus.isValidStatusCode(code)) {
39-
throw new IllegalArgumentException("Invalid status code: " + code);
40-
}
41-
this.code = code;
42-
this.message = message;
50+
this(code, message, true);
4351
}
4452

4553
public int getCode() {

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/Endpoints.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.vertx.core.Handler;
2626
import io.vertx.core.Vertx;
2727
import io.vertx.core.buffer.Buffer;
28+
import io.vertx.core.http.HttpClosedException;
2829
import io.vertx.core.http.WebSocketBase;
2930
import io.vertx.core.http.WebSocketFrame;
3031
import io.vertx.core.http.WebSocketFrameType;
@@ -342,6 +343,9 @@ private static boolean isSecurityFailure(Throwable throwable) {
342343
}
343344

344345
static boolean isWebSocketIsClosedFailure(Throwable throwable, WebSocketConnectionBase connection) {
346+
if (throwable instanceof HttpClosedException) {
347+
return true;
348+
}
345349
if (!connection.isClosed()) {
346350
return false;
347351
}

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectionBase.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import org.jboss.logging.Logger;
1010

11+
import io.netty.handler.codec.http.websocketx.WebSocketCloseStatus;
1112
import io.quarkus.vertx.utils.NoBoundChecksBuffer;
1213
import io.quarkus.websockets.next.CloseReason;
1314
import io.quarkus.websockets.next.Connection;
@@ -168,9 +169,13 @@ public CloseReason closeReason() {
168169
WebSocketBase ws = webSocket();
169170
if (ws.isClosed()) {
170171
Short code = ws.closeStatusCode();
171-
if (code == null) {
172+
if (code == null || code == WebSocketCloseStatus.EMPTY.code()) {
172173
// This could happen if the connection is terminated abruptly
173-
return CloseReason.INTERNAL_SERVER_ERROR;
174+
return CloseReason.EMPTY;
175+
}
176+
if (code == WebSocketCloseStatus.ABNORMAL_CLOSURE.code()) {
177+
// This could happen if a close frame is never received
178+
return CloseReason.ABNORMAL;
174179
}
175180
return new CloseReason(code, ws.closeReason());
176181
}

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ protected WebSocketClientOptions populateClientOptions() {
193193
clientOptions.setIdleTimeout((int) timeout.toMillis());
194194
}
195195
}
196+
if (config.connectionClosingTimeout().isPresent()) {
197+
long timeoutSeconds = config.connectionClosingTimeout().get().toSeconds();
198+
clientOptions.setClosingTimeout(timeoutSeconds > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) timeoutSeconds);
199+
}
196200
Optional<TlsConfiguration> maybeTlsConfiguration = TlsConfiguration.from(tlsConfigurationRegistry,
197201
Optional.ofNullable(tlsConfigurationName));
198202
if (maybeTlsConfiguration.isEmpty()) {

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/config/WebSocketsClientRuntimeConfig.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public interface WebSocketsClientRuntimeConfig {
3636

3737
/**
3838
* The maximum size of a frame in bytes. The default values is
39-
* {@value io.vertx.core.http.HttpClientOptions#DEFAULT_MAX_WEBSOCKET_FRAME_SIZEX}.
39+
* {@value io.vertx.core.http.HttpClientOptions#DEFAULT_MAX_WEBSOCKET_FRAME_SIZE}.
4040
*/
4141
OptionalInt maxFrameSize();
4242

@@ -52,6 +52,14 @@ public interface WebSocketsClientRuntimeConfig {
5252
*/
5353
Optional<Duration> connectionIdleTimeout();
5454

55+
/**
56+
* The amount of time a client will wait until it closes the TCP connection after sending a close frame.
57+
* The default value is {@value io.vertx.core.http.HttpClientOptions#DEFAULT_WEBSOCKET_CLOSING_TIMEOUT}s
58+
*
59+
* @see io.vertx.core.http.WebSocketClientOptions#setClosingTimeout(int)
60+
*/
61+
Optional<Duration> connectionClosingTimeout();
62+
5563
/**
5664
* The strategy used when an error occurs but no error handler can handle the failure.
5765
* <p>

0 commit comments

Comments
 (0)