1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.resolver.dns;
17
18 import io.netty5.channel.EventLoop;
19
20 import java.net.InetSocketAddress;
21 import java.util.Comparator;
22 import java.util.List;
23 import java.util.concurrent.ConcurrentMap;
24
25 import static io.netty5.util.internal.ObjectUtil.checkPositive;
26 import static io.netty5.util.internal.ObjectUtil.checkPositiveOrZero;
27 import static java.util.Objects.requireNonNull;
28
29
30
31
32 public class DefaultAuthoritativeDnsServerCache implements AuthoritativeDnsServerCache {
33
34 private final int minTtl;
35 private final int maxTtl;
36 private final Comparator<InetSocketAddress> comparator;
37 private final Cache<InetSocketAddress> resolveCache = new Cache<>() {
38 @Override
39 protected boolean shouldReplaceAll(InetSocketAddress entry) {
40 return false;
41 }
42
43 @Override
44 protected boolean equals(InetSocketAddress entry, InetSocketAddress otherEntry) {
45 return entry.getHostString().equalsIgnoreCase(otherEntry.getHostString());
46 }
47
48 @Override
49 protected void sortEntries(String hostname, List<InetSocketAddress> entries) {
50 if (comparator != null) {
51 entries.sort(comparator);
52 }
53 }
54 };
55
56
57
58
59 public DefaultAuthoritativeDnsServerCache() {
60 this(0, Cache.MAX_SUPPORTED_TTL_SECS, null);
61 }
62
63
64
65
66
67
68
69
70
71 public DefaultAuthoritativeDnsServerCache(int minTtl, int maxTtl, Comparator<InetSocketAddress> comparator) {
72 this.minTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositiveOrZero(minTtl, "minTtl"));
73 this.maxTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositive(maxTtl, "maxTtl"));
74 if (minTtl > maxTtl) {
75 throw new IllegalArgumentException(
76 "minTtl: " + minTtl + ", maxTtl: " + maxTtl + " (expected: 0 <= minTtl <= maxTtl)");
77 }
78 this.comparator = comparator;
79 }
80
81 @Override
82 public DnsServerAddressStream get(String hostname) {
83 requireNonNull(hostname, "hostname");
84
85 List<? extends InetSocketAddress> addresses = resolveCache.get(hostname);
86 if (addresses == null || addresses.isEmpty()) {
87 return null;
88 }
89 return new SequentialDnsServerAddressStream(addresses, 0);
90 }
91
92 @Override
93 public void cache(String hostname, InetSocketAddress address, long originalTtl, EventLoop loop) {
94 requireNonNull(hostname, "hostname");
95 requireNonNull(address, "address");
96 requireNonNull(loop, "loop");
97
98 if (address.getHostString() == null) {
99
100
101 return;
102 }
103
104 resolveCache.cache(hostname, address, Math.max(minTtl, (int) Math.min(maxTtl, originalTtl)), loop);
105 }
106
107 @Override
108 public void clear() {
109 resolveCache.clear();
110 }
111
112 @Override
113 public boolean clear(String hostname) {
114 requireNonNull(hostname, "hostname");
115
116 return resolveCache.clear(hostname);
117 }
118
119 @Override
120 public String toString() {
121 return "DefaultAuthoritativeDnsServerCache(minTtl=" + minTtl + ", maxTtl=" + maxTtl + ", cached nameservers=" +
122 resolveCache.size() + ')';
123 }
124 }