Skip to content

Commit 7b622b9

Browse files
committed
Merge branch '4.2.x' into 4.3.x
2 parents b8c8aa2 + eb6cb91 commit 7b622b9

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

spring-cloud-openfeign-core/src/main/java/org/springframework/cloud/openfeign/FeignAutoConfiguration.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import feign.okhttp.OkHttpClient;
4747
import jakarta.annotation.PreDestroy;
4848
import okhttp3.ConnectionPool;
49+
import okhttp3.Dispatcher;
4950
import okhttp3.Protocol;
5051
import org.apache.commons.logging.Log;
5152
import org.apache.commons.logging.LogFactory;
@@ -266,6 +267,8 @@ public okhttp3.OkHttpClient okHttpClient(okhttp3.OkHttpClient.Builder builder, C
266267
int connectTimeout = httpClientProperties.getConnectionTimeout();
267268
boolean disableSslValidation = httpClientProperties.isDisableSslValidation();
268269
Duration readTimeout = httpClientProperties.getOkHttp().getReadTimeout();
270+
int maxConnections = httpClientProperties.getMaxConnections();
271+
int maxConnectionsPerRoute = httpClientProperties.getMaxConnectionsPerRoute();
269272
List<Protocol> protocols = httpClientProperties.getOkHttp()
270273
.getProtocols()
271274
.stream()
@@ -274,11 +277,15 @@ public okhttp3.OkHttpClient okHttpClient(okhttp3.OkHttpClient.Builder builder, C
274277
if (disableSslValidation) {
275278
disableSsl(builder);
276279
}
280+
Dispatcher dispatcher = new Dispatcher();
281+
dispatcher.setMaxRequests(maxConnections);
282+
dispatcher.setMaxRequestsPerHost(maxConnectionsPerRoute);
277283
this.okHttpClient = builder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS)
278284
.followRedirects(followRedirects)
279285
.readTimeout(readTimeout)
280286
.connectionPool(connectionPool)
281287
.protocols(protocols)
288+
.dispatcher(dispatcher)
282289
.build();
283290
return this.okHttpClient;
284291
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2013-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.openfeign;
18+
19+
import okhttp3.OkHttpClient;
20+
import org.junit.jupiter.api.AfterEach;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.boot.WebApplicationType;
25+
import org.springframework.boot.builder.SpringApplicationBuilder;
26+
import org.springframework.context.ConfigurableApplicationContext;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
class OkHttpMaxRequestsTests {
31+
32+
private ConfigurableApplicationContext context;
33+
34+
@BeforeEach
35+
void setUp() {
36+
this.context = new SpringApplicationBuilder()
37+
.properties("spring.cloud.openfeign.okhttp.enabled=true",
38+
"spring.cloud.openfeign.httpclient.hc5.enabled=false",
39+
"spring.cloud.openfeign.httpclient.max-connections=101",
40+
"spring.cloud.openfeign.httpclient.max-connections-per-route=51")
41+
.web(WebApplicationType.NONE)
42+
.sources(FeignAutoConfiguration.class)
43+
.run();
44+
}
45+
46+
@AfterEach
47+
void tearDown() {
48+
if (context != null) {
49+
context.close();
50+
}
51+
}
52+
53+
@Test
54+
void shouldConfigureDispatcherLimits() {
55+
OkHttpClient httpClient = context.getBean(OkHttpClient.class);
56+
57+
assertThat(httpClient.dispatcher().getMaxRequests()).isEqualTo(101);
58+
assertThat(httpClient.dispatcher().getMaxRequestsPerHost()).isEqualTo(51);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)