Example usage for io.netty.resolver ResolvedAddressTypes IPV4_PREFERRED

List of usage examples for io.netty.resolver ResolvedAddressTypes IPV4_PREFERRED

Introduction

In this page you can find the example usage for io.netty.resolver ResolvedAddressTypes IPV4_PREFERRED.

Prototype

ResolvedAddressTypes IPV4_PREFERRED

To view the source code for io.netty.resolver ResolvedAddressTypes IPV4_PREFERRED.

Click Source Link

Document

Prefer IPv4 addresses over IPv6 ones

Usage

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroup.java

License:Apache License

private static List<DnsQuestion> newQuestions(String hostname,
        @Nullable ResolvedAddressTypes resolvedAddressTypes) {

    if (resolvedAddressTypes == null) {
        if (NetUtil.isIpV4StackPreferred()) {
            resolvedAddressTypes = ResolvedAddressTypes.IPV4_ONLY;
        } else {/*from   w  w  w.  j a  v  a 2  s  . c o m*/
            resolvedAddressTypes = ResolvedAddressTypes.IPV4_PREFERRED;
        }
    }

    final ImmutableList.Builder<DnsQuestion> builder = ImmutableList.builder();
    switch (resolvedAddressTypes) {
    case IPV4_ONLY:
    case IPV4_PREFERRED:
    case IPV6_PREFERRED:
        builder.add(new DefaultDnsQuestion(hostname, DnsRecordType.A));
        break;
    }
    switch (resolvedAddressTypes) {
    case IPV6_ONLY:
    case IPV4_PREFERRED:
    case IPV6_PREFERRED:
        builder.add(new DefaultDnsQuestion(hostname, DnsRecordType.AAAA));
        break;
    }
    return builder.build();
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

@Test
public void ipV4AndIpV6() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(new DefaultDnsQuestion("baz.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("baz.com.", "1.1.1.1")),
            new DefaultDnsQuestion("baz.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("baz.com.", "::1"))))) {
        try (DnsAddressEndpointGroup group = new DnsAddressEndpointGroupBuilder("baz.com").port(8080)
                .serverAddresses(server.addr()).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                .build()) {/*from   w w  w . j  a v a2 s.  c o m*/

            assertThat(group.awaitInitialEndpoints()).containsExactly(
                    Endpoint.of("baz.com", 8080).withIpAddr("1.1.1.1"),
                    Endpoint.of("baz.com", 8080).withIpAddr("::1"));
        }
    }
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

@Test
public void cname() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(new DefaultDnsQuestion("a.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newBadAddressRecord("a.com.", true))
                    .addRecord(ANSWER, newCnameRecord("a.com.", "b.com."))
                    .addRecord(ANSWER, newAddressRecord("b.com.", "1.1.1.1")),
            new DefaultDnsQuestion("a.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newBadAddressRecord("a.com.", false))
                    .addRecord(ANSWER, newCnameRecord("a.com.", "b.com."))
                    .addRecord(ANSWER, newAddressRecord("b.com.", "::1"))))) {
        try (DnsAddressEndpointGroup group = new DnsAddressEndpointGroupBuilder("a.com").port(8080)
                .serverAddresses(server.addr()).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                .build()) {/* ww  w  .  ja v  a 2 s  . c  om*/

            assertThat(group.awaitInitialEndpoints()).containsExactly(
                    Endpoint.of("a.com", 8080).withIpAddr("1.1.1.1"),
                    Endpoint.of("a.com", 8080).withIpAddr("::1"));
        }
    }
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

@Test
public void mixedLoopbackAddresses() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(new DefaultDnsQuestion("foo.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "127.0.0.1")),
            new DefaultDnsQuestion("foo.com.", AAAA),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("foo.com.", "::1"))))) {
        try (DnsAddressEndpointGroup group = new DnsAddressEndpointGroupBuilder("foo.com").port(8080)
                .serverAddresses(server.addr()).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                .build()) {//  w w w .  j a v  a  2 s.  co m

            assertThat(group.awaitInitialEndpoints())
                    .containsExactly(Endpoint.of("foo.com", 8080).withIpAddr("127.0.0.1"));
        }
    }
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

@Test
public void backoff() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of())) { // Respond nothing.
        try (DnsAddressEndpointGroup group = new DnsAddressEndpointGroupBuilder("backoff.com")
                .serverAddresses(server.addr()).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                .backoff(Backoff.fixed(500)).build()) {

            await().untilAsserted(() -> assertThat(group.attemptsSoFar).isGreaterThan(2));
            assertThat(group.endpoints()).isEmpty();

            // Start to respond correctly.
            server.setResponses(ImmutableMap.of(new DefaultDnsQuestion("backoff.com.", A),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("backoff.com", "1.1.1.1")),
                    new DefaultDnsQuestion("backoff.com.", AAAA),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("backoff.com", "::1"))));
            assertThat(group.awaitInitialEndpoints()).containsExactly(
                    Endpoint.of("backoff.com").withIpAddr("1.1.1.1"),
                    Endpoint.of("backoff.com").withIpAddr("::1"));
        }//from  w ww.j  av  a2 s  . c o m
    }
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

@Test
public void backoffOnEmptyResponse() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            // Respond with empty records.
            new DefaultDnsQuestion("empty.com.", A), new DefaultDnsResponse(0),
            new DefaultDnsQuestion("empty.com.", AAAA), new DefaultDnsResponse(0)))) {
        try (DnsAddressEndpointGroup group = new DnsAddressEndpointGroupBuilder("empty.com")
                .serverAddresses(server.addr()).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                .backoff(Backoff.fixed(500)).build()) {

            await().untilAsserted(() -> assertThat(group.attemptsSoFar).isGreaterThan(2));
            assertThat(group.endpoints()).isEmpty();

            // Start to respond correctly.
            server.setResponses(ImmutableMap.of(new DefaultDnsQuestion("empty.com.", A),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("empty.com", "1.1.1.1")),
                    new DefaultDnsQuestion("empty.com.", AAAA),
                    new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("empty.com", "::1"))));
            assertThat(group.awaitInitialEndpoints()).containsExactly(
                    Endpoint.of("empty.com").withIpAddr("1.1.1.1"), Endpoint.of("empty.com").withIpAddr("::1"));
        }//  ww w .jav  a 2s.c o  m
    }
}

From source file:com.linecorp.armeria.client.endpoint.dns.DnsAddressEndpointGroupTest.java

License:Apache License

@Test
public void partialResponse() throws Exception {
    try (TestDnsServer server = new TestDnsServer(ImmutableMap.of(
            // Respond A record only.
            // Respond with NXDOMAIN for AAAA.
            new DefaultDnsQuestion("partial.com.", A),
            new DefaultDnsResponse(0).addRecord(ANSWER, newAddressRecord("partial.com", "1.1.1.1"))))) {
        try (DnsAddressEndpointGroup group = new DnsAddressEndpointGroupBuilder("partial.com")
                .serverAddresses(server.addr()).resolvedAddressTypes(ResolvedAddressTypes.IPV4_PREFERRED)
                .backoff(Backoff.fixed(500)).build()) {

            assertThat(group.awaitInitialEndpoints())
                    .containsExactly(Endpoint.of("partial.com").withIpAddr("1.1.1.1"));
        }// w  w w  .j  a v  a2  s  . c o  m
    }
}