View Javadoc
1   /*
2    * Copyright 2017 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    *   http://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.util.internal.PlatformDependent;
19  import io.netty.util.internal.UnstableApi;
20  
21  import java.util.concurrent.TimeUnit;
22  import java.util.concurrent.atomic.AtomicLong;
23  
24  /**
25   * Utility methods related to {@link DnsServerAddressStreamProvider}.
26   */
27  @UnstableApi
28  public final class DnsServerAddressStreamProviders {
29      // We use 5 minutes which is the same as what OpenJDK is using in sun.net.dns.ResolverConfigurationImpl.
30      private static final long REFRESH_INTERVAL = TimeUnit.MINUTES.toNanos(5);
31  
32      // TODO(scott): how is this done on Windows? This may require a JNI call to GetNetworkParams
33      // https://msdn.microsoft.com/en-us/library/aa365968(VS.85).aspx.
34      private static final DnsServerAddressStreamProvider DEFAULT_DNS_SERVER_ADDRESS_STREAM_PROVIDER =
35              new DnsServerAddressStreamProvider() {
36          private volatile DnsServerAddressStreamProvider currentProvider = provider();
37          private final AtomicLong lastRefresh = new AtomicLong(System.nanoTime());
38  
39          @Override
40          public DnsServerAddressStream nameServerAddressStream(String hostname) {
41              long last = lastRefresh.get();
42              DnsServerAddressStreamProvider current = currentProvider;
43              if (System.nanoTime() - last > REFRESH_INTERVAL) {
44                  // This is slightly racy which means it will be possible still use the old configuration for a small
45                  // amount of time, but that's ok.
46                  if (lastRefresh.compareAndSet(last, System.nanoTime())) {
47                      current = currentProvider = provider();
48                  }
49              }
50              return current.nameServerAddressStream(hostname);
51          }
52  
53          private DnsServerAddressStreamProvider provider() {
54              // If on windows just use the DefaultDnsServerAddressStreamProvider.INSTANCE as otherwise
55              // we will log some error which may be confusing.
56              return PlatformDependent.isWindows() ? DefaultDnsServerAddressStreamProvider.INSTANCE :
57                      UnixResolverDnsServerAddressStreamProvider.parseSilently();
58          }
59      };
60  
61      private DnsServerAddressStreamProviders() {
62      }
63  
64      /**
65       * A {@link DnsServerAddressStreamProvider} which inherits the DNS servers from your local host's configuration.
66       * <p>
67       * Note that only macOS and Linux are currently supported.
68       * @return A {@link DnsServerAddressStreamProvider} which inherits the DNS servers from your local host's
69       * configuration.
70       */
71      public static DnsServerAddressStreamProvider platformDefault() {
72          return DEFAULT_DNS_SERVER_ADDRESS_STREAM_PROVIDER;
73      }
74  }