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:
Dmitriy Dumanskiy 2020-01-29 16:01:23 +02:00 committed by GitHub
parent eb50e5148a
commit b49bd5644f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 54 deletions

View File

@ -22,6 +22,7 @@ import io.netty.util.internal.ObjectUtil;
import java.net.URI; import java.net.URI;
import static io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig.DEFAULT_HANDSHAKE_TIMEOUT_MILLIS;
import static io.netty.util.internal.ObjectUtil.checkPositive; import static io.netty.util.internal.ObjectUtil.checkPositive;
/** /**
@ -29,9 +30,10 @@ import static io.netty.util.internal.ObjectUtil.checkPositive;
*/ */
public final class WebSocketClientProtocolConfig { public final class WebSocketClientProtocolConfig {
static final WebSocketClientProtocolConfig DEFAULT = new WebSocketClientProtocolConfig( static final boolean DEFAULT_PERFORM_MASKING = true;
URI.create("https://localhost/"), null, WebSocketVersion.V13, false, EmptyHttpHeaders.INSTANCE, static final boolean DEFAULT_ALLOW_MASK_MISMATCH = false;
65536, true, false, true, WebSocketCloseStatus.NORMAL_CLOSURE, true, 10000L, -1, false); static final boolean DEFAULT_HANDLE_CLOSE_FRAMES = true;
static final boolean DEFAULT_DROP_PONG_FRAMES = true;
private final URI webSocketUri; private final URI webSocketUri;
private final String subprotocol; private final String subprotocol;
@ -161,7 +163,21 @@ public final class WebSocketClientProtocolConfig {
} }
public static Builder newBuilder() { 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 { public static final class Builder {
@ -181,22 +197,50 @@ public final class WebSocketClientProtocolConfig {
private boolean absoluteUpgradeUrl; private boolean absoluteUpgradeUrl;
private Builder(WebSocketClientProtocolConfig clientConfig) { private Builder(WebSocketClientProtocolConfig clientConfig) {
ObjectUtil.checkNotNull(clientConfig, "clientConfig"); this(ObjectUtil.checkNotNull(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(); private Builder(URI webSocketUri,
subprotocol = clientConfig.subprotocol(); String subprotocol,
version = clientConfig.version(); WebSocketVersion version,
allowExtensions = clientConfig.allowExtensions(); boolean allowExtensions,
customHeaders = clientConfig.customHeaders(); HttpHeaders customHeaders,
maxFramePayloadLength = clientConfig.maxFramePayloadLength(); int maxFramePayloadLength,
performMasking = clientConfig.performMasking(); boolean performMasking,
allowMaskMismatch = clientConfig.allowMaskMismatch(); boolean allowMaskMismatch,
handleCloseFrames = clientConfig.handleCloseFrames(); boolean handleCloseFrames,
sendCloseFrame = clientConfig.sendCloseFrame(); WebSocketCloseStatus sendCloseFrame,
dropPongFrames = clientConfig.dropPongFrames(); boolean dropPongFrames,
handshakeTimeoutMillis = clientConfig.handshakeTimeoutMillis(); long handshakeTimeoutMillis,
forceCloseTimeoutMillis = clientConfig.forceCloseTimeoutMillis(); long forceCloseTimeoutMillis,
absoluteUpgradeUrl = clientConfig.absoluteUpgradeUrl(); 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;
} }
/** /**

View File

@ -23,7 +23,11 @@ import io.netty.handler.codec.http.HttpHeaders;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
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;
import static io.netty.util.internal.ObjectUtil.*; import static io.netty.util.internal.ObjectUtil.*;
/** /**
@ -124,7 +128,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
int maxFramePayloadLength, boolean handleCloseFrames, int maxFramePayloadLength, boolean handleCloseFrames,
boolean performMasking, boolean allowMaskMismatch) { boolean performMasking, boolean allowMaskMismatch) {
this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength,
handleCloseFrames, performMasking, allowMaskMismatch, DEFAULT.handshakeTimeoutMillis()); handleCloseFrames, performMasking, allowMaskMismatch, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
/** /**
@ -185,7 +189,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
boolean allowExtensions, HttpHeaders customHeaders, boolean allowExtensions, HttpHeaders customHeaders,
int maxFramePayloadLength, boolean handleCloseFrames) { int maxFramePayloadLength, boolean handleCloseFrames) {
this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength,
handleCloseFrames, DEFAULT.handshakeTimeoutMillis()); handleCloseFrames, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
/** /**
@ -212,7 +216,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength, boolean allowExtensions, HttpHeaders customHeaders, int maxFramePayloadLength,
boolean handleCloseFrames, long handshakeTimeoutMillis) { boolean handleCloseFrames, long handshakeTimeoutMillis) {
this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength, this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, maxFramePayloadLength,
handleCloseFrames, DEFAULT.performMasking(), DEFAULT.allowMaskMismatch(), handshakeTimeoutMillis); handleCloseFrames, DEFAULT_PERFORM_MASKING, DEFAULT_ALLOW_MASK_MISMATCH, handshakeTimeoutMillis);
} }
/** /**
@ -234,7 +238,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
boolean allowExtensions, HttpHeaders customHeaders, boolean allowExtensions, HttpHeaders customHeaders,
int maxFramePayloadLength) { int maxFramePayloadLength) {
this(webSocketURL, version, subprotocol, allowExtensions, this(webSocketURL, version, subprotocol, allowExtensions,
customHeaders, maxFramePayloadLength, DEFAULT.handshakeTimeoutMillis()); customHeaders, maxFramePayloadLength, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
/** /**
@ -259,7 +263,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
boolean allowExtensions, HttpHeaders customHeaders, boolean allowExtensions, HttpHeaders customHeaders,
int maxFramePayloadLength, long handshakeTimeoutMillis) { int maxFramePayloadLength, long handshakeTimeoutMillis) {
this(webSocketURL, version, subprotocol, allowExtensions, customHeaders, this(webSocketURL, version, subprotocol, allowExtensions, customHeaders,
maxFramePayloadLength, DEFAULT.handleCloseFrames(), handshakeTimeoutMillis); maxFramePayloadLength, DEFAULT_HANDLE_CLOSE_FRAMES, handshakeTimeoutMillis);
} }
/** /**
@ -272,7 +276,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
* {@code true} if close frames should not be forwarded and just close the channel * {@code true} if close frames should not be forwarded and just close the channel
*/ */
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames) { public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames) {
this(handshaker, handleCloseFrames, DEFAULT.handshakeTimeoutMillis()); this(handshaker, handleCloseFrames, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
/** /**
@ -289,7 +293,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
*/ */
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames,
long handshakeTimeoutMillis) { long handshakeTimeoutMillis) {
this(handshaker, handleCloseFrames, DEFAULT.dropPongFrames(), handshakeTimeoutMillis); this(handshaker, handleCloseFrames, DEFAULT_DROP_PONG_FRAMES, handshakeTimeoutMillis);
} }
/** /**
@ -305,7 +309,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
*/ */
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames, public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, boolean handleCloseFrames,
boolean dropPongFrames) { boolean dropPongFrames) {
this(handshaker, handleCloseFrames, dropPongFrames, DEFAULT.handshakeTimeoutMillis()); this(handshaker, handleCloseFrames, dropPongFrames, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
/** /**
@ -340,7 +344,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
* was established to the remote peer. * was established to the remote peer.
*/ */
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker) { public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker) {
this(handshaker, DEFAULT.handshakeTimeoutMillis()); this(handshaker, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
/** /**
@ -354,7 +358,7 @@ public class WebSocketClientProtocolHandler extends WebSocketProtocolHandler {
* event {@link ClientHandshakeStateEvent#HANDSHAKE_TIMEOUT} * event {@link ClientHandshakeStateEvent#HANDSHAKE_TIMEOUT}
*/ */
public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, long handshakeTimeoutMillis) { public WebSocketClientProtocolHandler(WebSocketClientHandshaker handshaker, long handshakeTimeoutMillis) {
this(handshaker, DEFAULT.handleCloseFrames(), handshakeTimeoutMillis); this(handshaker, DEFAULT_HANDLE_CLOSE_FRAMES, handshakeTimeoutMillis);
} }
@Override @Override

View File

@ -25,8 +25,7 @@ import static io.netty.util.internal.ObjectUtil.checkPositive;
*/ */
public final class WebSocketServerProtocolConfig { public final class WebSocketServerProtocolConfig {
static final WebSocketServerProtocolConfig DEFAULT = new WebSocketServerProtocolConfig( static final long DEFAULT_HANDSHAKE_TIMEOUT_MILLIS = 10000L;
"/", null, false, 10000L, 0, true, WebSocketCloseStatus.NORMAL_CLOSURE, true, WebSocketDecoderConfig.DEFAULT);
private final String websocketPath; private final String websocketPath;
private final String subprotocols; private final String subprotocols;
@ -116,7 +115,8 @@ public final class WebSocketServerProtocolConfig {
} }
public static Builder newBuilder() { 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 { public static final class Builder {
@ -132,16 +132,36 @@ public final class WebSocketServerProtocolConfig {
private WebSocketDecoderConfig.Builder decoderConfigBuilder; private WebSocketDecoderConfig.Builder decoderConfigBuilder;
private Builder(WebSocketServerProtocolConfig serverConfig) { private Builder(WebSocketServerProtocolConfig serverConfig) {
ObjectUtil.checkNotNull(serverConfig, "serverConfig"); this(ObjectUtil.checkNotNull(serverConfig, "serverConfig").websocketPath(),
websocketPath = serverConfig.websocketPath(); serverConfig.subprotocols(),
subprotocols = serverConfig.subprotocols(); serverConfig.checkStartsWith(),
checkStartsWith = serverConfig.checkStartsWith(); serverConfig.handshakeTimeoutMillis(),
handshakeTimeoutMillis = serverConfig.handshakeTimeoutMillis(); serverConfig.forceCloseTimeoutMillis(),
forceCloseTimeoutMillis = serverConfig.forceCloseTimeoutMillis(); serverConfig.handleCloseFrames(),
handleCloseFrames = serverConfig.handleCloseFrames(); serverConfig.sendCloseFrame(),
sendCloseFrame = serverConfig.sendCloseFrame(); serverConfig.dropPongFrames(),
dropPongFrames = serverConfig.dropPongFrames(); serverConfig.decoderConfig()
decoderConfig = 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;
} }
/** /**

View File

@ -29,9 +29,9 @@ import io.netty.util.AttributeKey;
import java.util.List; import java.util.List;
import static io.netty.handler.codec.http.HttpVersion.*; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import static io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig.DEFAULT; import static io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig.DEFAULT_HANDSHAKE_TIMEOUT_MILLIS;
import static io.netty.util.internal.ObjectUtil.*; import static io.netty.util.internal.ObjectUtil.checkNotNull;
/** /**
* This handler does all the heavy lifting for you to run a websocket server. * This handler does all the heavy lifting for you to run a websocket server.
@ -118,7 +118,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
} }
public WebSocketServerProtocolHandler(String websocketPath) { public WebSocketServerProtocolHandler(String websocketPath) {
this(websocketPath, DEFAULT.handshakeTimeoutMillis()); this(websocketPath, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
public WebSocketServerProtocolHandler(String websocketPath, long handshakeTimeoutMillis) { public WebSocketServerProtocolHandler(String websocketPath, long handshakeTimeoutMillis) {
@ -126,7 +126,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
} }
public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith) { 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) { public WebSocketServerProtocolHandler(String websocketPath, boolean checkStartsWith, long handshakeTimeoutMillis) {
@ -134,7 +134,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
} }
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols) { 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) { public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, long handshakeTimeoutMillis) {
@ -142,7 +142,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
} }
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions) { 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, public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions,
@ -152,7 +152,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
boolean allowExtensions, int maxFrameSize) { 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, public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
@ -163,7 +163,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) { boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch) {
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch,
DEFAULT.handshakeTimeoutMillis()); DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions,
@ -175,7 +175,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith) { boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean checkStartsWith) {
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, checkStartsWith, this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, checkStartsWith,
DEFAULT.handshakeTimeoutMillis()); DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, public WebSocketServerProtocolHandler(String websocketPath, String subprotocols,
@ -189,7 +189,7 @@ public class WebSocketServerProtocolHandler extends WebSocketProtocolHandler {
boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch, boolean allowExtensions, int maxFrameSize, boolean allowMaskMismatch,
boolean checkStartsWith, boolean dropPongFrames) { boolean checkStartsWith, boolean dropPongFrames) {
this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, checkStartsWith, this(websocketPath, subprotocols, allowExtensions, maxFrameSize, allowMaskMismatch, checkStartsWith,
dropPongFrames, DEFAULT.handshakeTimeoutMillis()); dropPongFrames, DEFAULT_HANDSHAKE_TIMEOUT_MILLIS);
} }
public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions, public WebSocketServerProtocolHandler(String websocketPath, String subprotocols, boolean allowExtensions,