|
1 | 1 | package io.quarkus.mongodb.runtime.dns; |
2 | 2 |
|
3 | 3 | import static java.lang.String.format; |
| 4 | +import static org.eclipse.microprofile.config.ConfigProvider.getConfig; |
4 | 5 |
|
5 | 6 | import java.io.IOException; |
6 | 7 | import java.nio.file.Files; |
|
17 | 18 | import java.util.stream.Collectors; |
18 | 19 | import java.util.stream.Stream; |
19 | 20 |
|
20 | | -import org.eclipse.microprofile.config.Config; |
21 | | -import org.eclipse.microprofile.config.ConfigProvider; |
22 | 21 | import org.jboss.logging.Logger; |
23 | 22 |
|
24 | 23 | import com.mongodb.MongoConfigurationException; |
|
27 | 26 |
|
28 | 27 | import io.quarkus.mongodb.runtime.MongodbConfig; |
29 | 28 | import io.quarkus.runtime.annotations.RegisterForReflection; |
| 29 | +import io.smallrye.config.SmallRyeConfig; |
30 | 30 | import io.smallrye.mutiny.Uni; |
31 | 31 | import io.vertx.core.dns.DnsClientOptions; |
32 | 32 | import io.vertx.mutiny.core.Vertx; |
33 | 33 | import io.vertx.mutiny.core.dns.SrvRecord; |
34 | 34 |
|
35 | 35 | @RegisterForReflection |
36 | 36 | public class MongoDnsClient implements DnsClient { |
37 | | - private static final String BASE_CONFIG_NAME = "quarkus." + MongodbConfig.CONFIG_NAME + "."; |
38 | | - |
39 | | - public static final String DNS_LOOKUP_TIMEOUT = BASE_CONFIG_NAME + MongodbConfig.DNS_LOOKUP_TIMEOUT; |
40 | | - public static final String NATIVE_DNS_LOOKUP_TIMEOUT = BASE_CONFIG_NAME + MongodbConfig.NATIVE_DNS_LOOKUP_TIMEOUT; |
41 | | - |
42 | | - public static final String DNS_LOG_ACTIVITY = BASE_CONFIG_NAME + MongodbConfig.DNS_LOG_ACTIVITY; |
43 | | - public static final String NATIVE_DNS_LOG_ACTIVITY = BASE_CONFIG_NAME + MongodbConfig.NATIVE_DNS_LOG_ACTIVITY; |
44 | | - |
45 | | - public static final String DNS_SERVER = BASE_CONFIG_NAME + MongodbConfig.DNS_SERVER_HOST; |
46 | | - public static final String NATIVE_DNS_SERVER = BASE_CONFIG_NAME + MongodbConfig.NATIVE_DNS_SERVER_HOST; |
47 | | - public static final String DNS_SERVER_PORT = BASE_CONFIG_NAME + MongodbConfig.DNS_SERVER_PORT; |
48 | | - public static final String NATIVE_DNS_SERVER_PORT = BASE_CONFIG_NAME + MongodbConfig.NATIVE_DNS_SERVER_PORT; |
49 | | - |
50 | | - private final Config config = ConfigProvider.getConfig(); |
51 | | - |
52 | | - private final io.vertx.mutiny.core.dns.DnsClient dnsClient; |
| 37 | + private static final Logger log = Logger.getLogger(MongoDnsClient.class); |
53 | 38 |
|
54 | 39 | // the static fields are used in order to hold DNS resolution result that has been performed on the main thread |
55 | 40 | // at application startup |
56 | 41 | // the reason we need this is to ensure that no blocking of event loop threads will occur due to DNS resolution |
57 | 42 | private static final Map<String, List<SrvRecord>> SRV_CACHE = new ConcurrentHashMap<>(); |
58 | 43 | private static final Map<String, List<String>> TXT_CACHE = new ConcurrentHashMap<>(); |
59 | 44 |
|
60 | | - private static final Logger log = Logger.getLogger(MongoDnsClient.class); |
| 45 | + private final io.vertx.mutiny.core.dns.DnsClient dnsClient; |
| 46 | + private final MongodbConfig mongoConfig; |
61 | 47 |
|
62 | 48 | MongoDnsClient(io.vertx.core.Vertx vertx) { |
| 49 | + this.mongoConfig = getConfig().unwrap(SmallRyeConfig.class).getConfigMapping(MongodbConfig.class); |
63 | 50 | Vertx mutinyVertx = new io.vertx.mutiny.core.Vertx(vertx); |
64 | 51 |
|
65 | | - boolean activity = config.getOptionalValue(DNS_LOG_ACTIVITY, Boolean.class).orElse(false); |
66 | | - |
67 | 52 | // If the server is not set, we attempt to read the /etc/resolv.conf. If it does not exist, we use the default |
68 | 53 | // configuration. |
69 | | - String server = config.getOptionalValue(DNS_SERVER, String.class).orElseGet(() -> { |
| 54 | + String server = mongoConfig.dnsServer().orElseGet(() -> { |
70 | 55 | List<String> list = nameServers(); |
71 | 56 | if (!list.isEmpty()) { |
72 | 57 | return list.get(0); |
73 | 58 | } |
74 | 59 | return null; |
75 | 60 | }); |
76 | 61 | DnsClientOptions dnsClientOptions = new DnsClientOptions() |
77 | | - .setLogActivity(activity); |
| 62 | + .setLogActivity(mongoConfig.dnsLookupLogActivity()); |
78 | 63 | if (server != null) { |
79 | 64 | dnsClientOptions.setHost(server); |
80 | | - if (config.getOptionalValue(DNS_SERVER_PORT, Integer.class).isPresent()) { |
81 | | - dnsClientOptions.setPort(config.getOptionalValue(DNS_SERVER_PORT, Integer.class).orElseThrow()); |
| 65 | + if (mongoConfig.dnsServerPort().isPresent()) { |
| 66 | + dnsClientOptions.setPort(mongoConfig.dnsServerPort().orElseThrow()); |
82 | 67 | } |
83 | 68 | } |
84 | | - dnsClientOptions.setQueryTimeout(config.getValue(DNS_LOOKUP_TIMEOUT, Duration.class).toMillis()); |
| 69 | + dnsClientOptions.setQueryTimeout(mongoConfig.dnsLookupTimeout().toMillis()); |
85 | 70 |
|
86 | 71 | if (log.isDebugEnabled()) { |
87 | 72 | log.debugf("DNS client options: %s", dnsClientOptions.toJson()); |
@@ -128,8 +113,7 @@ public List<String> getResourceRecordData(String name, String type) throws DnsEx |
128 | 113 | */ |
129 | 114 | private List<String> resolveSrvRequest(final String srvHost) { |
130 | 115 | List<String> hosts = new ArrayList<>(); |
131 | | - Duration timeout = config.getOptionalValue(DNS_LOOKUP_TIMEOUT, Duration.class) |
132 | | - .orElse(Duration.ofSeconds(5)); |
| 116 | + Duration timeout = mongoConfig.dnsLookupTimeout(); |
133 | 117 |
|
134 | 118 | try { |
135 | 119 | List<SrvRecord> srvRecords; |
@@ -184,9 +168,7 @@ public List<String> resolveTxtRequest(final String host) { |
184 | 168 | return TXT_CACHE.get(host); |
185 | 169 | } |
186 | 170 | try { |
187 | | - Duration timeout = config.getOptionalValue(DNS_LOOKUP_TIMEOUT, Duration.class) |
188 | | - .orElse(Duration.ofSeconds(5)); |
189 | | - |
| 171 | + Duration timeout = mongoConfig.dnsLookupTimeout(); |
190 | 172 | return Uni.createFrom().<List<String>> deferred( |
191 | 173 | new Supplier<>() { |
192 | 174 | @Override |
|
0 commit comments