From 69ce8a991825aabb3120071bd3fba32f6c3248a8 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Tue, 25 Nov 2025 12:31:07 -0500 Subject: [PATCH] Set max requests based on http client properties Fixes #1276 --- .../openfeign/FeignAutoConfiguration.java | 7 +++ .../openfeign/OkHttpMaxRequestsTests.java | 61 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/OkHttpMaxRequestsTests.java diff --git a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java index 651546513..66d504e62 100644 --- a/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java +++ b/spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java @@ -46,6 +46,7 @@ import feign.okhttp.OkHttpClient; import jakarta.annotation.PreDestroy; import okhttp3.ConnectionPool; +import okhttp3.Dispatcher; import okhttp3.Protocol; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -266,6 +267,8 @@ public okhttp3.OkHttpClient okHttpClient(okhttp3.OkHttpClient.Builder builder, C int connectTimeout = httpClientProperties.getConnectionTimeout(); boolean disableSslValidation = httpClientProperties.isDisableSslValidation(); Duration readTimeout = httpClientProperties.getOkHttp().getReadTimeout(); + int maxConnections = httpClientProperties.getMaxConnections(); + int maxConnectionsPerRoute = httpClientProperties.getMaxConnectionsPerRoute(); List protocols = httpClientProperties.getOkHttp() .getProtocols() .stream() @@ -274,11 +277,15 @@ public okhttp3.OkHttpClient okHttpClient(okhttp3.OkHttpClient.Builder builder, C if (disableSslValidation) { disableSsl(builder); } + Dispatcher dispatcher = new Dispatcher(); + dispatcher.setMaxRequests(maxConnections); + dispatcher.setMaxRequestsPerHost(maxConnectionsPerRoute); this.okHttpClient = builder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS) .followRedirects(followRedirects) .readTimeout(readTimeout) .connectionPool(connectionPool) .protocols(protocols) + .dispatcher(dispatcher) .build(); return this.okHttpClient; } diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/OkHttpMaxRequestsTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/OkHttpMaxRequestsTests.java new file mode 100644 index 000000000..d8f4e5c10 --- /dev/null +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/OkHttpMaxRequestsTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2013-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.openfeign; + +import okhttp3.OkHttpClient; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +class OkHttpMaxRequestsTests { + + private ConfigurableApplicationContext context; + + @BeforeEach + void setUp() { + this.context = new SpringApplicationBuilder() + .properties("spring.cloud.openfeign.okhttp.enabled=true", + "spring.cloud.openfeign.httpclient.hc5.enabled=false", + "spring.cloud.openfeign.httpclient.max-connections=101", + "spring.cloud.openfeign.httpclient.max-connections-per-route=51") + .web(WebApplicationType.NONE) + .sources(FeignAutoConfiguration.class) + .run(); + } + + @AfterEach + void tearDown() { + if (context != null) { + context.close(); + } + } + + @Test + void shouldConfigureDispatcherLimits() { + OkHttpClient httpClient = context.getBean(OkHttpClient.class); + + assertThat(httpClient.dispatcher().getMaxRequests()).isEqualTo(101); + assertThat(httpClient.dispatcher().getMaxRequestsPerHost()).isEqualTo(51); + } + +}