Skip to content

Commit 2e588f2

Browse files
authored
Merge pull request #5988 from eclipse-vertx/http-config-tuning
Tweaking HTTP configuration
2 parents da909d8 + f54f7f0 commit 2e588f2

22 files changed

Lines changed: 130 additions & 62 deletions

vertx-core/src/main/java/io/vertx/core/http/HttpClientConfig.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
package io.vertx.core.http;
1212

1313
import io.vertx.codegen.annotations.DataObject;
14+
import io.vertx.codegen.annotations.GenIgnore;
1415
import io.vertx.codegen.annotations.Unstable;
1516
import io.vertx.core.net.*;
1617
import io.vertx.core.tracing.TracingPolicy;
@@ -271,6 +272,18 @@ public HttpClientConfig setVersions(List<HttpVersion> versions) {
271272
return this;
272273
}
273274

275+
/**
276+
* Like {@link #setVersions(List)} using an array.
277+
*
278+
* @param versions the ordered list of supported versions
279+
* @return a reference to this, so the API can be used fluently
280+
*/
281+
@GenIgnore
282+
public HttpClientConfig setVersions(HttpVersion... versions) {
283+
this.versions = List.of(versions);
284+
return this;
285+
}
286+
274287
/**
275288
* @return the configuration specific to the HTTP/1.x protocol.
276289
*/

vertx-core/src/main/java/io/vertx/core/http/HttpServerConfig.java

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import io.netty.handler.codec.compression.CompressionOptions;
1414
import io.netty.handler.codec.compression.StandardCompressionOptions;
1515
import io.vertx.codegen.annotations.DataObject;
16+
import io.vertx.codegen.annotations.GenIgnore;
1617
import io.vertx.codegen.annotations.Unstable;
1718
import io.vertx.core.net.*;
1819
import io.vertx.core.tracing.TracingPolicy;
@@ -33,6 +34,8 @@ public class HttpServerConfig {
3334
*/
3435
public static final int DEFAULT_HTTP3_PORT = 443;
3536

37+
public static final Set<HttpVersion> DEFAULT_VERSIONS = Collections.unmodifiableSet(EnumSet.of(HttpVersion.HTTP_1_1, HttpVersion.HTTP_2));
38+
3639
public static final long DEFAULT_QUIC_INITIAL_MAX_DATA = 10_485_760L;
3740
public static final long DEFAULT_QUIC_INITIAL_MAX_STREAM_DATA_BIDI_LOCAL = 0L;
3841
public static final long DEFAULT_QUIC_INITIAL_MAX_STREAM_DATA_BIDI_REMOTE = 1_048_576L;
@@ -74,20 +77,29 @@ private static TcpServerConfig defaultTcpServerConfig() {
7477
private final TcpServerConfig tcpConfig;
7578
private final QuicServerConfig quicConfig;
7679

80+
/**
81+
* Copies the {@link HttpServerOptions}.
82+
* @param options the options to copy
83+
*/
7784
public HttpServerConfig(HttpServerOptions options) {
7885

79-
List<CompressionOptions> compressors = options.getCompression().getCompressors();
80-
if (compressors == null) {
81-
int compressionLevel = options.getCompressionLevel();
82-
compressors = Arrays.asList(StandardCompressionOptions.gzip(compressionLevel, 15, 8), StandardCompressionOptions.deflate(compressionLevel, 15, 8));
86+
HttpCompressionConfig compression;
87+
if (options.isCompressionSupported() || options.isDecompressionSupported()) {
88+
List<CompressionOptions> compressors = options.getCompression().getCompressors();
89+
if (compressors == null) {
90+
int compressionLevel = options.getCompressionLevel();
91+
compressors = Arrays.asList(StandardCompressionOptions.gzip(compressionLevel, 15, 8), StandardCompressionOptions.deflate(compressionLevel, 15, 8));
92+
}
93+
compression = new HttpCompressionConfig();
94+
compression.setCompressionEnabled(options.isCompressionSupported());
95+
compression.setDecompressionEnabled(options.isDecompressionSupported());
96+
compression.setContentSizeThreshold(options.getCompressionContentSizeThreshold());
97+
compression.setCompressors(compressors);
98+
} else {
99+
compression = null;
83100
}
84-
HttpCompressionConfig compression = new HttpCompressionConfig();
85-
compression.setCompressionEnabled(options.isCompressionSupported());
86-
compression.setDecompressionEnabled(options.isDecompressionSupported());
87-
compression.setContentSizeThreshold(options.getCompressionContentSizeThreshold());
88-
compression.setCompressors(compressors);
89101

90-
this.versions = EnumSet.of(HttpVersion.HTTP_1_1, HttpVersion.HTTP_2);
102+
this.versions = EnumSet.copyOf(DEFAULT_VERSIONS);
91103
this.maxFormAttributeSize = options.getMaxFormAttributeSize();
92104
this.maxFormFields = options.getMaxFormFields();
93105
this.maxFormBufferedBytes = options.getMaxFormBufferedBytes();
@@ -103,8 +115,11 @@ public HttpServerConfig(HttpServerOptions options) {
103115
this.quicConfig = defaultQuicConfig();
104116
}
105117

118+
/**
119+
* Create a default configuration of a server accepting HTTP/1.1 and HTTP/2 protocols.
120+
*/
106121
public HttpServerConfig() {
107-
this.versions = EnumSet.noneOf(HttpVersion.class);
122+
this.versions = EnumSet.of(HttpVersion.HTTP_1_1, HttpVersion.HTTP_2);
108123
this.maxFormAttributeSize = HttpServerOptions.DEFAULT_MAX_FORM_ATTRIBUTE_SIZE;
109124
this.maxFormFields = HttpServerOptions.DEFAULT_MAX_FORM_FIELDS;
110125
this.maxFormBufferedBytes = HttpServerOptions.DEFAULT_MAX_FORM_BUFFERED_SIZE;
@@ -115,12 +130,17 @@ public HttpServerConfig() {
115130
this.http1Config = null;
116131
this.http2Config = null;
117132
this.http3Config = null;
118-
this.webSocketConfig = new WebSocketServerConfig();
119-
this.compression = new HttpCompressionConfig();
133+
this.webSocketConfig = null;
134+
this.compression = null;
120135
this.tcpConfig = defaultTcpServerConfig();
121136
this.quicConfig = defaultQuicConfig();
122137
}
123138

139+
/**
140+
* Copy constructor
141+
*
142+
* @param other the config to be copied
143+
*/
124144
public HttpServerConfig(HttpServerConfig other) {
125145
this.versions = EnumSet.copyOf(other.versions);
126146
this.maxFormAttributeSize = other.maxFormAttributeSize;
@@ -203,13 +223,16 @@ public HttpServerConfig setVersions(Set<HttpVersion> versions) {
203223
}
204224

205225
/**
206-
* Add an HTTP version.
226+
* Set the HTTP versions.
207227
*
208-
* @param version an additional version to support
228+
* @param versions the versions
209229
* @return a reference to this, so the API can be used fluently
210230
*/
211-
public HttpServerConfig addVersion(HttpVersion version) {
212-
versions.add(version);
231+
@GenIgnore
232+
public HttpServerConfig setVersions(HttpVersion... versions) {
233+
EnumSet<HttpVersion> s = EnumSet.noneOf(HttpVersion.class);
234+
Collections.addAll(s, versions);
235+
this.versions = s;
213236
return this;
214237
}
215238

vertx-core/src/main/java/io/vertx/core/http/impl/HttpClientBuilderInternal.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.vertx.core.internal.http.HttpClientInternal;
1818
import io.vertx.core.internal.net.NetClientInternal;
1919
import io.vertx.core.net.ClientSSLOptions;
20+
import io.vertx.core.net.NetworkLogging;
2021
import io.vertx.core.net.ProxyOptions;
2122
import io.vertx.core.net.endpoint.LoadBalancer;
2223
import io.vertx.core.net.AddressResolver;
@@ -257,12 +258,13 @@ public HttpClientAgent build() {
257258
.protocol("http")
258259
.sslOptions(sslOptions)
259260
.build();
261+
NetworkLogging networkLogging = co.getTcpConfig().getNetworkLogging();
260262
transport = new TcpHttpClientTransport(
261263
tcpClient,
262264
co.getTracingPolicy(),
263265
co.isDecompressionEnabled(),
264-
co.getTcpConfig().getNetworkLogging() != null,
265-
co.getTcpConfig().getNetworkLogging() != null ? co.getTcpConfig().getNetworkLogging().getDataFormat() : null,
266+
networkLogging != null && networkLogging.isEnabled(),
267+
networkLogging != null ? networkLogging.getDataFormat() : null,
266268
co.isForceSni(),
267269
supportedVersions.contains(HttpVersion.HTTP_1_1) || supportedVersions.contains(HttpVersion.HTTP_1_0) ? (co.getHttp1Config() != null ? co.getHttp1Config() : new Http1ClientConfig()) : null,
268270
supportedVersions.contains(HttpVersion.HTTP_2) ? (co.getHttp2Config() != null ? co.getHttp2Config() : new Http2ClientConfig()) : null,

vertx-core/src/main/java/io/vertx/core/http/impl/tcp/TcpHttpClientTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static TcpHttpClientTransport create(NetClientInternal netClient,
6767
return new TcpHttpClientTransport(netClient,
6868
config.getTracingPolicy(),
6969
config.isDecompressionEnabled(),
70-
config.getTcpConfig().getNetworkLogging() != null,
70+
config.getTcpConfig().getNetworkLogging() != null && config.getTcpConfig().getNetworkLogging().isEnabled(),
7171
config.getTcpConfig().getNetworkLogging() != null ? config.getTcpConfig().getNetworkLogging().getDataFormat() : null,
7272
config.isForceSni(),
7373
config.getHttp1Config(),

vertx-core/src/main/java/io/vertx/core/http/impl/tcp/TcpHttpServer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public synchronized Future<HttpServer> listen(ContextInternal context, SocketAdd
206206
HttpCompressionConfig compression = config.getCompression();
207207
ServerSSLOptions sslOptions = configureSSLOptions(config, this.sslOptions);
208208
NetServerInternal server = new NetServerBuilder(vertx, config.getTcpConfig(), sslOptions)
209-
.fileRegionEnabled(!compression.isCompressionEnabled())
209+
.fileRegionEnabled(compression == null || !compression.isCompressionEnabled())
210210
.cleanable(false)
211211
.protocol("http")
212212
.build();
@@ -230,7 +230,7 @@ public synchronized Future<HttpServer> listen(ContextInternal context, SocketAdd
230230
exceptionHandler,
231231
config.getHttp2Config().getConnectionWindowSize());
232232

233-
List<CompressionOptions> compressors = compression.getCompressors();
233+
List<CompressionOptions> compressors = compression != null ? compression.getCompressors() : null;
234234
HttpServerConnectionInitializer initializer = new HttpServerConnectionInitializer(
235235
listenContext,
236236
context.threadingModel(),
@@ -242,7 +242,7 @@ public synchronized Future<HttpServer> listen(ContextInternal context, SocketAdd
242242
config.getTracingPolicy(),
243243
config.getTcpConfig().getNetworkLogging() != null,
244244
compressors != null ? compressors.toArray(new CompressionOptions[0]) : null,
245-
compression.getContentSizeThreshold(),
245+
compression != null ? compression.getContentSizeThreshold() : 0,
246246
config.isHandle100ContinueAutomatically(),
247247
config.getMaxFormAttributeSize(),
248248
config.getMaxFormFields(),

vertx-core/src/main/java/io/vertx/core/net/NetworkLogging.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,37 @@
2323
@DataObject
2424
public class NetworkLogging {
2525

26+
private boolean enabled;
2627
private ByteBufFormat dataFormat;
2728

2829
public NetworkLogging() {
30+
enabled = NetworkOptions.DEFAULT_LOG_ENABLED;
2931
dataFormat = NetworkOptions.DEFAULT_LOG_ACTIVITY_FORMAT;
3032
}
3133

3234
public NetworkLogging(NetworkLogging other) {
35+
enabled = other.enabled;
3336
dataFormat = other.dataFormat;
3437
}
3538

39+
/**
40+
* @return {@code} when network logging is enabled
41+
*/
42+
public boolean isEnabled() {
43+
return enabled;
44+
}
45+
46+
/**
47+
* Set to true to enable network logging: Netty's pipeline is configured for logging on Netty's logger.
48+
*
49+
* @param enabled true for logging the network activity
50+
* @return a reference to this, so the API can be used fluently
51+
*/
52+
public NetworkLogging setEnabled(boolean enabled) {
53+
this.enabled = enabled;
54+
return this;
55+
}
56+
3657
/**
3758
* @return per stream Netty's logging handler's data format.
3859
*/

vertx-core/src/main/java/io/vertx/core/net/TcpEndpointConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public TcpEndpointConfig(TCPSSLOptions options) {
4747
setIdleTimeout(Duration.of(options.getIdleTimeout(), options.getIdleTimeoutUnit().toChronoUnit()));
4848
setReadIdleTimeout(Duration.of(options.getReadIdleTimeout(), options.getIdleTimeoutUnit().toChronoUnit()));
4949
setWriteIdleTimeout(Duration.of(options.getWriteIdleTimeout(), options.getIdleTimeoutUnit().toChronoUnit()));
50-
setNetworkLogging(options.getLogActivity() ? new NetworkLogging().setDataFormat(options.getActivityLogDataFormat()) : null);
50+
setNetworkLogging(options.getLogActivity() ? new NetworkLogging().setEnabled(true).setDataFormat(options.getActivityLogDataFormat()) : null);
5151
setSsl(options.isSsl());
5252
}
5353

vertx-core/src/main/java/io/vertx/core/net/impl/quic/QuicClientImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ private Future<QuicConnection> connect(Channel ch,
187187
@Override
188188
protected void initChannel(Channel ch) {
189189
connectionGroup.add(ch);
190-
ByteBufFormat activityLogging = config.getNetworkLogging() != null ? config.getNetworkLogging().getDataFormat() : null;
190+
NetworkLogging networkLogging = config.getNetworkLogging();
191+
ByteBufFormat activityLogging = networkLogging != null && networkLogging.isEnabled()? networkLogging.getDataFormat() : null;
191192
QuicConnectionHandler handler = new QuicConnectionHandler(context, metrics, config.getIdleTimeout(),
192193
config.getReadIdleTimeout(), config.getWriteIdleTimeout(), activityLogging, remoteAddress, promise::tryComplete);
193194
ch.pipeline().addLast("handler", handler);

vertx-core/src/main/java/io/vertx/core/net/impl/quic/QuicServerImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ protected QuicCodecBuilder<?> codecBuilder(ContextInternal context, SslContextPr
157157
protected void initChannel(Channel ch) {
158158
connectionGroup.add(ch);
159159
QuicChannel channel = (QuicChannel) ch;
160-
ByteBufFormat activityLogging = config.getNetworkLogging() != null ? config.getNetworkLogging().getDataFormat() : null;
160+
NetworkLogging networkLogging = config.getNetworkLogging();
161+
ByteBufFormat activityLogging = networkLogging != null && networkLogging.isEnabled() ? networkLogging.getDataFormat() : null;
161162
QuicConnectionHandler handler = new QuicConnectionHandler(context, metrics, config.getIdleTimeout(),
162163
config.getReadIdleTimeout(), config.getWriteIdleTimeout(), activityLogging,
163164
vertx.transport().convert(channel.remoteSocketAddress()), QuicServerImpl.this.handler);

vertx-core/src/main/java/io/vertx/core/net/impl/tcp/NetClientImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ protected void handleClose(Completable<Void> completion) {
8080
NetClientImpl.this.handleClose(completion);
8181
}
8282
};
83+
NetworkLogging networkLogging = config.getNetworkLogging();
8384
this.config = config;
8485
this.registerWriteHandler = registerWriteHandler;
8586
this.sslContextManager = new SslContextManager(SslContextManager.resolveEngineOptions(config.getSslEngineOptions(), sslOptions != null && sslOptions.isUseAlpn()));
8687
this.metrics = vertx.metrics() != null ? vertx.metrics().createTcpClientMetrics(config, protocol) : null;
87-
this.logging = config.getNetworkLogging() != null ? config.getNetworkLogging().getDataFormat() : null;
88+
this.logging = networkLogging != null && networkLogging.isEnabled() ? networkLogging.getDataFormat() : null;
8889
this.idleTimeout = config.getIdleTimeout() != null ? config.getIdleTimeout() : Duration.ofMillis(0L);
8990
this.readIdleTimeout = config.getReadIdleTimeout() != null ? config.getReadIdleTimeout() : Duration.ofMillis(0L);
9091
this.writeIdleTimeout = config.getWriteIdleTimeout() != null ? config.getWriteIdleTimeout() : Duration.ofMillis(0L);

0 commit comments

Comments
 (0)