Skip to content

Commit

Permalink
[ISSUE #3284] correct fix #3063 (#3314)
Browse files Browse the repository at this point in the history
* [ISSUE #3284] correct fix #3063

* use ObjectProvider

* fix comment
  • Loading branch information
loongs-zhang authored Apr 25, 2022
1 parent 55cacb0 commit 2eef279
Showing 1 changed file with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import reactor.netty.tcp.ProxyProvider;
import reactor.netty.tcp.SslProvider;
import reactor.netty.tcp.TcpClient;
import reactor.netty.tcp.TcpResources;

import java.security.cert.X509Certificate;
import java.util.Objects;
Expand All @@ -66,14 +65,30 @@ public HttpClientProperties httpClientProperties() {
return new HttpClientProperties();
}

/**
* Http client loop resource.
*
* @param properties the properties
* @return the http client loop resource
*/
@Bean
@ConditionalOnProperty("shenyu.netty.httpclient.threadPool.prefix")
public LoopResources httpClientLoopResource(final HttpClientProperties properties) {
HttpClientProperties.ThreadPool threadPool = properties.getThreadPool();
return LoopResources.create(threadPool.getPrefix(), threadPool.getSelectCount(),
threadPool.getWorkerCount(), threadPool.getDaemon());
}

/**
* Shenyu http client.
*
* @param properties the properties
* @param provider the loop resources bean provider
* @return the http client
*/
@Bean
public HttpClient httpClient(final HttpClientProperties properties) {
public HttpClient httpClient(final HttpClientProperties properties,
final ObjectProvider<LoopResources> provider) {
// configure pool resources.
HttpClientProperties.Pool pool = properties.getPool();
ConnectionProvider connectionProvider = buildConnectionProvider(pool);
Expand All @@ -92,11 +107,9 @@ public HttpClient httpClient(final HttpClientProperties properties) {
connection.addHandlerLast(new WriteTimeoutHandler(properties.getWriteTimeout(), TimeUnit.MILLISECONDS));
connection.addHandlerLast(new ReadTimeoutHandler(properties.getReadTimeout(), TimeUnit.MILLISECONDS));
});
HttpClientProperties.ThreadPool threadPool = properties.getThreadPool();
if (StringUtils.isNotEmpty(threadPool.getPrefix())) {
LoopResources resources = LoopResources.create(threadPool.getPrefix(),
threadPool.getSelectCount(), threadPool.getWorkerCount(), threadPool.getDaemon());
TcpResources.set(resources);
final LoopResources loopResources = provider.getIfAvailable();
if (Objects.nonNull(loopResources)) {
tcpClient = tcpClient.runOn(loopResources);
}
return tcpClient;
});
Expand All @@ -113,7 +126,7 @@ public HttpClient httpClient(final HttpClientProperties properties) {
// see https://github.com/reactor/reactor-netty/issues/388
return httpClient.keepAlive(properties.isKeepAlive());
}

private void setSsl(final SslProvider.SslContextSpec sslContextSpec, final HttpClientProperties.Ssl ssl) {
SslContextBuilder sslContextBuilder = SslContextBuilder.forClient();
X509Certificate[] trustedX509Certificates = ssl.getTrustedX509CertificatesForTrustManager();
Expand All @@ -129,7 +142,7 @@ private void setSsl(final SslProvider.SslContextSpec sslContextSpec, final HttpC
.closeNotifyFlushTimeout(ssl.getCloseNotifyFlushTimeout())
.closeNotifyReadTimeout(ssl.getCloseNotifyReadTimeout());
}

private TcpClient setTcpClientProxy(final TcpClient tcpClient, final HttpClientProperties.Proxy proxy) {
return tcpClient.proxy(proxySpec -> {
ProxyProvider.Builder builder = proxySpec
Expand All @@ -145,7 +158,7 @@ private TcpClient setTcpClientProxy(final TcpClient tcpClient, final HttpClientP
.to(builder::nonProxyHosts);
});
}

private ConnectionProvider buildConnectionProvider(final HttpClientProperties.Pool pool) {
ConnectionProvider connectionProvider;
if (pool.getType() == HttpClientProperties.Pool.PoolType.DISABLED) {
Expand Down

0 comments on commit 2eef279

Please sign in to comment.