forked from spotify/dns-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DnsSrvResolvers.java
127 lines (102 loc) · 4.55 KB
/
DnsSrvResolvers.java
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
/*
* 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 com.spotify.dns.statistics.DnsReporter;
import org.xbill.DNS.Lookup;
import static com.google.common.primitives.Ints.checkedCast;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
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;
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 DnsSrvResolverBuilder() {
this(null,
false,
false,
SECONDS.toMillis(DEFAULT_DNS_TIMEOUT_SECONDS),
HOURS.toMillis(DEFAULT_RETENTION_DURATION_HOURS));
}
private DnsSrvResolverBuilder(
DnsReporter reporter,
boolean retainData,
boolean cacheLookups,
long dnsLookupTimeoutMillis,
long retentionDurationMillis) {
this.reporter = reporter;
this.retainData = retainData;
this.cacheLookups = cacheLookups;
this.dnsLookupTimeoutMillis = dnsLookupTimeoutMillis;
this.retentionDurationMillis = retentionDurationMillis;
}
public DnsSrvResolver build() {
// NOTE: this sucks, but is the only reasonably sane way to set a timeout in dnsjava...
// the effect of doing this is to set a global timeout for all Lookup instances - except
// those that potentially get a new Resolver assigned via the setResolver method... Since
// Lookup instances are mostly encapsulated in this library, we should be fine.
int timeoutSecs = checkedCast(MILLISECONDS.toSeconds(dnsLookupTimeoutMillis));
int millisRemainder = checkedCast(dnsLookupTimeoutMillis - SECONDS.toMillis(timeoutSecs));
Lookup.getDefaultResolver().setTimeout(timeoutSecs, millisRemainder);
LookupFactory lookupFactory = new SimpleLookupFactory();
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);
}
public DnsSrvResolverBuilder retainingDataOnFailures(boolean retainData) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis);
}
public DnsSrvResolverBuilder cachingLookups(boolean cacheLookups) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis);
}
public DnsSrvResolverBuilder dnsLookupTimeoutMillis(long dnsLookupTimeoutMillis) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis);
}
public DnsSrvResolverBuilder retentionDurationMillis(long retentionDurationMillis) {
return new DnsSrvResolverBuilder(reporter, retainData, cacheLookups, dnsLookupTimeoutMillis,
retentionDurationMillis);
}
}
private DnsSrvResolvers() {
// prevent instantiation
}
}