Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Protocol> protocols = httpClientProperties.getOkHttp()
.getProtocols()
.stream()
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}