Skip to content

Commit dee63d4

Browse files
committed
fix: OkHttpClientFactory.additionalConfig can be used to override the default OkHttp Dispatcher
Signed-off-by: Marc Nuri <[email protected]>
1 parent 5833cc1 commit dee63d4

File tree

4 files changed

+56
-17
lines changed

4 files changed

+56
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* Fix #4832: NO_PROXY can match cidr with bit suffix <10
99
* Fix #4851: adding buffer cloning to ensure buffers cannot be modified after sending
1010
* Fix #4794: improving the semantics of manually calling informer stop
11+
* Fix #4797: OkHttpClientFactory.additionalConfig can be used to override the default OkHttp Dispatcher
1112
* Fix #4798: fix leader election release on cancel
1213
* Fix #4815: (java-generator) create target download directory if it doesn't exist
1314
* Fix #4846: allowed for pod read / copy operations to distinguish when the target doesn't exist

httpclient-okhttp/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientBuilderImpl.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.fabric8.kubernetes.client.http.StandardHttpClientBuilder;
2020
import okhttp3.Authenticator;
2121
import okhttp3.ConnectionSpec;
22-
import okhttp3.Dispatcher;
2322
import okhttp3.Interceptor;
2423
import okhttp3.OkHttpClient;
2524
import okhttp3.Protocol;
@@ -36,7 +35,7 @@
3635
class OkHttpClientBuilderImpl
3736
extends StandardHttpClientBuilder<OkHttpClientImpl, OkHttpClientFactory, OkHttpClientBuilderImpl> {
3837

39-
private okhttp3.OkHttpClient.Builder builder;
38+
private final okhttp3.OkHttpClient.Builder builder;
4039

4140
public OkHttpClientBuilderImpl(OkHttpClientFactory clientFactory, okhttp3.OkHttpClient.Builder builder) {
4241
super(clientFactory);
@@ -81,8 +80,7 @@ public OkHttpClientImpl initialBuild(okhttp3.OkHttpClient.Builder builder) {
8180
}
8281
if (tlsVersions != null) {
8382
ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
84-
.tlsVersions(Arrays.asList(tlsVersions)
85-
.stream()
83+
.tlsVersions(Arrays.stream(tlsVersions)
8684
.map(tls -> okhttp3.TlsVersion.valueOf(tls.name()))
8785
.toArray(okhttp3.TlsVersion[]::new))
8886
.build();
@@ -91,14 +89,6 @@ public OkHttpClientImpl initialBuild(okhttp3.OkHttpClient.Builder builder) {
9189
if (preferHttp11) {
9290
builder.protocols(Collections.singletonList(Protocol.HTTP_1_1));
9391
}
94-
Dispatcher dispatcher = new Dispatcher();
95-
// websockets and long running http requests count against this and eventually starve
96-
// the work that can be done
97-
dispatcher.setMaxRequests(Integer.MAX_VALUE);
98-
// long running http requests count against this and eventually exhaust
99-
// the work that can be done
100-
dispatcher.setMaxRequestsPerHost(Integer.MAX_VALUE);
101-
builder.dispatcher(dispatcher);
10292
return derivedBuild(builder);
10393
}
10494

httpclient-okhttp/src/main/java/io/fabric8/kubernetes/client/okhttp/OkHttpClientFactory.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.fabric8.kubernetes.client.KubernetesClientException;
2121
import io.fabric8.kubernetes.client.http.HttpClient;
2222
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
23+
import okhttp3.Dispatcher;
2324
import okhttp3.OkHttpClient;
2425
import okhttp3.logging.HttpLoggingInterceptor;
2526
import org.slf4j.Logger;
@@ -38,7 +39,7 @@ public boolean isDefault() {
3839
* Subclasses may use this to apply a base configuration to the builder
3940
*/
4041
protected OkHttpClient.Builder newOkHttpClientBuilder() {
41-
return new OkHttpClient.Builder();
42+
return new OkHttpClient.Builder().dispatcher(initDispatcher());
4243
}
4344

4445
/**
@@ -108,4 +109,15 @@ protected boolean shouldDisableHttp2() {
108109
return System.getProperty("java.version", "").startsWith("1.8");
109110
}
110111

112+
protected Dispatcher initDispatcher() {
113+
Dispatcher dispatcher = new Dispatcher();
114+
// websockets and long-running http requests count against this and eventually starve
115+
// the work that can be done
116+
dispatcher.setMaxRequests(Integer.MAX_VALUE);
117+
// long-running http requests count against this and eventually exhaust
118+
// the work that can be done
119+
dispatcher.setMaxRequestsPerHost(Integer.MAX_VALUE);
120+
return dispatcher;
121+
}
122+
111123
}
Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@
1818

1919
import io.fabric8.kubernetes.client.Config;
2020
import io.fabric8.kubernetes.client.ConfigBuilder;
21+
import okhttp3.Dispatcher;
22+
import okhttp3.OkHttpClient;
2123
import org.junit.jupiter.api.Test;
2224

25+
import static org.assertj.core.api.Assertions.assertThat;
2326
import static org.junit.jupiter.api.Assertions.assertEquals;
2427

2528
class OkHttpClientFactoryTest {
2629

2730
@Test
2831
void shouldNotRespectMaxRequests() {
29-
OkHttpClientImpl client = new OkHttpClientFactory().newBuilder(new ConfigBuilder().build()).build();
32+
OkHttpClientImpl client = new OkHttpClientFactory().newBuilder(Config.empty()).build();
3033

3134
assertEquals(Integer.MAX_VALUE, client.getOkHttpClient().dispatcher().getMaxRequests());
3235

33-
Config config = new ConfigBuilder()
36+
Config config = new ConfigBuilder(Config.empty())
3437
.withMaxConcurrentRequests(120)
3538
.build();
3639

@@ -40,11 +43,11 @@ void shouldNotRespectMaxRequests() {
4043

4144
@Test
4245
void shouldNotRespectMaxRequestsPerHost() {
43-
OkHttpClientImpl client = new OkHttpClientFactory().newBuilder(new ConfigBuilder().build()).build();
46+
OkHttpClientImpl client = new OkHttpClientFactory().newBuilder(Config.empty()).build();
4447

4548
assertEquals(Integer.MAX_VALUE, client.getOkHttpClient().dispatcher().getMaxRequestsPerHost());
4649

47-
Config config = new ConfigBuilder()
50+
Config config = new ConfigBuilder(Config.empty())
4851
.withMaxConcurrentRequestsPerHost(20)
4952
.build();
5053

@@ -53,4 +56,37 @@ void shouldNotRespectMaxRequestsPerHost() {
5356
assertEquals(Integer.MAX_VALUE, client.getOkHttpClient().dispatcher().getMaxRequestsPerHost());
5457
}
5558

59+
@Test
60+
void additionalConfigCanOverrideDispatcher() {
61+
final Dispatcher dispatcher = new Dispatcher();
62+
OkHttpClientImpl client = new OkHttpClientFactory() {
63+
@Override
64+
protected void additionalConfig(OkHttpClient.Builder builder) {
65+
dispatcher.setMaxRequests(1337);
66+
builder.dispatcher(dispatcher);
67+
}
68+
}
69+
.newBuilder(Config.empty())
70+
.build();
71+
assertThat(client.getOkHttpClient().dispatcher())
72+
.isSameAs(dispatcher)
73+
.hasFieldOrPropertyWithValue("maxRequests", 1337);
74+
}
75+
76+
@Test
77+
void initDispatcherCanOverrideDispatcher() {
78+
OkHttpClientImpl client = new OkHttpClientFactory() {
79+
@Override
80+
protected Dispatcher initDispatcher() {
81+
final Dispatcher dispatcher = super.initDispatcher();
82+
dispatcher.setMaxRequests(1337);
83+
return dispatcher;
84+
}
85+
}
86+
.newBuilder(Config.empty())
87+
.build();
88+
assertThat(client.getOkHttpClient().dispatcher())
89+
.hasFieldOrPropertyWithValue("maxRequests", 1337);
90+
}
91+
5692
}

0 commit comments

Comments
 (0)