Skip to content

Commit

Permalink
Introduce retry into Mongo DNS resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Sep 12, 2024
1 parent 8f586aa commit 0b95574
Showing 1 changed file with 33 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -26,6 +27,7 @@

import io.quarkus.mongodb.runtime.MongodbConfig;
import io.quarkus.runtime.annotations.RegisterForReflection;
import io.smallrye.mutiny.Uni;
import io.vertx.core.dns.DnsClientOptions;
import io.vertx.mutiny.core.Vertx;
import io.vertx.mutiny.core.dns.SrvRecord;
Expand Down Expand Up @@ -130,12 +132,21 @@ private List<String> resolveSrvRequest(final String srvHost) {
if (SRV_CACHE.containsKey(srvHost)) {
srvRecords = SRV_CACHE.get(srvHost);
} else {
srvRecords = dnsClient.resolveSRV(srvHost).invoke(new Consumer<>() {
@Override
public void accept(List<SrvRecord> srvRecords) {
SRV_CACHE.put(srvHost, srvRecords);
}
}).await().atMost(timeout);
srvRecords = Uni.createFrom().<List<SrvRecord>> deferred(
new Supplier<>() {
@Override
public Uni<? extends List<SrvRecord>> get() {
return dnsClient.resolveSRV(srvHost);
}
})
.onFailure().retry().withBackOff(Duration.ofSeconds(1)).atMost(3)
.invoke(new Consumer<>() {
@Override
public void accept(List<SrvRecord> srvRecords) {
SRV_CACHE.put(srvHost, srvRecords);
}
})
.await().atMost(timeout);
}

if (srvRecords.isEmpty()) {
Expand Down Expand Up @@ -167,12 +178,22 @@ public List<String> resolveTxtRequest(final String host) {
try {
Duration timeout = config.getOptionalValue(DNS_LOOKUP_TIMEOUT, Duration.class)
.orElse(Duration.ofSeconds(5));
return dnsClient.resolveTXT(host).invoke(new Consumer<>() {
@Override
public void accept(List<String> strings) {
TXT_CACHE.put(host, strings);
}
}).await().atMost(timeout);

return Uni.createFrom().<List<String>> deferred(
new Supplier<>() {
@Override
public Uni<? extends List<String>> get() {
return dnsClient.resolveTXT(host);
}
})
.onFailure().retry().withBackOff(Duration.ofSeconds(1)).atMost(3)
.invoke(new Consumer<>() {
@Override
public void accept(List<String> strings) {
TXT_CACHE.put(host, strings);
}
})
.await().atMost(timeout);
} catch (Throwable e) {
throw new MongoConfigurationException("Unable to look up TXT record for host " + host, e);
}
Expand Down

0 comments on commit 0b95574

Please sign in to comment.