From 01f314a4756ed55e2e4602d9e7dca34e8893b42b Mon Sep 17 00:00:00 2001 From: Tim Peeters Date: Fri, 24 Jan 2020 13:41:25 +0100 Subject: [PATCH 1/2] Adds configurable maxLifeTime in HttpClientProperties. fixes gh-1542 --- docs/src/main/asciidoc/_configprops.adoc | 1 + .../config/GatewayAutoConfiguration.java | 4 ++-- .../gateway/config/HttpClientProperties.java | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/src/main/asciidoc/_configprops.adoc b/docs/src/main/asciidoc/_configprops.adoc index 4578dd24..0c8ee8cc 100644 --- a/docs/src/main/asciidoc/_configprops.adoc +++ b/docs/src/main/asciidoc/_configprops.adoc @@ -30,6 +30,7 @@ |spring.cloud.gateway.httpclient.pool.acquire-timeout | | Only for type FIXED, the maximum time in millis to wait for aquiring. |spring.cloud.gateway.httpclient.pool.max-connections | | Only for type FIXED, the maximum number of connections before starting pending acquisition on existing ones. |spring.cloud.gateway.httpclient.pool.max-idle-time | | Time in millis after which the channel will be closed. If NULL, there is no max idle time. +|spring.cloud.gateway.httpclient.pool.max-life-time | | Duration after which the channel will be closed. If NULL, there is no max life time. |spring.cloud.gateway.httpclient.pool.name | proxy | The channel pool map name, defaults to proxy. |spring.cloud.gateway.httpclient.pool.type | | Type of pool for HttpClient to use, defaults to ELASTIC. |spring.cloud.gateway.httpclient.proxy.host | | Hostname for proxy configuration of Netty HttpClient. diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java index 00ce39cf..dc27a9bb 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java @@ -591,11 +591,11 @@ public class GatewayAutoConfiguration { else if (pool.getType() == FIXED) { connectionProvider = ConnectionProvider.fixed(pool.getName(), pool.getMaxConnections(), pool.getAcquireTimeout(), - pool.getMaxIdleTime()); + pool.getMaxIdleTime(), pool.getMaxLifeTime()); } else { connectionProvider = ConnectionProvider.elastic(pool.getName(), - pool.getMaxIdleTime()); + pool.getMaxIdleTime(), pool.getMaxLifeTime()); } HttpClient httpClient = HttpClient.create(connectionProvider) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java index 1b0b61c7..0ecfdc29 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java @@ -178,6 +178,12 @@ public class HttpClientProperties { */ private Duration maxIdleTime = null; + /** + * Duration after which the channel will be closed. If NULL, there is no max life + * time. + */ + private Duration maxLifeTime = null; + public PoolType getType() { return type; } @@ -218,11 +224,20 @@ public class HttpClientProperties { this.maxIdleTime = maxIdleTime; } + public Duration getMaxLifeTime() { + return maxLifeTime; + } + + public void setMaxLifeTime(Duration maxLifeTime) { + this.maxLifeTime = maxLifeTime; + } + @Override public String toString() { return "Pool{" + "type=" + type + ", name='" + name + '\'' + ", maxConnections=" + maxConnections + ", acquireTimeout=" - + acquireTimeout + '}'; + + acquireTimeout + ", maxIdleTime=" + maxIdleTime + ", maxLifeTime=" + + maxLifeTime + '}'; } public enum PoolType { From 4fbadeec7eb711ed685f9236310a162f5d88bbf5 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 27 Jan 2020 11:59:50 -0500 Subject: [PATCH 2/2] Implements missing methods --- .../cloud/gateway/support/DefaultClientResponse.java | 5 +++++ .../cloud/gateway/support/DefaultServerRequest.java | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/DefaultClientResponse.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/DefaultClientResponse.java index c215e9d2..6257e58e 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/DefaultClientResponse.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/DefaultClientResponse.java @@ -136,6 +136,11 @@ public class DefaultClientResponse implements ClientResponse { throw new UnsupportedOperationException(); } + @Override + public String logPrefix() { + return ""; + } + @SuppressWarnings("unchecked") private Mono consumeAndCancel() { return (Mono) this.response.getBody().map(buffer -> { diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/DefaultServerRequest.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/DefaultServerRequest.java index 22bd0c3d..4369bf16 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/DefaultServerRequest.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/DefaultServerRequest.java @@ -127,7 +127,12 @@ public class DefaultServerRequest implements ServerRequest { @Override public Optional remoteAddress() { - return Optional.of(request().getRemoteAddress()); + return Optional.ofNullable(request().getRemoteAddress()); + } + + @Override + public Optional localAddress() { + return Optional.ofNullable(request().getLocalAddress()); } @Override