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