|
25 | 25 |
|
26 | 26 | public class VertxHttpClientFactory implements io.fabric8.kubernetes.client.http.HttpClient.Factory {
|
27 | 27 |
|
| 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 | + |
28 | 56 | private final Vertx vertx;
|
29 | 57 |
|
30 | 58 | 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; |
32 | 69 | }
|
33 | 70 |
|
34 | 71 | @Override
|
35 | 72 | public VertxHttpClientBuilder<VertxHttpClientFactory> newBuilder() {
|
36 | 73 | return new VertxHttpClientBuilder<>(this, vertx);
|
37 | 74 | }
|
38 | 75 |
|
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 |
| - |
61 | 76 | /**
|
62 | 77 | * Additional configuration to be applied to the options after the {@link Config} has been processed.
|
63 | 78 | */
|
|
0 commit comments