Skip to content

Commit fd3c6f5

Browse files
authored
Provides configuration for Netty's http client pool leasing strategy (#3575)
1 parent 95a8a55 commit fd3c6f5

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

docs/modules/ROOT/partials/_configprops.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
|spring.cloud.gateway.httpclient.pool.metrics | `+++false+++` | Enables channel pools metrics to be collected and registered in Micrometer. Disabled by default.
9191
|spring.cloud.gateway.httpclient.pool.name | `+++proxy+++` | The channel pool map name, defaults to proxy.
9292
|spring.cloud.gateway.httpclient.pool.type | `+++elastic+++` | Type of pool for HttpClient to use, defaults to ELASTIC.
93+
|spring.cloud.gateway.httpclient.pool.leasingStrategy | `+++fifo+++` | Configures the leasing strategy for the pool, defaults to FIFO.
9394
|spring.cloud.gateway.httpclient.proxy.host | | Hostname for proxy configuration of Netty HttpClient.
9495
|spring.cloud.gateway.httpclient.proxy.non-proxy-hosts-pattern | | Regular expression (Java) for a configured list of hosts. that should be reached directly, bypassing the proxy
9596
|spring.cloud.gateway.httpclient.proxy.password | | Password for proxy configuration of Netty HttpClient.

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/HttpClientFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.util.CollectionUtils;
3434
import org.springframework.util.StringUtils;
3535

36+
import static org.springframework.cloud.gateway.config.HttpClientProperties.Pool.LeasingStrategy.FIFO;
3637
import static org.springframework.cloud.gateway.config.HttpClientProperties.Pool.PoolType.DISABLED;
3738
import static org.springframework.cloud.gateway.config.HttpClientProperties.Pool.PoolType.FIXED;
3839

@@ -189,6 +190,16 @@ protected ConnectionProvider buildConnectionProvider(HttpClientProperties proper
189190
}
190191
builder.evictInBackground(pool.getEvictionInterval());
191192
builder.metrics(pool.isMetrics());
193+
194+
// Define the pool leasing strategy
195+
if (pool.getLeasingStrategy() == FIFO) {
196+
builder.fifo();
197+
}
198+
else {
199+
// LIFO
200+
builder.lifo();
201+
}
202+
192203
connectionProvider = builder.build();
193204
}
194205
return connectionProvider;

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/HttpClientProperties.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,11 @@ public static class Pool {
208208
*/
209209
private boolean metrics = false;
210210

211+
/**
212+
* Configures the leasing strategy for the pool, defaults to FIFO which is Netty's default.
213+
*/
214+
private LeasingStrategy leasingStrategy = LeasingStrategy.FIFO;
215+
211216
public PoolType getType() {
212217
return type;
213218
}
@@ -272,11 +277,20 @@ public void setMetrics(boolean metrics) {
272277
this.metrics = metrics;
273278
}
274279

280+
public LeasingStrategy getLeasingStrategy() {
281+
return leasingStrategy;
282+
}
283+
284+
public void setLeasingStrategy(LeasingStrategy leasingStrategy) {
285+
this.leasingStrategy = leasingStrategy;
286+
}
287+
275288
@Override
276289
public String toString() {
277290
return "Pool{" + "type=" + type + ", name='" + name + '\'' + ", maxConnections=" + maxConnections
278291
+ ", acquireTimeout=" + acquireTimeout + ", maxIdleTime=" + maxIdleTime + ", maxLifeTime="
279-
+ maxLifeTime + ", evictionInterval=" + evictionInterval + ", metrics=" + metrics + '}';
292+
+ maxLifeTime + ", evictionInterval=" + evictionInterval + ", metrics=" + metrics
293+
+ ", leasingStrategy=" + leasingStrategy + '}';
280294
}
281295

282296
public enum PoolType {
@@ -298,6 +312,20 @@ public enum PoolType {
298312

299313
}
300314

315+
public enum LeasingStrategy {
316+
317+
/**
318+
* FIFO leasing strategy.
319+
*/
320+
FIFO,
321+
322+
/**
323+
* LIFO leasing strategy.
324+
*/
325+
LIFO
326+
327+
}
328+
301329
}
302330

303331
public static class Proxy {

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/config/GatewayAutoConfigurationTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import org.springframework.cloud.gateway.actuate.GatewayControllerEndpoint;
5353
import org.springframework.cloud.gateway.actuate.GatewayLegacyControllerEndpoint;
5454
import org.springframework.cloud.gateway.config.GatewayAutoConfigurationTests.CustomHttpClientFactory.CustomSslConfigurer;
55+
import org.springframework.cloud.gateway.config.HttpClientProperties.Pool.LeasingStrategy;
5556
import org.springframework.cloud.gateway.filter.factory.TokenRelayGatewayFilterFactory;
5657
import org.springframework.cloud.gateway.filter.headers.GRPCRequestHeadersFilter;
5758
import org.springframework.cloud.gateway.filter.headers.GRPCResponseHeadersFilter;
@@ -119,6 +120,7 @@ public void nettyHttpClientConfigured() {
119120
"spring.cloud.gateway.httpclient.pool.eviction-interval=10s",
120121
"spring.cloud.gateway.httpclient.pool.type=fixed",
121122
"spring.cloud.gateway.httpclient.pool.metrics=true",
123+
"spring.cloud.gateway.httpclient.pool.leasing-strategy=lifo",
122124
"spring.cloud.gateway.httpclient.compression=true", "spring.cloud.gateway.httpclient.wiretap=true",
123125
// greater than integer max value
124126
"spring.cloud.gateway.httpclient.max-initial-line-length=2147483647",
@@ -133,6 +135,7 @@ public void nettyHttpClientConfigured() {
133135
assertThat(properties.isCompression()).isEqualTo(true);
134136
assertThat(properties.getPool().getEvictionInterval()).hasSeconds(10);
135137
assertThat(properties.getPool().isMetrics()).isEqualTo(true);
138+
assertThat(properties.getPool().getLeasingStrategy()).isEqualTo(LeasingStrategy.LIFO);
136139

137140
assertThat(httpClient.configuration().isAcceptGzip()).isTrue();
138141
assertThat(httpClient.configuration().loggingHandler()).isNotNull();

0 commit comments

Comments
 (0)