View Javadoc
1   /*
2    * Copyright 2018 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Default implementation of {@link AuthoritativeDnsServerCache}, backed by a {@link ConcurrentMap}.
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       * Create a cache that respects the TTL returned by the DNS server.
58       */
59      public DefaultAuthoritativeDnsServerCache() {
60          this(0, Cache.MAX_SUPPORTED_TTL_SECS, null);
61      }
62  
63      /**
64       * Create a cache.
65       *
66       * @param minTtl the minimum TTL
67       * @param maxTtl the maximum TTL
68       * @param comparator the {@link Comparator} to order the {@link InetSocketAddress} for a hostname or {@code null}
69       *                   if insertion order should be used.
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              // We only cache addresses that have also a host string as we will need it later when trying to replace
100             // unresolved entries in the cache.
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 }