Skip to content

Commit 820f11e

Browse files
authored
Netty HttpClient changes (#45143)
Netty HttpClient changes
1 parent dbbeb65 commit 820f11e

File tree

2 files changed

+38
-29
lines changed

2 files changed

+38
-29
lines changed

sdk/core/azure-core-http-netty/src/main/java/com/azure/core/http/netty/NettyAsyncHttpClientBuilder.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,34 @@ public NettyAsyncHttpClientBuilder(HttpClient nettyHttpClient) {
174174
public com.azure.core.http.HttpClient build() {
175175
HttpClient nettyHttpClient;
176176

177+
Configuration buildConfiguration
178+
= (configuration == null) ? Configuration.getGlobalConfiguration() : configuration;
179+
180+
ProxyOptions buildProxyOptions
181+
= proxyOptions == null ? ProxyOptions.fromConfiguration(buildConfiguration, true) : proxyOptions;
182+
183+
/*
184+
* Only configure the custom authorization challenge handler and challenge holder when using an authenticated
185+
* HTTP proxy. All other proxying such as SOCKS4, SOCKS5, and anonymous HTTP will use Netty's built-in handlers.
186+
*/
187+
boolean useCustomProxyHandler = shouldUseCustomProxyHandler(buildProxyOptions);
188+
AuthorizationChallengeHandler handler = useCustomProxyHandler
189+
? new AuthorizationChallengeHandler(buildProxyOptions.getUsername(), buildProxyOptions.getPassword())
190+
: null;
191+
AtomicReference<ChallengeHolder> proxyChallengeHolder = useCustomProxyHandler ? new AtomicReference<>() : null;
192+
177193
// Used to track if the builder set the DefaultAddressResolverGroup. If it did, when proxying it allows the
178194
// no-op address resolver to be set.
179-
boolean addressResolverWasSetByBuilder = false;
180195
if (this.baseHttpClient != null) {
181196
nettyHttpClient = baseHttpClient;
182197
} else if (this.connectionProvider != null) {
183-
nettyHttpClient = HttpClient.create(this.connectionProvider).resolver(DefaultAddressResolverGroup.INSTANCE);
184-
addressResolverWasSetByBuilder = true;
198+
nettyHttpClient = HttpClient.create(this.connectionProvider);
185199
} else {
186-
nettyHttpClient = HttpClient.create().resolver(DefaultAddressResolverGroup.INSTANCE);
187-
addressResolverWasSetByBuilder = true;
200+
nettyHttpClient = HttpClient.create();
201+
}
202+
203+
if (proxyOptions == null) {
204+
nettyHttpClient = nettyHttpClient.resolver(DefaultAddressResolverGroup.INSTANCE);
188205
}
189206

190207
long writeTimeout = getTimeout(this.writeTimeout, getDefaultWriteTimeout()).toMillis();
@@ -213,22 +230,6 @@ public com.azure.core.http.HttpClient build() {
213230
nettyHttpClient.wiretap(enableWiretap);
214231
}
215232

216-
Configuration buildConfiguration
217-
= (configuration == null) ? Configuration.getGlobalConfiguration() : configuration;
218-
219-
ProxyOptions buildProxyOptions
220-
= proxyOptions == null ? ProxyOptions.fromConfiguration(buildConfiguration, true) : proxyOptions;
221-
222-
/*
223-
* Only configure the custom authorization challenge handler and challenge holder when using an authenticated
224-
* HTTP proxy. All other proxying such as SOCKS4, SOCKS5, and anonymous HTTP will use Netty's built-in handlers.
225-
*/
226-
boolean useCustomProxyHandler = shouldUseCustomProxyHandler(buildProxyOptions);
227-
AuthorizationChallengeHandler handler = useCustomProxyHandler
228-
? new AuthorizationChallengeHandler(buildProxyOptions.getUsername(), buildProxyOptions.getPassword())
229-
: null;
230-
AtomicReference<ChallengeHolder> proxyChallengeHolder = useCustomProxyHandler ? new AtomicReference<>() : null;
231-
232233
boolean addProxyHandler = false;
233234

234235
if (eventLoopGroup != null) {
@@ -257,6 +258,12 @@ public com.azure.core.http.HttpClient build() {
257258
handler, proxyChallengeHolder));
258259
}
259260
});
261+
262+
AddressResolverGroup<?> resolver = nettyHttpClient.configuration().resolver();
263+
if (resolver == null) {
264+
// This mimics behaviors seen when Reactor Netty proxying is used.
265+
nettyHttpClient = nettyHttpClient.resolver(NoopAddressResolverGroup.INSTANCE);
266+
}
260267
} else {
261268
nettyHttpClient
262269
= nettyHttpClient.proxy(proxy -> proxy.type(toReactorNettyProxyType(buildProxyOptions.getType()))
@@ -265,12 +272,6 @@ public com.azure.core.http.HttpClient build() {
265272
.password(ignored -> buildProxyOptions.getPassword())
266273
.nonProxyHosts(buildProxyOptions.getNonProxyHosts()));
267274
}
268-
269-
AddressResolverGroup<?> resolver = nettyHttpClient.configuration().resolver();
270-
if (resolver == null || addressResolverWasSetByBuilder) {
271-
// This mimics behaviors seen when Reactor Netty proxying is used.
272-
nettyHttpClient = nettyHttpClient.resolver(NoopAddressResolverGroup.INSTANCE);
273-
}
274275
}
275276

276277
return new NettyAsyncHttpClient(nettyHttpClient, disableBufferCopy, addProxyHandler);

sdk/core/azure-core-http-netty/src/main/java/com/azure/core/http/netty/implementation/NettyUtility.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,18 @@ public static void closeConnection(Connection reactorNettyConnection) {
7676
// From there the only thing that needs to be checked is whether the inbound has been disposed (completed),
7777
// and if not dispose it (aka drain it).
7878
if (!channelOperations.isInboundDisposed()) {
79-
channelOperations.channel().eventLoop().execute(channelOperations::discard);
79+
if (channelOperations.channel().isRegistered()) {
80+
channelOperations.channel().eventLoop().execute(channelOperations::discard);
81+
} else {
82+
channelOperations.discard();
83+
}
8084
}
8185
} else if (!reactorNettyConnection.isDisposed()) {
82-
reactorNettyConnection.channel().eventLoop().execute(reactorNettyConnection::dispose);
86+
if (reactorNettyConnection.channel().isRegistered()) {
87+
reactorNettyConnection.channel().eventLoop().execute(reactorNettyConnection::dispose);
88+
} else {
89+
reactorNettyConnection.dispose();
90+
}
8391
}
8492
}
8593

0 commit comments

Comments
 (0)