Skip to content

Commit 03439f2

Browse files
authored
fix(vertx): VertxHttpClientFactory reuses the same Vertx instance for each VertxHttpClient instance
Signed-off-by: Marc Nuri <[email protected]>
1 parent a09d1c1 commit 03439f2

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212

1313
#### _**Note**_: Breaking changes
1414

15+
### 7.0.1 (TBD)
16+
17+
#### Bugs
18+
19+
* Fix #6709: VertxHttpClientFactory reuses the same Vertx instance for each VertxHttpClient instance
1520

1621
### 7.0.0 (2024-12-03)
1722

httpclient-vertx/src/main/java/io/fabric8/kubernetes/client/vertx/VertxHttpClientFactory.java

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,54 @@
2525

2626
public class VertxHttpClientFactory implements io.fabric8.kubernetes.client.http.HttpClient.Factory {
2727

28+
private static final class VertxHolder {
29+
30+
private static final Vertx INSTANCE = createVertxInstance();
31+
32+
private static synchronized Vertx createVertxInstance() {
33+
// We must disable the async DNS resolver as it can cause issues when resolving the Vault instance.
34+
// This is done using the DISABLE_DNS_RESOLVER_PROP_NAME system property.
35+
// The DNS resolver used by vert.x is configured during the (synchronous) initialization.
36+
// So, we just need to disable the async resolver around the Vert.x instance creation.
37+
final String originalValue = System.getProperty(DISABLE_DNS_RESOLVER_PROP_NAME);
38+
Vertx vertx;
39+
try {
40+
System.setProperty(DISABLE_DNS_RESOLVER_PROP_NAME, "true");
41+
vertx = Vertx.vertx(new VertxOptions()
42+
.setFileSystemOptions(new FileSystemOptions().setFileCachingEnabled(false).setClassPathResolvingEnabled(false))
43+
.setUseDaemonThread(true));
44+
} finally {
45+
// Restore the original value
46+
if (originalValue == null) {
47+
System.clearProperty(DISABLE_DNS_RESOLVER_PROP_NAME);
48+
} else {
49+
System.setProperty(DISABLE_DNS_RESOLVER_PROP_NAME, originalValue);
50+
}
51+
}
52+
return vertx;
53+
}
54+
}
55+
2856
private final Vertx vertx;
2957

3058
public VertxHttpClientFactory() {
31-
this.vertx = createVertxInstance();
59+
this(VertxHolder.INSTANCE);
60+
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
61+
if (vertx != null) {
62+
vertx.close();
63+
}
64+
}));
65+
}
66+
67+
public VertxHttpClientFactory(Vertx vertx) {
68+
this.vertx = vertx;
3269
}
3370

3471
@Override
3572
public VertxHttpClientBuilder<VertxHttpClientFactory> newBuilder() {
3673
return new VertxHttpClientBuilder<>(this, vertx);
3774
}
3875

39-
private static synchronized Vertx createVertxInstance() {
40-
// We must disable the async DNS resolver as it can cause issues when resolving the Vault instance.
41-
// This is done using the DISABLE_DNS_RESOLVER_PROP_NAME system property.
42-
// The DNS resolver used by vert.x is configured during the (synchronous) initialization.
43-
// So, we just need to disable the async resolver around the Vert.x instance creation.
44-
final String originalValue = System.getProperty(DISABLE_DNS_RESOLVER_PROP_NAME);
45-
Vertx vertx;
46-
try {
47-
System.setProperty(DISABLE_DNS_RESOLVER_PROP_NAME, "true");
48-
vertx = Vertx.vertx(new VertxOptions()
49-
.setFileSystemOptions(new FileSystemOptions().setFileCachingEnabled(false).setClassPathResolvingEnabled(false)));
50-
} finally {
51-
// Restore the original value
52-
if (originalValue == null) {
53-
System.clearProperty(DISABLE_DNS_RESOLVER_PROP_NAME);
54-
} else {
55-
System.setProperty(DISABLE_DNS_RESOLVER_PROP_NAME, originalValue);
56-
}
57-
}
58-
return vertx;
59-
}
60-
6176
/**
6277
* Additional configuration to be applied to the options after the {@link Config} has been processed.
6378
*/

0 commit comments

Comments
 (0)