diff --git a/pom.xml b/pom.xml
index 8bc0ff6..539a9a4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
com.spotify
dns
bundle
- 3.2.3-SNAPSHOT
+ 3.2.3-lookup-factory-SNAPSHOT
Spotify DNS wrapper library
A thin wrapper around dnsjava for some features related to SRV lookups.
diff --git a/src/main/java/com/spotify/dns/DnsSrvResolvers.java b/src/main/java/com/spotify/dns/DnsSrvResolvers.java
index c23f77a..931f1be 100644
--- a/src/main/java/com/spotify/dns/DnsSrvResolvers.java
+++ b/src/main/java/com/spotify/dns/DnsSrvResolvers.java
@@ -16,24 +16,27 @@
package com.spotify.dns;
-import static java.util.concurrent.TimeUnit.HOURS;
-import static java.util.concurrent.TimeUnit.SECONDS;
-
import com.spotify.dns.statistics.DnsReporter;
+import org.xbill.DNS.ExtendedResolver;
+import org.xbill.DNS.Resolver;
+
import java.net.UnknownHostException;
import java.time.Duration;
import java.util.List;
-import org.xbill.DNS.ExtendedResolver;
-import org.xbill.DNS.Resolver;
-/**
- * Provides builders for configuring and instantiating {@link DnsSrvResolver}s.
- */
+import static java.util.concurrent.TimeUnit.HOURS;
+import static java.util.concurrent.TimeUnit.SECONDS;
+
+/** Provides builders for configuring and instantiating {@link DnsSrvResolver}s. */
public final class DnsSrvResolvers {
private static final int DEFAULT_DNS_TIMEOUT_SECONDS = 5;
private static final int DEFAULT_RETENTION_DURATION_HOURS = 2;
+ private DnsSrvResolvers() {
+ // prevent instantiation
+ }
+
public static DnsSrvResolverBuilder newBuilder() {
return new DnsSrvResolverBuilder();
}
@@ -46,14 +49,17 @@ public static final class DnsSrvResolverBuilder {
private final long dnsLookupTimeoutMillis;
private final long retentionDurationMillis;
private final List servers;
+ private final Resolver resolver;
private DnsSrvResolverBuilder() {
- this(null,
- false,
- false,
- SECONDS.toMillis(DEFAULT_DNS_TIMEOUT_SECONDS),
- HOURS.toMillis(DEFAULT_RETENTION_DURATION_HOURS),
- null);
+ this(
+ null,
+ false,
+ false,
+ SECONDS.toMillis(DEFAULT_DNS_TIMEOUT_SECONDS),
+ HOURS.toMillis(DEFAULT_RETENTION_DURATION_HOURS),
+ null,
+ null);
}
private DnsSrvResolverBuilder(
@@ -62,26 +68,34 @@ private DnsSrvResolverBuilder(
boolean cacheLookups,
long dnsLookupTimeoutMillis,
long retentionDurationMillis,
- List servers) {
+ List servers,
+ Resolver resolver) {
this.reporter = reporter;
this.retainData = retainData;
this.cacheLookups = cacheLookups;
this.dnsLookupTimeoutMillis = dnsLookupTimeoutMillis;
this.retentionDurationMillis = retentionDurationMillis;
this.servers = servers;
+ this.resolver = resolver;
}
public DnsSrvResolver build() {
Resolver resolver;
- try {
- // If the user specified DNS servers, create a new ExtendedResolver which uses them.
- // Otherwise, use the default constructor. That will use the servers in ResolverConfig,
- // or if that's empty, localhost.
- resolver = servers == null ?
- new ExtendedResolver() :
- new ExtendedResolver(servers.toArray(new String[servers.size()]));
- } catch (UnknownHostException e) {
- throw new RuntimeException(e);
+
+ if(this.resolver != null) {
+ resolver = this.resolver;
+ } else {
+ try {
+ // If the user specified DNS servers, create a new ExtendedResolver which uses them.
+ // Otherwise, use the default constructor. That will use the servers in ResolverConfig,
+ // or if that's empty, localhost.
+ resolver =
+ servers == null
+ ? new ExtendedResolver()
+ : new ExtendedResolver(servers.toArray(new String[servers.size()]));
+ } catch (UnknownHostException e) {
+ throw new RuntimeException(e);
+ }
}
// Configure the Resolver to use our timeouts.
@@ -108,46 +122,97 @@ public DnsSrvResolver build() {
}
public DnsSrvResolverBuilder metered(DnsReporter reporter) {
- return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
- retentionDurationMillis, servers);
+ return new DnsSrvResolverBuilder(
+ reporter,
+ retainData,
+ cacheLookups,
+ dnsLookupTimeoutMillis,
+ retentionDurationMillis,
+ servers,
+ resolver);
}
public DnsSrvResolverBuilder retainingDataOnFailures(boolean retainData) {
- return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
- retentionDurationMillis, servers);
+ return new DnsSrvResolverBuilder(
+ reporter,
+ retainData,
+ cacheLookups,
+ dnsLookupTimeoutMillis,
+ retentionDurationMillis,
+ servers,
+ resolver);
}
public DnsSrvResolverBuilder cachingLookups(boolean cacheLookups) {
- return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
- retentionDurationMillis, servers);
+ return new DnsSrvResolverBuilder(
+ reporter,
+ retainData,
+ cacheLookups,
+ dnsLookupTimeoutMillis,
+ retentionDurationMillis,
+ servers,
+ resolver);
}
public DnsSrvResolverBuilder dnsLookupTimeoutMillis(long dnsLookupTimeoutMillis) {
- return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
- retentionDurationMillis, servers);
+ return new DnsSrvResolverBuilder(
+ reporter,
+ retainData,
+ cacheLookups,
+ dnsLookupTimeoutMillis,
+ retentionDurationMillis,
+ servers,
+ resolver);
}
public DnsSrvResolverBuilder retentionDurationMillis(long retentionDurationMillis) {
- return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
- retentionDurationMillis, servers);
+ return new DnsSrvResolverBuilder(
+ reporter,
+ retainData,
+ cacheLookups,
+ dnsLookupTimeoutMillis,
+ retentionDurationMillis,
+ servers,
+ resolver);
}
/**
* Allows the user to specify which DNS servers should be used to perform DNS lookups. Servers
* can be specified using either hostname or IP address. If not specified, the underlying DNS
- * library will determine which servers to use according to the steps documented in
- *
+ * library will determine which servers to use according to the steps documented in
* ResolverConfig.java
+ *
* @param servers the DNS servers to use
* @return this builder
*/
public DnsSrvResolverBuilder servers(List servers) {
- return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
- retentionDurationMillis, servers);
+ return new DnsSrvResolverBuilder(
+ reporter,
+ retainData,
+ cacheLookups,
+ dnsLookupTimeoutMillis,
+ retentionDurationMillis,
+ servers,
+ resolver);
}
- }
- private DnsSrvResolvers() {
- // prevent instantiation
+ /**
+ * Allows the user to specify a custom resolver to perform the actual DNS lookup, useful for
+ * things like testing & custom protocols
+ *
+ * @param resolver
+ * @return
+ */
+ public DnsSrvResolverBuilder resolver(Resolver resolver) {
+ return new DnsSrvResolverBuilder(
+ reporter,
+ retainData,
+ cacheLookups,
+ dnsLookupTimeoutMillis,
+ retentionDurationMillis,
+ servers,
+ resolver);
+ }
}
}