From 805c8ee35bb9f6c5ef9bbfc54d99daf3210c98b9 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Fri, 10 Jun 2022 19:13:55 +1000 Subject: [PATCH] Use StaticException class in jetty-util for websocket flushers. Signed-off-by: Lachlan Roberts --- .../org/eclipse/jetty/util/StaticException.java | 16 +++++++++------- .../core/internal/DemandingFlusher.java | 4 ++-- .../websocket/core/internal/FrameFlusher.java | 4 ++-- .../core/internal/TransformingFlusher.java | 4 ++-- 4 files changed, 15 insertions(+), 13 deletions(-) rename jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/exception/SentinelWebSocketCloseException.java => jetty-util/src/main/java/org/eclipse/jetty/util/StaticException.java (61%) diff --git a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/exception/SentinelWebSocketCloseException.java b/jetty-util/src/main/java/org/eclipse/jetty/util/StaticException.java similarity index 61% rename from jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/exception/SentinelWebSocketCloseException.java rename to jetty-util/src/main/java/org/eclipse/jetty/util/StaticException.java index 7f9b683d0ba1..699442bc742c 100644 --- a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/exception/SentinelWebSocketCloseException.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/StaticException.java @@ -11,22 +11,24 @@ // ======================================================================== // -package org.eclipse.jetty.websocket.core.exception; +package org.eclipse.jetty.util; /** - * Suppressed exceptions are disabled for this implementation, + * This exception can safely be stored in a static variable as suppressed exceptions are disabled, * meaning calling {@link #addSuppressed(Throwable)} has no effect. - * This means instances of {@link SentinelWebSocketCloseException} are suitable to be kept as static fields. + * This prevents potential memory leaks where a statically-stored exception would accumulate + * suppressed exceptions added to them. */ -public class SentinelWebSocketCloseException extends Exception +public class StaticException extends Exception { - public SentinelWebSocketCloseException() + public StaticException() { this(null); } - public SentinelWebSocketCloseException(String message) + public StaticException(String message) { + // Make sure to call the super constructor that disables suppressed exception. super(message, null, false, true); } -} +} \ No newline at end of file diff --git a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/DemandingFlusher.java b/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/DemandingFlusher.java index 49b0eba16106..e14ec84451bd 100644 --- a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/DemandingFlusher.java +++ b/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/DemandingFlusher.java @@ -20,10 +20,10 @@ import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.CountingCallback; import org.eclipse.jetty.util.IteratingCallback; +import org.eclipse.jetty.util.StaticException; import org.eclipse.jetty.websocket.core.Extension; import org.eclipse.jetty.websocket.core.Frame; import org.eclipse.jetty.websocket.core.IncomingFrames; -import org.eclipse.jetty.websocket.core.exception.SentinelWebSocketCloseException; /** *

This flusher can be used to mutated and fragment {@link Frame}s and forwarded them on towards the application using the @@ -39,7 +39,7 @@ */ public abstract class DemandingFlusher extends IteratingCallback implements DemandChain { - private static final Throwable SENTINEL_CLOSE_EXCEPTION = new SentinelWebSocketCloseException(); + private static final Throwable SENTINEL_CLOSE_EXCEPTION = new StaticException("Closed"); private final IncomingFrames _emitFrame; private final AtomicLong _demand = new AtomicLong(); diff --git a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameFlusher.java b/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameFlusher.java index 1bd3fab65ae6..0f2199375c4e 100644 --- a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameFlusher.java +++ b/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/FrameFlusher.java @@ -29,13 +29,13 @@ import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.IteratingCallback; +import org.eclipse.jetty.util.StaticException; import org.eclipse.jetty.util.TypeUtil; import org.eclipse.jetty.util.thread.AutoLock; import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.websocket.core.CloseStatus; import org.eclipse.jetty.websocket.core.Frame; import org.eclipse.jetty.websocket.core.OpCode; -import org.eclipse.jetty.websocket.core.exception.SentinelWebSocketCloseException; import org.eclipse.jetty.websocket.core.exception.WebSocketException; import org.eclipse.jetty.websocket.core.exception.WebSocketWriteTimeoutException; import org.slf4j.Logger; @@ -45,7 +45,7 @@ public class FrameFlusher extends IteratingCallback { public static final Frame FLUSH_FRAME = new Frame(OpCode.BINARY); private static final Logger LOG = LoggerFactory.getLogger(FrameFlusher.class); - private static final Throwable CLOSED_CHANNEL = new SentinelWebSocketCloseException(); + private static final Throwable CLOSED_CHANNEL = new StaticException("Closed"); private final AutoLock lock = new AutoLock(); private final LongAdder messagesOut = new LongAdder(); diff --git a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/TransformingFlusher.java b/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/TransformingFlusher.java index 1ad545984a89..4792cd434703 100644 --- a/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/TransformingFlusher.java +++ b/jetty-websocket/websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/internal/TransformingFlusher.java @@ -18,9 +18,9 @@ import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.IteratingCallback; +import org.eclipse.jetty.util.StaticException; import org.eclipse.jetty.util.thread.AutoLock; import org.eclipse.jetty.websocket.core.Frame; -import org.eclipse.jetty.websocket.core.exception.SentinelWebSocketCloseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,7 +34,7 @@ public abstract class TransformingFlusher { private final Logger log = LoggerFactory.getLogger(this.getClass()); - private static final Throwable SENTINEL_CLOSE_EXCEPTION = new SentinelWebSocketCloseException(); + private static final Throwable SENTINEL_CLOSE_EXCEPTION = new StaticException("Closed"); private final AutoLock lock = new AutoLock(); private final Queue entries = new ArrayDeque<>();