View Javadoc
1   /*
2    * Copyright 2016 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  
17  package io.netty.resolver.dns;
18  
19  import io.netty.channel.ChannelFactory;
20  import io.netty.channel.EventLoop;
21  import io.netty.channel.socket.DatagramChannel;
22  import io.netty.resolver.AddressResolver;
23  import io.netty.resolver.AddressResolverGroup;
24  import io.netty.resolver.NameResolver;
25  import io.netty.resolver.RoundRobinInetAddressResolver;
26  import io.netty.util.internal.UnstableApi;
27  
28  import java.net.InetAddress;
29  import java.net.InetSocketAddress;
30  
31  /**
32   * A {@link AddressResolverGroup} of {@link DnsNameResolver}s that supports random selection of destination addresses if
33   * multiple are provided by the nameserver. This is ideal for use in applications that use a pool of connections, for
34   * which connecting to a single resolved address would be inefficient.
35   */
36  @UnstableApi
37  public class RoundRobinDnsAddressResolverGroup extends DnsAddressResolverGroup {
38  
39      public RoundRobinDnsAddressResolverGroup(
40              Class<? extends DatagramChannel> channelType,
41              DnsServerAddressStreamProvider nameServerProvider) {
42          super(channelType, nameServerProvider);
43      }
44  
45      public RoundRobinDnsAddressResolverGroup(
46              ChannelFactory<? extends DatagramChannel> channelFactory,
47              DnsServerAddressStreamProvider nameServerProvider) {
48          super(channelFactory, nameServerProvider);
49      }
50  
51      /**
52       * We need to override this method, not
53       * {@link #newNameResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)},
54       * because we need to eliminate possible caching of {@link io.netty.resolver.NameResolver#resolve}
55       * by {@link InflightNameResolver} created in
56       * {@link #newResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)}.
57       */
58      @Override
59      protected final AddressResolver<InetSocketAddress> newAddressResolver(EventLoop eventLoop,
60                                                                            NameResolver<InetAddress> resolver)
61              throws Exception {
62          return new RoundRobinInetAddressResolver(eventLoop, resolver).asAddressResolver();
63      }
64  }