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.netty.resolver.dns;
17  
18  import io.netty.channel.EventLoop;
19  
20  import java.net.InetSocketAddress;
21  import java.util.Collections;
22  import java.util.Comparator;
23  import java.util.List;
24  import java.util.concurrent.ConcurrentMap;
25  
26  import static io.netty.util.internal.ObjectUtil.checkNotNull;
27  import static io.netty.util.internal.ObjectUtil.checkPositive;
28  import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
29  
30  /**
31   * Default implementation of {@link AuthoritativeDnsServerCache}, backed by a {@link ConcurrentMap}.
32   */
33  public class DefaultAuthoritativeDnsServerCache implements AuthoritativeDnsServerCache {
34  
35      private final int minTtl;
36      private final int maxTtl;
37      private final Comparator<InetSocketAddress> comparator;
38      private final Cache<InetSocketAddress> resolveCache = new Cache<InetSocketAddress>() {
39          @Override
40          protected boolean shouldReplaceAll(InetSocketAddress entry) {
41              return false;
42          }
43  
44          @Override
45          protected boolean equals(InetSocketAddress entry, InetSocketAddress otherEntry) {
46              return entry.getHostString().equalsIgnoreCase(otherEntry.getHostString());
47          }
48  
49          @Override
50          protected void sortEntries(String hostname, List<InetSocketAddress> entries) {
51              if (comparator != null) {
52                  Collections.sort(entries, comparator);
53              }
54          }
55      };
56  
57      /**
58       * Create a cache that respects the TTL returned by the DNS server.
59       */
60      public DefaultAuthoritativeDnsServerCache() {
61          this(0, Cache.MAX_SUPPORTED_TTL_SECS, null);
62      }
63  
64      /**
65       * Create a cache.
66       *
67       * @param minTtl the minimum TTL
68       * @param maxTtl the maximum TTL
69       * @param comparator the {@link Comparator} to order the {@link InetSocketAddress} for a hostname or {@code null}
70       *                   if insertion order should be used.
71       */
72      public DefaultAuthoritativeDnsServerCache(int minTtl, int maxTtl, Comparator<InetSocketAddress> comparator) {
73          this.minTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositiveOrZero(minTtl, "minTtl"));
74          this.maxTtl = Math.min(Cache.MAX_SUPPORTED_TTL_SECS, checkPositive(maxTtl, "maxTtl"));
75          if (minTtl > maxTtl) {
76              throw new IllegalArgumentException(
77                      "minTtl: " + minTtl + ", maxTtl: " + maxTtl + " (expected: 0 <= minTtl <= maxTtl)");
78          }
79          this.comparator = comparator;
80      }
81  
82      @SuppressWarnings("unchecked")
83      @Override
84      public DnsServerAddressStream get(String hostname) {
85          checkNotNull(hostname, "hostname");
86  
87          List<? extends InetSocketAddress> addresses = resolveCache.get(hostname);
88          if (addresses == null || addresses.isEmpty()) {
89              return null;
90          }
91          return new SequentialDnsServerAddressStream(addresses, 0);
92      }
93  
94      @Override
95      public void cache(String hostname, InetSocketAddress address, long originalTtl, EventLoop loop) {
96          checkNotNull(hostname, "hostname");
97          checkNotNull(address, "address");
98          checkNotNull(loop, "loop");
99  
100         if (address.getHostString() == null) {
101             // We only cache addresses that have also a host string as we will need it later when trying to replace
102             // unresolved entries in the cache.
103             return;
104         }
105 
106         resolveCache.cache(hostname, address, Math.max(minTtl, (int) Math.min(maxTtl, originalTtl)), loop);
107     }
108 
109     @Override
110     public void clear() {
111         resolveCache.clear();
112     }
113 
114     @Override
115     public boolean clear(String hostname) {
116         return resolveCache.clear(checkNotNull(hostname, "hostname"));
117     }
118 
119     @Override
120     public String toString() {
121         return "DefaultAuthoritativeDnsServerCache(minTtl=" + minTtl + ", maxTtl=" + maxTtl + ", cached nameservers=" +
122                 resolveCache.size() + ')';
123     }
124 
125     // Package visibility for testing purposes
126     int minTtl() {
127         return minTtl;
128     }
129 
130     // Package visibility for testing purposes
131     int maxTtl() {
132         return maxTtl;
133     }
134 }