Use compile time constants instead of status field in WebSocketServer/ClientProtocolConfig (#9976)
Motivation: Avoid allocation of default static `WebSocketServerProtocolConfig` and `WebSocketClientProtocolConfig` configs. Prefer compile time constants instead. Modification: Static field with config object replaced with constructor with default fields. Result: No more default config allocation and static field for it. Compile time variables used instead.
This commit is contained in:
parent
e59c86cf7b
commit
6b6782ea01
@ -22,6 +22,7 @@ import io.netty.handler.codec.http.websocketx.WebSocketClientProtocolHandler.Cli
|
||||
import java.net.URI;
|
||||
import java.util.Objects;
|
||||
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig.DEFAULT_HANDSHAKE_TIMEOUT_MILLIS;
|
||||
import static io.netty.util.internal.ObjectUtil.checkPositive;
|
||||
|
||||
/**
|
||||
@ -29,9 +30,10 @@ import static io.netty.util.internal.ObjectUtil.checkPositive;
|
||||
*/
|
||||
public final class WebSocketClientProtocolConfig {
|
||||
|
||||
static final WebSocketClientProtocolConfig DEFAULT = new WebSocketClientProtocolConfig(
|
||||
URI.create("https://localhost/"), null, WebSocketVersion.V13, false, EmptyHttpHeaders.INSTANCE,
|
||||
65536, true, false, true, WebSocketCloseStatus.NORMAL_CLOSURE, true, 10000L, -1, false);
|
||||
static final boolean DEFAULT_PERFORM_MASKING = true;
|
||||
static final boolean DEFAULT_ALLOW_MASK_MISMATCH = false;
|
||||
static final boolean DEFAULT_HANDLE_CLOSE_FRAMES = true;
|
||||
static final boolean DEFAULT_DROP_PONG_FRAMES = true;
|
||||
|
||||
private final URI webSocketUri;
|
||||
private final String subprotocol;
|
||||
@ -161,7 +163,21 @@ public final class WebSocketClientProtocolConfig {
|
||||
}
|
||||
|
||||
public static Builder newBuilder() {
|
||||
return new Builder(DEFAULT);
|
||||
return new Builder(
|
||||
URI.create("https://localhost/"),
|
||||
null,
|
||||
WebSocketVersion.V13,
|
||||
false,
|
||||
EmptyHttpHeaders.INSTANCE,
|
||||
65536,
|
||||
DEFAULT_PERFORM_MASKING,
|
||||
DEFAULT_ALLOW_MASK_MISMATCH,
|
||||
DEFAULT_HANDLE_CLOSE_FRAMES,
|
||||
WebSocketCloseStatus.NORMAL_CLOSURE,
|
||||
DEFAULT_DROP_PONG_FRAMES,
|
||||
DEFAULT_HANDSHAKE_TIMEOUT_MILLIS,
|
||||
-1,
|
||||
false);
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
@ -181,22 +197,50 @@ public final class WebSocketClientProtocolConfig {
|
||||
private boolean absoluteUpgradeUrl;
|
||||
|
||||
private Builder(WebSocketClientProtocolConfig clientConfig) {
|
||||
Objects.requireNonNull(clientConfig, "clientConfig");
|
||||
this(Objects.requireNonNull(clientConfig, "clientConfig").webSocketUri(),
|
||||
clientConfig.subprotocol(),
|
||||
clientConfig.version(),
|
||||
clientConfig.allowExtensions(),
|
||||
clientConfig.customHeaders(),
|
||||
clientConfig.maxFramePayloadLength(),
|
||||
clientConfig.performMasking(),
|
||||
clientConfig.allowMaskMismatch(),
|
||||
clientConfig.handleCloseFrames(),
|
||||
clientConfig.sendCloseFrame(),
|
||||
clientConfig.dropPongFrames(),
|
||||
clientConfig.handshakeTimeoutMillis(),
|
||||
clientConfig.forceCloseTimeoutMillis(),
|
||||
clientConfig.absoluteUpgradeUrl());
|
||||
}
|
||||
|
||||
webSocketUri = clientConfig.webSocketUri();
|
||||
subprotocol = clientConfig.subprotocol();
|
||||
version = clientConfig.version();
|
||||
allowExtensions = clientConfig.allowExtensions();
|
||||
customHeaders = clientConfig.customHeaders();
|
||||
maxFramePayloadLength = clientConfig.maxFramePayloadLength();
|
||||
performMasking = clientConfig.performMasking();
|
||||
allowMaskMismatch = clientConfig.allowMaskMismatch();
|
||||
handleCloseFrames = clientConfig.handleCloseFrames();
|
||||
sendCloseFrame = clientConfig.sendCloseFrame();
|
||||
dropPongFrames = clientConfig.dropPongFrames();
|
||||
handshakeTimeoutMillis = clientConfig.handshakeTimeoutMillis();
|
||||
forceCloseTimeoutMillis = clientConfig.forceCloseTimeoutMillis();
|
||||
absoluteUpgradeUrl = clientConfig.absoluteUpgradeUrl();
|
||||
private Builder(URI webSocketUri,
|
||||
String subprotocol,
|
||||
WebSocketVersion version,
|
||||
boolean allowExtensions,
|
||||
HttpHeaders customHeaders,
|
||||
int maxFramePayloadLength,
|
||||
boolean performMasking,
|
||||
boolean allowMaskMismatch,
|
||||
boolean handleCloseFrames,
|
||||
WebSocketCloseStatus sendCloseFrame,
|
||||
boolean dropPongFrames,
|
||||
long handshakeTimeoutMillis,
|
||||
long forceCloseTimeoutMillis,
|
||||
boolean absoluteUpgradeUrl) {
|
||||
this.webSocketUri = webSocketUri;
|
||||
this.subprotocol = subprotocol;
|
||||
this.version = version;
|
||||
this.allowExtensions = allowExtensions;
|
||||
this.customHeaders = customHeaders;
|
||||
this.maxFramePayloadLength = maxFramePayloadLength;
|
||||
this.performMasking = performMasking;
|
||||
this.allowMaskMismatch = allowMaskMismatch;
|
||||
this.handleCloseFrames = handleCloseFrames;
|
||||
this.sendCloseFrame = sendCloseFrame;
|
||||
this.dropPongFrames = dropPongFrames;
|
||||
this.handshakeTimeoutMillis = handshakeTimeoutMillis;
|
||||
this.forceCloseTimeoutMillis = forceCloseTimeoutMillis;
|
||||
this.absoluteUpgradeUrl = absoluteUpgradeUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -23,7 +23,11 @@ import io.netty.handler.codec.http.HttpHeaders;
|
||||
import java.net.URI;
|
||||
import java.util.Objects;
|
||||
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig.DEFAULT;
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig.DEFAULT_ALLOW_MASK_MISMATCH;
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig.DEFAULT_DROP_PONG_FRAMES;
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig.DEFAULT_HANDLE_CLOSE_FRAMES;
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketClientProtocolConfig.DEFAULT_PERFORM_MASKING;
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig.DEFAULT_HANDSHAKE_TIMEOUT_MILLIS;
|
||||
|
||||
/**
|
||||
* This handler does all the heavy lifting for you to run a websocket client.
|
||||
@ -122,7 +126,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
int maxFramePayloadLength, boolean handleCloseFrames,
|
||||
boolean performMasking, boolean allowMaskMismatch) {
|
||||
this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength,
|
||||
handleCloseFrames, performMasking, allowMaskMismatch, DEFAULT.handshakeTimeoutMillis());
|
||||
handleCloseFrames, performMasking, allowMaskMismatch, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -183,7 +187,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
boolean allowExtensions, HttpHeaders customHeaders,
|
||||
int maxFramePayloadLength, boolean handleCloseFrames) {
|
||||
this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength,
|
||||
handleCloseFrames, DEFAULT.handshakeTimeoutMillis());
|
||||
handleCloseFrames, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -210,7 +214,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
|
||||
boolean handleCloseFrames, long handshakeTimeoutMillis) {
|
||||
this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength,
|
||||
handleCloseFrames, DEFAULT.performMasking(), DEFAULT.allowMaskMismatch(), handshakeTimeoutMillis);
|
||||
handleCloseFrames, DEFAULT_PERFORM_MASKING, DEFAULT_ALLOW_MASK_MISMATCH, handshakeTimeoutMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -232,7 +236,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
boolean allowExtensions, HttpHeaders customHeaders,
|
||||
int maxFramePayloadLength) {
|
||||
this(webSocketURL, version, subprotocol, allowExtensions,
|
||||
customHeaders, maxFramePayloadLength, DEFAULT.handshakeTimeoutMillis());
|
||||
customHeaders, maxFramePayloadLength, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -257,7 +261,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
boolean allowExtensions, HttpHeaders customHeaders,
|
||||
int maxFramePayloadLength, long handshakeTimeoutMillis) {
|
||||
this(webSocketURL, version, subprotocol, allowExtensions, customHeaders,
|
||||
maxFramePayloadLength, DEFAULT.handleCloseFrames(), handshakeTimeoutMillis);
|
||||
maxFramePayloadLength, DEFAULT_HANDLE_CLOSE_FRAMES, handshakeTimeoutMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -270,7 +274,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
* {@code true} if close frames should not be forwarded and just close the channel
|
||||
*/
|
||||
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames) {
|
||||
this(handshaker, handleCloseFrames, DEFAULT.handshakeTimeoutMillis());
|
||||
this(handshaker, handleCloseFrames, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -287,7 +291,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
*/
|
||||
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames,
|
||||
long handshakeTimeoutMillis) {
|
||||
this(handshaker, handleCloseFrames, DEFAULT.dropPongFrames(), handshakeTimeoutMillis);
|
||||
this(handshaker, handleCloseFrames, DEFAULT_DROP_PONG_FRAMES, handshakeTimeoutMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -303,7 +307,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
*/
|
||||
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames,
|
||||
boolean dropPongFrames) {
|
||||
this(handshaker, handleCloseFrames, dropPongFrames, DEFAULT.handshakeTimeoutMillis());
|
||||
this(handshaker, handleCloseFrames, dropPongFrames, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -338,7 +342,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
* was established to the remote peer.
|
||||
*/
|
||||
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker) {
|
||||
this(handshaker, DEFAULT.handshakeTimeoutMillis());
|
||||
this(handshaker, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -352,7 +356,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
|
||||
* event {@link ClientHandshakeStateEvent#HANDSHAKE_TIMEOUT}
|
||||
*/
|
||||
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, long handshakeTimeoutMillis) {
|
||||
this(handshaker, DEFAULT.handleCloseFrames(), handshakeTimeoutMillis);
|
||||
this(handshaker, DEFAULT_HANDLE_CLOSE_FRAMES, handshakeTimeoutMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -26,8 +26,7 @@ import static io.netty.util.internal.ObjectUtil.checkPositive;
|
||||
*/
|
||||
public final class WebSocketServerProtocolConfig {
|
||||
|
||||
static final WebSocketServerProtocolConfig DEFAULT = new WebSocketServerProtocolConfig(
|
||||
"/", null, false, 10000L, 0, true, WebSocketCloseStatus.NORMAL_CLOSURE, true, WebSocketDecoderConfig.DEFAULT);
|
||||
static final long DEFAULT_HANDSHAKE_TIMEOUT_MILLIS = 10000L;
|
||||
|
||||
private final String websocketPath;
|
||||
private final String subprotocols;
|
||||
@ -117,7 +116,8 @@ public final class WebSocketServerProtocolConfig {
|
||||
}
|
||||
|
||||
public static Builder newBuilder() {
|
||||
return new Builder(DEFAULT);
|
||||
return new Builder("/", null, false, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS, 0L,
|
||||
true, WebSocketCloseStatus.NORMAL_CLOSURE, true, WebSocketDecoderConfig.DEFAULT);
|
||||
}
|
||||
|
||||
public static final class Builder {
|
||||
@ -133,16 +133,36 @@ public final class WebSocketServerProtocolConfig {
|
||||
private WebSocketDecoderConfig.Builder decoderConfigBuilder;
|
||||
|
||||
private Builder(WebSocketServerProtocolConfig serverConfig) {
|
||||
Objects.requireNonNull(serverConfig, "serverConfig");
|
||||
websocketPath = serverConfig.websocketPath();
|
||||
subprotocols = serverConfig.subprotocols();
|
||||
checkStartsWith = serverConfig.checkStartsWith();
|
||||
handshakeTimeoutMillis = serverConfig.handshakeTimeoutMillis();
|
||||
forceCloseTimeoutMillis = serverConfig.forceCloseTimeoutMillis();
|
||||
handleCloseFrames = serverConfig.handleCloseFrames();
|
||||
sendCloseFrame = serverConfig.sendCloseFrame();
|
||||
dropPongFrames = serverConfig.dropPongFrames();
|
||||
decoderConfig = serverConfig.decoderConfig();
|
||||
this(Objects.requireNonNull(serverConfig, "serverConfig").websocketPath(),
|
||||
serverConfig.subprotocols(),
|
||||
serverConfig.checkStartsWith(),
|
||||
serverConfig.handshakeTimeoutMillis(),
|
||||
serverConfig.forceCloseTimeoutMillis(),
|
||||
serverConfig.handleCloseFrames(),
|
||||
serverConfig.sendCloseFrame(),
|
||||
serverConfig.dropPongFrames(),
|
||||
serverConfig.decoderConfig()
|
||||
);
|
||||
}
|
||||
|
||||
private Builder(String websocketPath,
|
||||
String subprotocols,
|
||||
boolean checkStartsWith,
|
||||
long handshakeTimeoutMillis,
|
||||
long forceCloseTimeoutMillis,
|
||||
boolean handleCloseFrames,
|
||||
WebSocketCloseStatus sendCloseFrame,
|
||||
boolean dropPongFrames,
|
||||
WebSocketDecoderConfig decoderConfig) {
|
||||
this.websocketPath = websocketPath;
|
||||
this.subprotocols = subprotocols;
|
||||
this.checkStartsWith = checkStartsWith;
|
||||
this.handshakeTimeoutMillis = handshakeTimeoutMillis;
|
||||
this.forceCloseTimeoutMillis = forceCloseTimeoutMillis;
|
||||
this.handleCloseFrames = handleCloseFrames;
|
||||
this.sendCloseFrame = sendCloseFrame;
|
||||
this.dropPongFrames = dropPongFrames;
|
||||
this.decoderConfig = decoderConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -29,8 +29,8 @@ import io.netty.util.AttributeKey;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import static io.netty.handler.codec.http.HttpVersion.*;
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig.DEFAULT;
|
||||
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
|
||||
import static io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig.DEFAULT_HANDSHAKE_TIMEOUT_MILLIS;
|
||||
|
||||
/**
|
||||
* This handler does all the heavy lifting for you to run a websocket server.
|
||||
@ -114,7 +114,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath) {
|
||||
this(websocketPath, DEFAULT.handshakeTimeoutMillis());
|
||||
this(websocketPath, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, long handshakeTimeoutMillis) {
|
||||
@ -122,7 +122,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith) {
|
||||
this(websocketPath, checkStartsWith, DEFAULT.handshakeTimeoutMillis());
|
||||
this(websocketPath, checkStartsWith, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis) {
|
||||
@ -130,7 +130,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols) {
|
||||
this(websocketPath, subprotocols, DEFAULT.handshakeTimeoutMillis());
|
||||
this(websocketPath, subprotocols, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, long handshakeTimeoutMillis) {
|
||||
@ -138,7 +138,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions) {
|
||||
this(websocketPath, subprotocols, allowExtensions, DEFAULT.handshakeTimeoutMillis());
|
||||
this(websocketPath, subprotocols, allowExtensions, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions,
|
||||
@ -148,7 +148,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
|
||||
boolean allowExtensions, int maxFrameSize) {
|
||||
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, DEFAULT.handshakeTimeoutMillis());
|
||||
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
|
||||
@ -159,7 +159,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
|
||||
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) {
|
||||
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch,
|
||||
DEFAULT.handshakeTimeoutMillis());
|
||||
DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions,
|
||||
@ -171,7 +171,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
|
||||
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith) {
|
||||
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, checkStartsWith,
|
||||
DEFAULT.handshakeTimeoutMillis());
|
||||
DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
|
||||
@ -185,7 +185,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
|
||||
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch,
|
||||
boolean checkStartsWith, boolean dropPongFrames) {
|
||||
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, checkStartsWith,
|
||||
dropPongFrames, DEFAULT.handshakeTimeoutMillis());
|
||||
dropPongFrames, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
|
||||
}
|
||||
|
||||
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions,
|
||||
|
Loading…
Reference in New Issue
Block a user