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(DnsNameResolverBuilder dnsResolverBuilder) {
40          super(dnsResolverBuilder);
41      }
42  
43      public RoundRobinDnsAddressResolverGroup(
44              Class<? extends DatagramChannel> channelType,
45              DnsServerAddressStreamProvider nameServerProvider) {
46          super(channelType, nameServerProvider);
47      }
48  
49      public RoundRobinDnsAddressResolverGroup(
50              ChannelFactory<? extends DatagramChannel> channelFactory,
51              DnsServerAddressStreamProvider nameServerProvider) {
52          super(channelFactory, nameServerProvider);
53      }
54  
55      /**
56       * We need to override this method, not
57       * {@link #newNameResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)},
58       * because we need to eliminate possible caching of {@link io.netty.resolver.NameResolver#resolve}
59       * by {@link InflightNameResolver} created in
60       * {@link #newResolver(EventLoop, ChannelFactory, DnsServerAddressStreamProvider)}.
61       */
62      @Override
63      protected final AddressResolver<InetSocketAddress> newAddressResolver(EventLoop eventLoop,
64                                                                            NameResolver<InetAddress> resolver)
65              throws Exception {
66          return new RoundRobinInetAddressResolver(eventLoop, resolver).asAddressResolver();
67      }
68  }