forked from spotify/dns-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDnsSrvResolvers.java
More file actions
153 lines (128 loc) · 5.53 KB
/
DnsSrvResolvers.java
File metadata and controls
153 lines (128 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (c) 2015 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 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.
*/
public final class DnsSrvResolvers {
private static final int DEFAULT_DNS_TIMEOUT_SECONDS = 5;
private static final int DEFAULT_RETENTION_DURATION_HOURS = 2;
public static DnsSrvResolverBuilder newBuilder() {
return new DnsSrvResolverBuilder();
}
public static final class DnsSrvResolverBuilder {
private final DnsReporter reporter;
private final boolean retainData;
private final boolean cacheLookups;
private final long dnsLookupTimeoutMillis;
private final long retentionDurationMillis;
private final List<String> servers;
private DnsSrvResolverBuilder() {
this(null,
false,
false,
SECONDS.toMillis(DEFAULT_DNS_TIMEOUT_SECONDS),
HOURS.toMillis(DEFAULT_RETENTION_DURATION_HOURS),
null);
}
private DnsSrvResolverBuilder(
DnsReporter reporter,
boolean retainData,
boolean cacheLookups,
long dnsLookupTimeoutMillis,
long retentionDurationMillis,
List<String> servers) {
this.reporter = reporter;
this.retainData = retainData;
this.cacheLookups = cacheLookups;
this.dnsLookupTimeoutMillis = dnsLookupTimeoutMillis;
this.retentionDurationMillis = retentionDurationMillis;
this.servers = servers;
}
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);
}
// Configure the Resolver to use our timeouts.
final Duration timeoutDuration = Duration.ofMillis(dnsLookupTimeoutMillis);
resolver.setTimeout(timeoutDuration);
LookupFactory lookupFactory = new SimpleLookupFactory(resolver);
if (cacheLookups) {
lookupFactory = new CachingLookupFactory(lookupFactory);
}
DnsSrvResolver result = new XBillDnsSrvResolver(lookupFactory);
if (reporter != null) {
result = new MeteredDnsSrvResolver(result, reporter);
}
if (retainData) {
result = new RetainingDnsSrvResolver(result, retentionDurationMillis);
}
return result;
}
public DnsSrvResolverBuilder metered(DnsReporter reporter) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis, servers);
}
public DnsSrvResolverBuilder retainingDataOnFailures(boolean retainData) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis, servers);
}
public DnsSrvResolverBuilder cachingLookups(boolean cacheLookups) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis, servers);
}
public DnsSrvResolverBuilder dnsLookupTimeoutMillis(long dnsLookupTimeoutMillis) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis, servers);
}
public DnsSrvResolverBuilder retentionDurationMillis(long retentionDurationMillis) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis, servers);
}
/**
* 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
* <a href="https://github.com/dnsjava/dnsjava/blob/master/org/xbill/DNS/ResolverConfig.java">
* ResolverConfig.java</a>
* @param servers the DNS servers to use
* @return this builder
*/
public DnsSrvResolverBuilder servers(List<String> servers) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis, servers);
}
}
private DnsSrvResolvers() {
// prevent instantiation
}
}