From 9e383f0891fdf838636c0193f954bdb54539d602 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 28 Jul 2020 16:48:49 +1000 Subject: [PATCH] Issue #4919 - changes from review Signed-off-by: Lachlan Roberts --- .../websocket/javax/common/JavaxWebSocketContainer.java | 2 +- .../jetty/websocket/javax/common/SessionTracker.java | 3 +++ .../eclipse/jetty/websocket/client/WebSocketClient.java | 8 ++++++-- .../eclipse/jetty/websocket/common/SessionTracker.java | 3 +++ .../websocket/server/JettyWebSocketServerContainer.java | 2 +- .../org/eclipse/jetty/websocket/util/ShutdownUtil.java | 4 ++-- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketContainer.java b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketContainer.java index 5c4f5380afb..527f06cbe04 100644 --- a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketContainer.java +++ b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/JavaxWebSocketContainer.java @@ -211,7 +211,7 @@ public abstract class JavaxWebSocketContainer extends ContainerLifeCycle impleme @Override public CompletableFuture shutdown() { - return ShutdownUtil.shutdown(sessionTracker); + return ShutdownUtil.stop(sessionTracker); } @Override diff --git a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/SessionTracker.java b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/SessionTracker.java index 600540259a3..0663c08b84f 100644 --- a/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/SessionTracker.java +++ b/jetty-websocket/websocket-javax-common/src/main/java/org/eclipse/jetty/websocket/javax/common/SessionTracker.java @@ -57,6 +57,9 @@ public class SessionTracker extends AbstractLifeCycle implements JavaxWebSocketS { for (Session session : sessions) { + if (Thread.interrupted()) + break; + try { // GOING_AWAY is abnormal close status so it will hard close connection after sent. diff --git a/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java b/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java index 48ccb97ab00..e1bdedc37bb 100644 --- a/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java +++ b/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketClient.java @@ -71,7 +71,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketPoli private final Configuration.ConfigurationCustomizer configurationCustomizer = new Configuration.ConfigurationCustomizer(); private final WebSocketComponents components = new WebSocketComponents(); private boolean stopAtShutdown = false; - private long _stopTimeout = 200; + private long _stopTimeout = Long.MAX_VALUE; /** * Instantiate a WebSocketClient with defaults @@ -391,6 +391,10 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketPoli stopAtShutdown = stop; } + /** + * The timeout to allow all remaining open Sessions to be closed gracefully using the close code {@link org.eclipse.jetty.websocket.api.StatusCode#SHUTDOWN}. + * @param stopTimeout the time in ms to wait for the graceful close, use a value less than or equal to 0 to not gracefully close. + */ public void setStopTimeout(long stopTimeout) { _stopTimeout = stopTimeout; @@ -424,7 +428,7 @@ public class WebSocketClient extends ContainerLifeCycle implements WebSocketPoli @Override public CompletableFuture shutdown() { - return ShutdownUtil.shutdown(sessionTracker); + return ShutdownUtil.stop(sessionTracker); } @Override diff --git a/jetty-websocket/websocket-jetty-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java b/jetty-websocket/websocket-jetty-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java index 9c504802e99..a2cb83a8a01 100644 --- a/jetty-websocket/websocket-jetty-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java +++ b/jetty-websocket/websocket-jetty-common/src/main/java/org/eclipse/jetty/websocket/common/SessionTracker.java @@ -53,6 +53,9 @@ public class SessionTracker extends AbstractLifeCycle implements WebSocketSessio { for (Session session : sessions) { + if (Thread.interrupted()) + break; + // SHUTDOWN is abnormal close status so it will hard close connection after sent. session.close(StatusCode.SHUTDOWN, "Container being shut down"); } diff --git a/jetty-websocket/websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/JettyWebSocketServerContainer.java b/jetty-websocket/websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/JettyWebSocketServerContainer.java index 7fa99c2465c..708d02904ff 100644 --- a/jetty-websocket/websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-websocket/websocket-jetty-server/src/main/java/org/eclipse/jetty/websocket/server/JettyWebSocketServerContainer.java @@ -293,7 +293,7 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements @Override public CompletableFuture shutdown() { - return ShutdownUtil.shutdown(sessionTracker); + return ShutdownUtil.stop(sessionTracker); } @Override diff --git a/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/ShutdownUtil.java b/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/ShutdownUtil.java index 739fdac249e..cded9dce6aa 100644 --- a/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/ShutdownUtil.java +++ b/jetty-websocket/websocket-util/src/main/java/org/eclipse/jetty/websocket/util/ShutdownUtil.java @@ -30,11 +30,11 @@ public class ShutdownUtil private static final Logger LOG = LoggerFactory.getLogger(ShutdownUtil.class); /** - * Shutdown a {@link LifeCycle} in a new daemon thread and be notified on the result in a {@link CompletableFuture}. + * Stop a {@link LifeCycle} in a new daemon thread and be notified of the result in a {@link CompletableFuture}. * @param lifeCycle the LifeCycle to stop. * @return the CompletableFuture to be notified when the stop either completes or fails. */ - public static CompletableFuture shutdown(LifeCycle lifeCycle) + public static CompletableFuture stop(LifeCycle lifeCycle) { AtomicReference stopThreadReference = new AtomicReference<>(); CompletableFuture shutdown = new CompletableFuture<>()