Merge branch '2.2.x'
This commit is contained in:
@@ -637,36 +637,7 @@ public class GatewayAutoConfiguration {
|
||||
public HttpClient gatewayHttpClient(HttpClientProperties properties, List<HttpClientCustomizer> customizers) {
|
||||
|
||||
// configure pool resources
|
||||
HttpClientProperties.Pool pool = properties.getPool();
|
||||
|
||||
ConnectionProvider connectionProvider;
|
||||
if (pool.getType() == DISABLED) {
|
||||
connectionProvider = ConnectionProvider.newConnection();
|
||||
}
|
||||
else if (pool.getType() == FIXED) {
|
||||
ConnectionProvider.Builder builder = ConnectionProvider.builder(pool.getName())
|
||||
.maxConnections(pool.getMaxConnections()).pendingAcquireMaxCount(-1)
|
||||
.pendingAcquireTimeout(Duration.ofMillis(pool.getAcquireTimeout()));
|
||||
if (pool.getMaxIdleTime() != null) {
|
||||
builder.maxIdleTime(pool.getMaxIdleTime());
|
||||
}
|
||||
if (pool.getMaxLifeTime() != null) {
|
||||
builder.maxLifeTime(pool.getMaxLifeTime());
|
||||
}
|
||||
connectionProvider = builder.build();
|
||||
}
|
||||
else {
|
||||
ConnectionProvider.Builder builder = ConnectionProvider.builder(pool.getName())
|
||||
.maxConnections(Integer.MAX_VALUE).pendingAcquireTimeout(Duration.ofMillis(0))
|
||||
.pendingAcquireMaxCount(-1);
|
||||
if (pool.getMaxIdleTime() != null) {
|
||||
builder.maxIdleTime(pool.getMaxIdleTime());
|
||||
}
|
||||
if (pool.getMaxLifeTime() != null) {
|
||||
builder.maxLifeTime(pool.getMaxLifeTime());
|
||||
}
|
||||
connectionProvider = builder.build();
|
||||
}
|
||||
ConnectionProvider connectionProvider = buildConnectionProvider(properties);
|
||||
|
||||
HttpClient httpClient = HttpClient.create(connectionProvider)
|
||||
// TODO: move customizations to HttpClientCustomizers
|
||||
@@ -754,6 +725,42 @@ public class GatewayAutoConfiguration {
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
private ConnectionProvider buildConnectionProvider(
|
||||
HttpClientProperties properties) {
|
||||
HttpClientProperties.Pool pool = properties.getPool();
|
||||
|
||||
ConnectionProvider connectionProvider;
|
||||
if (pool.getType() == DISABLED) {
|
||||
connectionProvider = ConnectionProvider.newConnection();
|
||||
}
|
||||
else {
|
||||
// create either Fixed or Elastic pool
|
||||
ConnectionProvider.Builder builder = ConnectionProvider
|
||||
.builder(pool.getName());
|
||||
if (pool.getType() == FIXED) {
|
||||
builder.maxConnections(pool.getMaxConnections())
|
||||
.pendingAcquireMaxCount(-1).pendingAcquireTimeout(
|
||||
Duration.ofMillis(pool.getAcquireTimeout()));
|
||||
}
|
||||
else {
|
||||
// Elastic
|
||||
builder.maxConnections(Integer.MAX_VALUE)
|
||||
.pendingAcquireTimeout(Duration.ofMillis(0))
|
||||
.pendingAcquireMaxCount(-1);
|
||||
}
|
||||
|
||||
if (pool.getMaxIdleTime() != null) {
|
||||
builder.maxIdleTime(pool.getMaxIdleTime());
|
||||
}
|
||||
if (pool.getMaxLifeTime() != null) {
|
||||
builder.maxLifeTime(pool.getMaxLifeTime());
|
||||
}
|
||||
builder.evictInBackground(pool.getEvictionInterval());
|
||||
connectionProvider = builder.build();
|
||||
}
|
||||
return connectionProvider;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HttpClientProperties httpClientProperties() {
|
||||
return new HttpClientProperties();
|
||||
|
||||
@@ -211,6 +211,12 @@ public class HttpClientProperties {
|
||||
*/
|
||||
private Duration maxLifeTime = null;
|
||||
|
||||
/**
|
||||
* Perform regular eviction checks in the background at a specified interval.
|
||||
* Disabled by default ({@link Duration#ZERO})
|
||||
*/
|
||||
private Duration evictionInterval = Duration.ZERO;
|
||||
|
||||
public PoolType getType() {
|
||||
return type;
|
||||
}
|
||||
@@ -259,11 +265,19 @@ public class HttpClientProperties {
|
||||
this.maxLifeTime = maxLifeTime;
|
||||
}
|
||||
|
||||
public Duration getEvictionInterval() {
|
||||
return evictionInterval;
|
||||
}
|
||||
|
||||
public void setEvictionInterval(Duration evictionInterval) {
|
||||
this.evictionInterval = evictionInterval;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Pool{" + "type=" + type + ", name='" + name + '\'' + ", maxConnections=" + maxConnections
|
||||
+ ", acquireTimeout=" + acquireTimeout + ", maxIdleTime=" + maxIdleTime + ", maxLifeTime="
|
||||
+ maxLifeTime + '}';
|
||||
+ maxLifeTime + ", evictionInterval=" + evictionInterval + '}';
|
||||
}
|
||||
|
||||
public enum PoolType {
|
||||
|
||||
@@ -97,6 +97,7 @@ public class GatewayAutoConfigurationTests {
|
||||
.withPropertyValues("spring.cloud.gateway.httpclient.ssl.use-insecure-trust-manager=true",
|
||||
"spring.cloud.gateway.httpclient.connect-timeout=10",
|
||||
"spring.cloud.gateway.httpclient.response-timeout=10s",
|
||||
"spring.cloud.gateway.httpclient.pool.eviction-interval=10s",
|
||||
"spring.cloud.gateway.httpclient.pool.type=fixed",
|
||||
"spring.cloud.gateway.httpclient.compression=true",
|
||||
// greather than integer max value
|
||||
@@ -109,6 +110,7 @@ public class GatewayAutoConfigurationTests {
|
||||
HttpClientProperties properties = context.getBean(HttpClientProperties.class);
|
||||
assertThat(properties.getMaxInitialLineLength().toBytes()).isLessThanOrEqualTo(Integer.MAX_VALUE);
|
||||
assertThat(properties.isCompression()).isEqualTo(true);
|
||||
assertThat(properties.getPool().getEvictionInterval()).hasSeconds(10);
|
||||
/*
|
||||
* FIXME: 2.1.0 HttpClientOptions options = httpClient.options();
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user