Skip to content

Commit 4400ac5

Browse files
authored
Better Configuration for Netty Connector (#5937)
Signed-off-by: jansupol <[email protected]>
1 parent 1f13aa5 commit 4400ac5

File tree

16 files changed

+3170
-190
lines changed

16 files changed

+3170
-190
lines changed

connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/Expect100ContinueConnectorExtension.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 2025 Oracle and/or its affiliates. All rights reserved.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License v. 2.0, which is available at
@@ -30,20 +30,21 @@
3030

3131
class Expect100ContinueConnectorExtension
3232
implements ConnectorExtension<HttpRequest, IOException> {
33+
34+
private final NettyConnectorProvider.Config.RW requestConfiguration;
35+
36+
Expect100ContinueConnectorExtension(NettyConnectorProvider.Config.RW requestConfiguration) {
37+
this.requestConfiguration = requestConfiguration;
38+
}
39+
3340
private static final String EXCEPTION_MESSAGE = "Server rejected operation";
3441
@Override
3542
public void invoke(ClientRequest request, HttpRequest extensionParam) {
3643

3744
final long length = request.getLengthLong();
38-
final RequestEntityProcessing entityProcessing = request.resolveProperty(
39-
ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.class);
40-
41-
final Boolean expectContinueActivated = request.resolveProperty(
42-
ClientProperties.EXPECT_100_CONTINUE, Boolean.class);
43-
final Long expectContinueSizeThreshold = request.resolveProperty(
44-
ClientProperties.EXPECT_100_CONTINUE_THRESHOLD_SIZE,
45-
ClientProperties.DEFAULT_EXPECT_100_CONTINUE_THRESHOLD_SIZE);
46-
45+
final RequestEntityProcessing entityProcessing = requestConfiguration.requestEntityProcessing(request);
46+
final Boolean expectContinueActivated = requestConfiguration.expect100Continue(request);
47+
final long expectContinueSizeThreshold = requestConfiguration.expect100ContinueThreshold(request);
4748
final boolean allowStreaming = length > expectContinueSizeThreshold
4849
|| entityProcessing == RequestEntityProcessing.CHUNKED;
4950

connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/JerseyClientHandler.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
import javax.ws.rs.core.Response;
3030

31-
import org.glassfish.jersey.client.ClientProperties;
3231
import org.glassfish.jersey.client.ClientRequest;
3332
import org.glassfish.jersey.client.ClientResponse;
3433
import org.glassfish.jersey.http.HttpHeaders;
@@ -53,37 +52,35 @@
5352
*/
5453
class JerseyClientHandler extends SimpleChannelInboundHandler<HttpObject> {
5554

56-
private static final int DEFAULT_MAX_REDIRECTS = 5;
57-
5855
// Modified only by the same thread. No need to synchronize it.
5956
private final Set<URI> redirectUriHistory;
6057
private final ClientRequest jerseyRequest;
6158
private final CompletableFuture<ClientResponse> responseAvailable;
6259
private final CompletableFuture<?> responseDone;
63-
private final boolean followRedirects;
64-
private final int maxRedirects;
6560
private final NettyConnector connector;
6661
private final NettyHttpRedirectController redirectController;
62+
private final NettyConnectorProvider.Config.RW requestConfiguration;
6763

6864
private NettyInputStream nis;
6965
private ClientResponse jerseyResponse;
7066

7167
private boolean readTimedOut;
7268

7369
JerseyClientHandler(ClientRequest request, CompletableFuture<ClientResponse> responseAvailable,
74-
CompletableFuture<?> responseDone, Set<URI> redirectUriHistory, NettyConnector connector) {
70+
CompletableFuture<?> responseDone, Set<URI> redirectUriHistory, NettyConnector connector,
71+
NettyConnectorProvider.Config.RW requestConfiguration) {
7572
this.redirectUriHistory = redirectUriHistory;
7673
this.jerseyRequest = request;
7774
this.responseAvailable = responseAvailable;
7875
this.responseDone = responseDone;
79-
// Follow redirects by default
80-
this.followRedirects = jerseyRequest.resolveProperty(ClientProperties.FOLLOW_REDIRECTS, true);
81-
this.maxRedirects = jerseyRequest.resolveProperty(NettyClientProperties.MAX_REDIRECTS, DEFAULT_MAX_REDIRECTS);
76+
this.requestConfiguration = requestConfiguration;
8277
this.connector = connector;
78+
// Follow redirects by default
79+
requestConfiguration.followRedirects(jerseyRequest);
80+
requestConfiguration.maxRedirects(jerseyRequest);
8381

84-
final NettyHttpRedirectController customRedirectController = jerseyRequest
85-
.resolveProperty(NettyClientProperties.HTTP_REDIRECT_CONTROLLER, NettyHttpRedirectController.class);
86-
this.redirectController = customRedirectController == null ? new NettyHttpRedirectController() : customRedirectController;
82+
this.redirectController = requestConfiguration.redirectController(jerseyRequest);
83+
this.redirectController.init(requestConfiguration);
8784
}
8885

8986
@Override
@@ -109,7 +106,7 @@ protected void notifyResponse(ChannelHandlerContext ctx) {
109106
ClientResponse cr = jerseyResponse;
110107
jerseyResponse = null;
111108
int responseStatus = cr.getStatus();
112-
if (followRedirects
109+
if (Boolean.TRUE.equals(requestConfiguration.followRedirects())
113110
&& (responseStatus == ResponseStatus.Redirect3xx.MOVED_PERMANENTLY_301.getStatusCode()
114111
|| responseStatus == ResponseStatus.Redirect3xx.FOUND_302.getStatusCode()
115112
|| responseStatus == ResponseStatus.Redirect3xx.SEE_OTHER_303.getStatusCode()
@@ -136,16 +133,17 @@ protected void notifyResponse(ChannelHandlerContext ctx) {
136133
// infinite loop detection
137134
responseAvailable.completeExceptionally(
138135
new RedirectException(LocalizationMessages.REDIRECT_INFINITE_LOOP()));
139-
} else if (redirectUriHistory.size() > maxRedirects) {
136+
} else if (redirectUriHistory.size() > requestConfiguration.maxRedirects.get()) {
140137
// maximal number of redirection
141-
responseAvailable.completeExceptionally(
142-
new RedirectException(LocalizationMessages.REDIRECT_LIMIT_REACHED(maxRedirects)));
138+
responseAvailable.completeExceptionally(new RedirectException(
139+
LocalizationMessages.REDIRECT_LIMIT_REACHED(requestConfiguration.maxRedirects.get())));
143140
} else {
144141
ClientRequest newReq = new ClientRequest(jerseyRequest);
145142
newReq.setUri(newUri);
146143
ctx.close();
147144
if (redirectController.prepareRedirect(newReq, cr)) {
148-
final NettyConnector newConnector = new NettyConnector(newReq.getClient());
145+
final NettyConnector newConnector =
146+
new NettyConnector(newReq.getClient(), connector.connectorConfiguration);
149147
newConnector.execute(newReq, redirectUriHistory, new CompletableFuture<ClientResponse>() {
150148
@Override
151149
public boolean complete(ClientResponse value) {

connectors/netty-connector/src/main/java/org/glassfish/jersey/netty/connector/NettyClientProperties.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ public class NettyClientProperties {
157157
DEFAULT_HEADER_SIZE = 8192;
158158

159159
/**
160-
* Parameter which allows extending of the initial line length for the Netty connector
160+
* Parameter which allows extending of the first line length of the HTTP header for the Netty connector.
161+
* Taken from {@link io.netty.handler.codec.http.HttpClientCodec#HttpClientCodec(int, int, int)}.
161162
*
162163
* @since 2.44
163164
*/
@@ -166,12 +167,12 @@ public class NettyClientProperties {
166167

167168
/**
168169
* Default initial line length for Netty Connector.
169-
* Taken from {@link io.netty.handler.codec.http.HttpClientCodec#HttpClientCodec(int, int, int)}
170+
* Typically, set this to the same value as {@link #MAX_HEADER_SIZE}.
170171
*
171172
* @since 2.44
172173
*/
173174
public static final Integer
174-
DEFAULT_INITIAL_LINE_LENGTH = 4096;
175+
DEFAULT_INITIAL_LINE_LENGTH = 8192;
175176

176177
/**
177178
* Parameter which allows extending of the chunk size for the Netty connector
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2025 Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Public License v. 2.0, which is available at
6+
* http://www.eclipse.org/legal/epl-2.0.
7+
*
8+
* This Source Code may also be made available under the following Secondary
9+
* Licenses when the conditions for such availability set forth in the
10+
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11+
* version 2 with the GNU Classpath Exception, which is available at
12+
* https://www.gnu.org/software/classpath/license.html.
13+
*
14+
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15+
*/
16+
17+
package org.glassfish.jersey.netty.connector;
18+
19+
import org.glassfish.jersey.client.ClientRequest;
20+
21+
import java.net.URI;
22+
23+
/**
24+
* Adjustable connection pooling controller.
25+
*/
26+
public class NettyConnectionController {
27+
/**
28+
* Get the group of connections to be pooled, purged idle, and reused together.
29+
*
30+
* @param clientRequest the HTTP client request.
31+
* @param uri the uri for the HTTP client request.
32+
* @param hostName the hostname for the request. Can differ from the hostname in the uri based on other request attributes.
33+
* @param port the real port for the request. Can differ from the port in the uri based on other request attributes.
34+
* @return the group of connections identifier.
35+
*/
36+
public String getConnectionGroup(ClientRequest clientRequest, URI uri, String hostName, int port) {
37+
return uri.getScheme() + "://" + hostName + ":" + port;
38+
}
39+
}

0 commit comments

Comments
 (0)