rschmitt commented on code in PR #428: URL: https://github.com/apache/httpcomponents-client/pull/428#discussion_r1161038232
########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/InetAddressComparator.java: ########## @@ -0,0 +1,470 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.hc.client5.http.impl.nio; + +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.annotation.ThreadingBehavior; + +import java.io.IOException; +import java.net.DatagramSocket; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Comparator; + +/** + * This class implements a comparator for {@link InetAddress} instances based on the Happy Eyeballs V2 algorithm. + * <p> + * The comparator is used to sort a list of IP addresses based on their reachability and preference. + * + * <p> + * The Happy Eyeballs algorithm is a mechanism for reducing connection latency when connecting to IPv6-capable + * <p> + * servers over networks where both IPv6 and IPv4 are available. The algorithm attempts to establish connections + * <p> + * using IPv6 and IPv4 in parallel, and selects the first connection to complete successfully. + * + * <p> + * This comparator implements the Happy Eyeballs V2 rules defined in RFC 8305. The following rules are used for + * <p> + * comparing two IP addresses: + * + * <ul> + * <li>Rule 1: Avoid unusable destinations.</li> + * <li>Rule 2: Prefer matching scope.</li> + * <li>Rule 3: Avoid deprecated addresses.</li> + * <li>Rule 4: Prefer higher precedence.</li> + * <li>Rule 5: Prefer matching label.</li> + * <li>Rule 6: Prefer smaller address.</li> + * <li>Rule 7: Prefer home network.</li> + * <li>Rule 8: Prefer public network.</li> + * <li>Rule 9: Prefer stable privacy addresses.</li> + * <li>Rule 10: Prefer temporary addresses.</li> + * </ul> + * + * @see <a href="https://tools.ietf.org/html/rfc8305">RFC 8305 - Happy Eyeballs Version 2: Better Connectivity Using + * <p> + * bash + * Copy code + * Concurrency</a> + * @since 5.3 + */ +@Contract(threading = ThreadingBehavior.STATELESS) +@Internal +class InetAddressComparator implements Comparator<InetAddress> { + + /** + * Singleton instance of the comparator. + */ + public static final InetAddressComparator INSTANCE = new InetAddressComparator(); + + /** + * Compares two IP addresses based on the Happy Eyeballs algorithm rules. + * <p> + * The method first orders the addresses based on their precedence, and then compares them based on other rules, + * <p> + * including avoiding unusable destinations, preferring matching scope, preferring global scope, preferring + * <p> + * IPv6 addresses, and preferring smaller address prefixes. + * + * @param addr1 the first address to be compared + * @param addr2 the second address to be compared + * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater + * <p> + * than the second + */ + @Override + public int compare(final InetAddress addr1, final InetAddress addr2) { + if (addr1 == null && addr2 == null) { + return 0; + } + if (addr1 == null) { + return -1; + } + if (addr2 == null) { + return 1; + } + + // Rule 1: Avoid unusable destinations. + final boolean add1IsReachable; + final boolean add2IsReachable; + try { + add1IsReachable = addr1.isReachable(500); Review Comment: You can't call this method, especially not from a comparator. The Javadoc states: ``` * Test whether that address is reachable. Best effort is made by the * implementation to try to reach the host, but firewalls and server * configuration may block requests resulting in an unreachable status * while some specific ports may be accessible. * A typical implementation will use ICMP ECHO REQUESTs if the * privilege can be obtained, otherwise it will try to establish * a TCP connection on port 7 (Echo) of the destination host. ``` I believe that this is supposed to work by looking at which source addresses are available. For example, if there's no IPv6 source address (for the client's end of the TCP connection), then IPv6 destination addresses are _ipso facto_ unusable. ########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/InetAddressComparator.java: ########## @@ -0,0 +1,470 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ +package org.apache.hc.client5.http.impl.nio; + +import org.apache.hc.core5.annotation.Contract; +import org.apache.hc.core5.annotation.Internal; +import org.apache.hc.core5.annotation.ThreadingBehavior; + +import java.io.IOException; +import java.net.DatagramSocket; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Comparator; + +/** + * This class implements a comparator for {@link InetAddress} instances based on the Happy Eyeballs V2 algorithm. + * <p> + * The comparator is used to sort a list of IP addresses based on their reachability and preference. + * + * <p> + * The Happy Eyeballs algorithm is a mechanism for reducing connection latency when connecting to IPv6-capable + * <p> + * servers over networks where both IPv6 and IPv4 are available. The algorithm attempts to establish connections + * <p> + * using IPv6 and IPv4 in parallel, and selects the first connection to complete successfully. + * + * <p> + * This comparator implements the Happy Eyeballs V2 rules defined in RFC 8305. The following rules are used for + * <p> + * comparing two IP addresses: + * + * <ul> + * <li>Rule 1: Avoid unusable destinations.</li> + * <li>Rule 2: Prefer matching scope.</li> + * <li>Rule 3: Avoid deprecated addresses.</li> + * <li>Rule 4: Prefer higher precedence.</li> + * <li>Rule 5: Prefer matching label.</li> + * <li>Rule 6: Prefer smaller address.</li> + * <li>Rule 7: Prefer home network.</li> + * <li>Rule 8: Prefer public network.</li> + * <li>Rule 9: Prefer stable privacy addresses.</li> + * <li>Rule 10: Prefer temporary addresses.</li> + * </ul> + * + * @see <a href="https://tools.ietf.org/html/rfc8305">RFC 8305 - Happy Eyeballs Version 2: Better Connectivity Using + * <p> + * bash + * Copy code + * Concurrency</a> + * @since 5.3 + */ +@Contract(threading = ThreadingBehavior.STATELESS) +@Internal +class InetAddressComparator implements Comparator<InetAddress> { + + /** + * Singleton instance of the comparator. + */ + public static final InetAddressComparator INSTANCE = new InetAddressComparator(); + + /** + * Compares two IP addresses based on the Happy Eyeballs algorithm rules. + * <p> + * The method first orders the addresses based on their precedence, and then compares them based on other rules, + * <p> + * including avoiding unusable destinations, preferring matching scope, preferring global scope, preferring + * <p> + * IPv6 addresses, and preferring smaller address prefixes. + * + * @param addr1 the first address to be compared + * @param addr2 the second address to be compared + * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater + * <p> + * than the second + */ + @Override + public int compare(final InetAddress addr1, final InetAddress addr2) { + if (addr1 == null && addr2 == null) { + return 0; + } + if (addr1 == null) { + return -1; + } + if (addr2 == null) { + return 1; + } + + // Rule 1: Avoid unusable destinations. + final boolean add1IsReachable; + final boolean add2IsReachable; + try { + add1IsReachable = addr1.isReachable(500); + } catch (final IOException e) { + return -1; + } + try { + + add2IsReachable = addr2.isReachable(500); + } catch (final IOException e) { + return 1; + } + + if (add1IsReachable && !add2IsReachable) { + return -1; + } else if (!add1IsReachable && add2IsReachable) { + return 1; + } + + + // Rule 2: Prefer matching scope. + final int addr1Scope = getScope(addr1); + final int addr2Scope = getScope(addr2); + final int srcScope; + try { + srcScope = getScope(getLocalAddress()); + } catch (final IOException e) { + return 0; + } + + if (addr1Scope == srcScope && addr2Scope != srcScope) { + return -1; + } else if (addr1Scope != srcScope && addr2Scope == srcScope) { + return 1; + } + + //Rule 3: Avoid deprecated addresses. + final boolean add1IsDeprecated = isDeprecated(addr1); + final boolean add2IsDeprecated = isDeprecated(addr2); + + if (add1IsDeprecated && !add2IsDeprecated) { + return 1; + } else if (!add1IsDeprecated && add2IsDeprecated) { + return -1; + } + + + // Rule 4: Prefer home addresses. + final boolean add1IsLocal = addr1.isLinkLocalAddress() || addr1.isSiteLocalAddress(); + final boolean add2IsLocal = addr1.isLinkLocalAddress() || addr1.isSiteLocalAddress(); + + if (add1IsLocal && !add2IsLocal) { + return -1; + } else if (!add1IsLocal && add2IsLocal) { + return 1; + } + + // Rule 5: Avoid deprecated addresses. + final String label1; + try { + label1 = getLabel(addr1); + } catch (final SocketException e) { + return -1; + } + final String label2; + try { + label2 = getLabel(addr2); + } catch (final SocketException e) { + return 1; + } + + if (label1.equals(label2)) { + return 0; + } else if (label1.isEmpty()) { + return 1; + } else if (label2.isEmpty()) { + return -1; + } + + // Rule 6 rule: Prefer the smaller address. + final int add1Precedence = getPrecedence(addr1); + final int add2Precedence = getPrecedence(addr2); + + if (add1Precedence > add2Precedence) { + return -1; + } else if (add1Precedence < add2Precedence) { + return 1; + } + + // Rule 7: Prefer native transport. + final boolean add1IsIPv4 = addr1 instanceof Inet4Address; + final boolean add2IsIPv4 = addr2 instanceof Inet4Address; + + if (add1IsIPv4 && !add2IsIPv4) { + return -1; + } else if (!add1IsIPv4 && add2IsIPv4) { + return 1; + } else if (addr1 instanceof Inet6Address && addr2 instanceof Inet6Address) { + final Inet6Address ipv6Addr1 = (Inet6Address) addr1; + final Inet6Address ipv6Addr2 = (Inet6Address) addr2; + + if (ipv6Addr1.isIPv4CompatibleAddress() && !ipv6Addr2.isIPv4CompatibleAddress()) { + return -1; + } else if (!ipv6Addr1.isIPv4CompatibleAddress() && ipv6Addr2.isIPv4CompatibleAddress()) { + return 1; + } + } + + + // Rule 8: Prefer smaller scope. + final int add1Scope = addr1 instanceof Inet6Address ? ((Inet6Address) addr1).getScopeId() : -1; + final int add2Scope = addr2 instanceof Inet6Address ? ((Inet6Address) addr2).getScopeId() : -1; + + if (add1Scope < add2Scope) { + return -1; + } else if (add1Scope > add2Scope) { + return 1; + } + + // Rule 9: Use longest matching prefix. + final int prefixLen1 = getMatchingPrefixLength(addr1, addr1); + final int prefixLen2 = getMatchingPrefixLength(addr2, addr1); + + if (prefixLen1 > prefixLen2) { + return -1; + } else if (prefixLen1 < prefixLen2) { + return 1; + } + + + // Rule 9: Use longest matching prefix. + final byte[] ba1 = addr1.getAddress(); + final byte[] ba2 = addr2.getAddress(); + int prefixLen = 0; + for (int i = 0; i < ba1.length; i++) { + if (ba1[i] == ba2[i]) { + prefixLen += 8; + } else { + final int xor = ba1[i] ^ ba2[i]; + // Count the number of leading zeroes in the XOR result + final int zeroes = Integer.numberOfLeadingZeros(xor) - 24; + prefixLen += zeroes; + break; + } + } + if (prefixLen == 128) { + return 0; + } else if ((ba1.length == 4 && prefixLen >= 24) || (ba1.length == 16 && prefixLen >= 64)) { + return 1; + } else if ((ba2.length == 4 && prefixLen >= 24) || (ba2.length == 16 && prefixLen >= 64)) { + return -1; + } + + + // Rule 10: Otherwise, leave the order unchanged. + return 0; + } + + + /** + * Returns the scope of the given address. For IPv6 addresses, this is the identifier of the + * scope the address is associated with. For IPv4 addresses, this always returns -1. + * + * @param address the address to get the scope for. + * @return the scope of the given address. + */ + private int getScope(final InetAddress address) { + if (address instanceof Inet6Address) { + final Inet6Address ipv6Addr = (Inet6Address) address; + final int scope = ipv6Addr.getScopeId(); + if (scope > 0) { + return scope; + } + } + return -1; Review Comment: Isn't `127.0.0.1/8` supposed to have special treatment? ########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/nio/HappyEyeballsV2AsyncClientConnectionOperator.java: ########## @@ -0,0 +1,698 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * <http://www.apache.org/>. + * + */ + +package org.apache.hc.client5.http.impl.nio; + +import org.apache.hc.client5.http.DnsResolver; +import org.apache.hc.client5.http.SchemePortResolver; +import org.apache.hc.client5.http.SystemDefaultDnsResolver; +import org.apache.hc.client5.http.impl.ConnPoolSupport; +import org.apache.hc.client5.http.impl.DefaultSchemePortResolver; +import org.apache.hc.client5.http.nio.AsyncClientConnectionOperator; +import org.apache.hc.client5.http.nio.ManagedAsyncClientConnection; +import org.apache.hc.core5.concurrent.FutureCallback; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.config.Lookup; +import org.apache.hc.core5.http.nio.ssl.TlsStrategy; +import org.apache.hc.core5.http.protocol.HttpContext; +import org.apache.hc.core5.reactor.ConnectionInitiator; +import org.apache.hc.core5.reactor.ssl.TransportSecurityLayer; +import org.apache.hc.core5.util.Args; +import org.apache.hc.core5.util.Timeout; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.ConnectException; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.SocketAddress; +import java.net.UnknownHostException; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +/** + * The {@link AsyncClientConnectionOperator} implementation that uses Happy Eyeballs V2 algorithm to connect + * to the target server. Happy Eyeballs V2 (HEV2) algorithm is used to connect to the target server by concurrently + * attempting to establish multiple connections to different IP addresses. The first connection to complete + * successfully is selected and the others are closed. If all connections fail, the last error is rethrown. + * The algorithm also applies a configurable delay before subsequent connection attempts. HEV2 was introduced + * as a means to mitigate the latency issues caused by IPv4 and IPv6 co-existence in the Internet. HEV2 is defined + * in RFC 8305. + * + * <p> + * This connection operator maintains a connection pool for each unique route (combination of target host and + * target port) and selects the next connection from the pool to establish a new connection or reuse an + * existing connection. The connection pool uses a First-In-First-Out (FIFO) queue and has a configurable limit + * on the maximum number of connections that can be kept alive in the pool. Once the maximum number of connections + * has been reached, the oldest connection in the pool is closed to make room for a new one. + * </p> + * + * <p> + * This class is thread-safe and can be used in a multi-threaded environment. + * </p> + * + * <p> + * The HEV2 algorithm is configurable through the following parameters: + * <ul> + * <li>{@code dualStackEnabled}: Whether to enable dual-stack connectivity. When set to {@code true}, + * the operator attempts to connect to both IPv4 and IPv6 addresses concurrently. When set to {@code false}, + * only IPv4 or IPv6 addresses are attempted depending on the address type of the target server.</li> + * <li>{@code maxAttempts}: The maximum number of connection attempts to be made before failing. If all + * attempts fail, the last error is rethrown.</li> + * <li>{@code delay}: The delay (in milliseconds) to apply before subsequent connection attempts.</li> + * <li>{@code connectTimeout}: The connection timeout (in milliseconds) for each attempt.</li> + * </ul> + * </p> + * + * + * <p> + * This class can be used with any {@link org.apache.hc.core5.http.nio.AsyncClientEndpoint} implementation + * that supports HTTP/1.1 or HTTP/2 protocols. + * </p> + * + * @since 5.3 + */ +public class HappyEyeballsV2AsyncClientConnectionOperator implements AsyncClientConnectionOperator { + + private static final Logger LOG = LoggerFactory.getLogger(AsyncClientConnectionOperator.class); + + + /** + * The default delay used between subsequent DNS resolution attempts, in milliseconds. + */ + private final Timeout DEFAULT_RESOLUTION_DELAY = Timeout.ofMilliseconds(50); + /** + * The default timeout duration for establishing a connection, in milliseconds. + */ + private final Timeout DEFAULT_TIMEOUT = Timeout.ofMilliseconds(250); + + /** + * The default minimum delay between connection attempts. + * This delay is used to prevent the connection operator from spamming connection attempts and to provide a reasonable + * delay between attempts for the user. + */ + private final Timeout DEFAULT_MINIMUM_CONNECTION_ATTEMPT_DELAY = Timeout.ofMilliseconds(100); + + /** + * The default maximum delay between connection attempts. + * This delay is used to prevent the connection operator from spamming connection attempts and to provide a reasonable + * delay between attempts for the user. This value is used to cap the delay between attempts to prevent the delay from becoming + * too long and causing unnecessary delays in the application's processing. + */ + private final Timeout DEFAULT_MAXIMUM_CONNECTION_ATTEMPT_DELAY = Timeout.ofMilliseconds(2000); + + /** + * The default delay before attempting to establish a connection. + * This delay is used to provide a reasonable amount of time for the underlying transport to be ready before attempting + * to establish a connection. This can help to improve the likelihood of successful connection attempts and reduce + * unnecessary delays in the application's processing. + */ + private final Timeout DEFAULT_CONNECTION_ATTEMPT_DELAY = Timeout.ofMilliseconds(250); + + + /** + * The {@link ScheduledExecutorService} used by this connection operator to execute delayed tasks, such as DNS resolution and connection attempts. + * This executor is used to control the timing of tasks in order to optimize the performance of connection attempts. By default, a single thread is used + * to execute tasks sequentially, but this can be adjusted depending on the application's workload and number of instances of the connection operator. + * If multiple instances of the connection operator are being used in the same application, it may be more efficient to use a {@link java.util.concurrent.ThreadPoolExecutor} + * with a fixed number of threads instead of a single thread executor. This will allow tasks to be executed in parallel, which can improve the overall + * performance of the application. + * If the scheduler provided to the constructor is null, a new instance of {@link Executors#newSingleThreadScheduledExecutor()} will be used as the default. + */ + private final ScheduledExecutorService scheduler; + + /** + * The underlying {@link AsyncClientConnectionOperator} that is used to establish connections + * to the target server. + */ + private final AsyncClientConnectionOperator connectionOperator; + + /** + * The DNS resolver used to resolve hostnames to IP addresses. + */ + private final DnsResolver dnsResolver; + + /** + * A lookup table used to determine the {@link TlsStrategy} to use for a given connection route. + */ + private final Lookup<TlsStrategy> tlsStrategyLookup; + + /** + * The default timeout for connection establishment attempts. If a connection cannot be established + * within this timeout, the attempt is considered failed. + */ + private final Timeout timeout; + + /** + * The minimum delay between connection establishment attempts. + */ + private final Timeout minimumConnectionAttemptDelay; + + /** + * The maximum delay between connection establishment attempts. + */ + private final Timeout maximumConnectionAttemptDelay; + + /** + * The current delay between connection establishment attempts. + */ + private final Timeout connectionAttemptDelay; + + /** + * The delay before resolution is started. + */ + private final Timeout resolution_delay; + + /** + * The number of IP addresses of each address family to include in the initial list of + * IP addresses to attempt connections to. This value is set to 2 by default, but can be + * increased to more aggressively favor a particular address family (e.g. set to 4 for IPv6). + */ + private final int firstAddressFamilyCount; + + /** + * The address family to use for establishing connections. This can be set to either + * {@link AddressFamily#IPv4} or {@link AddressFamily#IPv6}. + */ + private final AddressFamily addressFamily; + + /** + * The AddressFamily enum represents the possible address families that can be used when attempting to establish + * <p> + * connections using the Happy Eyeballs V2 algorithm. + * + * <p> + * The Happy Eyeballs V2 algorithm allows for concurrent connection attempts to be made to different IP addresses, + * <p> + * so this enum specifies whether connections should be attempted using IPv4 or IPv6 addresses. + * + * </p> + */ + public enum AddressFamily { + IPv4, IPv6 + } + + /** + * Constructs a new {@link HappyEyeballsV2AsyncClientConnectionOperator} with the specified parameters. + * + * @param tlsStrategyLookup the lookup object used to retrieve a {@link TlsStrategy} for a given {@link Route} + * @param connectionOperator the underlying {@link AsyncClientConnectionOperator} to use for establishing connections + * @param dnsResolver the {@link DnsResolver} to use for resolving target hostnames + * @param timeout the timeout duration for establishing a connection + * @param resolution_delay the configurable delay before subsequent DNS resolution attempts + * @param minimumConnectionAttemptDelay the minimum configurable delay between connection attempts + * @param maximumConnectionAttemptDelay the maximum configurable delay between connection attempts + * @param connectionAttemptDelay the configurable delay before attempting to establish a connection + * @param firstAddressFamilyCount the number of initial address families to use for establishing a connection + * @param addressFamily the preferred address family to use for establishing a connection + * @param scheduler the {@link ScheduledExecutorService} to use for scheduling tasks + * @throws IllegalArgumentException if {@code firstAddressFamilyCount} is not positive + */ + public HappyEyeballsV2AsyncClientConnectionOperator(final Lookup<TlsStrategy> tlsStrategyLookup, + final AsyncClientConnectionOperator connectionOperator, + final DnsResolver dnsResolver, + final Timeout timeout, + final Timeout resolution_delay, + final Timeout minimumConnectionAttemptDelay, + final Timeout maximumConnectionAttemptDelay, + final Timeout connectionAttemptDelay, + final int firstAddressFamilyCount, + final AddressFamily addressFamily, + final ScheduledExecutorService scheduler) { + this.tlsStrategyLookup = Args.notNull(tlsStrategyLookup, "TLS strategy lookup"); + this.connectionOperator = Args.notNull(connectionOperator, "Connection operator"); + this.dnsResolver = dnsResolver != null ? dnsResolver : SystemDefaultDnsResolver.INSTANCE; + this.timeout = timeout != null ? timeout : DEFAULT_TIMEOUT; + this.resolution_delay = resolution_delay != null ? resolution_delay : DEFAULT_RESOLUTION_DELAY; + this.minimumConnectionAttemptDelay = minimumConnectionAttemptDelay != null ? minimumConnectionAttemptDelay : DEFAULT_MINIMUM_CONNECTION_ATTEMPT_DELAY; + this.maximumConnectionAttemptDelay = maximumConnectionAttemptDelay != null ? maximumConnectionAttemptDelay : DEFAULT_MAXIMUM_CONNECTION_ATTEMPT_DELAY; + this.connectionAttemptDelay = connectionAttemptDelay != null ? connectionAttemptDelay : DEFAULT_CONNECTION_ATTEMPT_DELAY; + this.firstAddressFamilyCount = Args.positive(firstAddressFamilyCount, "firstAddressFamilyCount"); + this.addressFamily = addressFamily != null ? addressFamily : AddressFamily.IPv6; + this.scheduler = scheduler != null ? scheduler : Executors.newSingleThreadScheduledExecutor(); + } + + /** + * Constructs a new instance of {@link HappyEyeballsV2AsyncClientConnectionOperator} using the specified + * {@link Lookup} for {@link TlsStrategy} and {@link SchemePortResolver} and {@link DnsResolver}. + * <p> + * The constructor internally creates a new instance of {@link DefaultAsyncClientConnectionOperator} with the + * specified {@link Lookup} for {@link TlsStrategy}, {@link SchemePortResolver} and {@link DnsResolver}. The + * created {@link AsyncClientConnectionOperator} is then passed to the main constructor along with default values + * for other parameters. + * </p> + * + * @param tlsStrategyLookup The {@link Lookup} for {@link TlsStrategy}. + * @param schemePortResolver The {@link SchemePortResolver} to use for resolving scheme ports. + * @param dnsResolver The {@link DnsResolver} to use for resolving hostnames to IP addresses. + * @throws IllegalArgumentException if the {@code tlsStrategyLookup} or {@code schemePortResolver} or {@code dnsResolver} parameter is {@code null}. + */ + public HappyEyeballsV2AsyncClientConnectionOperator( + final Lookup<TlsStrategy> tlsStrategyLookup, + final SchemePortResolver schemePortResolver, + final DnsResolver dnsResolver) { + this(tlsStrategyLookup, + new DefaultAsyncClientConnectionOperator(tlsStrategyLookup, schemePortResolver, dnsResolver), + dnsResolver, + null, + null, + null, + null, + null, + 1, + null, + null); + } + + /** + * Creates a new instance of {@link HappyEyeballsV2AsyncClientConnectionOperator} using the provided TLS strategy lookup + * and scheme-port resolver. The DNS resolver will be set to the system default resolver. + * + * @param tlsStrategyLookup The lookup instance for {@link TlsStrategy} to be used for establishing connections. + * @param schemePortResolver The resolver instance for mapping scheme names to default port numbers. + * @throws IllegalArgumentException if {@code tlsStrategyLookup} is {@code null}. + */ + public HappyEyeballsV2AsyncClientConnectionOperator( + final Lookup<TlsStrategy> tlsStrategyLookup, + final SchemePortResolver schemePortResolver) { + this(tlsStrategyLookup, schemePortResolver != null ? schemePortResolver : DefaultSchemePortResolver.INSTANCE, null); + } + + /** + * Creates a new instance of {@link HappyEyeballsV2AsyncClientConnectionOperator} using the provided TLS strategy lookup. + * The scheme-port resolver and DNS resolver will be set to their default instances. + * + * @param tlsStrategyLookup The lookup instance for {@link TlsStrategy} to be used for establishing connections. + * @throws IllegalArgumentException if {@code tlsStrategyLookup} is {@code null}. + */ + public HappyEyeballsV2AsyncClientConnectionOperator( + final Lookup<TlsStrategy> tlsStrategyLookup) { + this(tlsStrategyLookup, DefaultSchemePortResolver.INSTANCE, null); + } + + + /** + * Attempts to connect to the given host and returns a Future that will be completed when the connection is established + * or when an error occurs. This method may attempt to connect to multiple IP addresses associated with the host, + * depending on the address family and the number of connection attempts to execute. The address family and number of + * connection attempts can be configured by calling the corresponding setters on this class. + * + * @param connectionInitiator the connection initiator to use when creating the connection + * @param host the host to connect to + * @param localAddress the local address to bind to when connecting, or null to use any available local address + * @param connectTimeout the timeout to use when connecting, or null to use the default timeout + * @param attachment the attachment to associate with the connection, or null if no attachment is needed + * @param callback the callback to invoke when the connection is established or an error occurs, or null if no callback is needed + * @return a Future that will be completed when the connection is established or when an error occurs + */ + @Override + public Future<ManagedAsyncClientConnection> connect( + final ConnectionInitiator connectionInitiator, + final HttpHost host, + final SocketAddress localAddress, + final Timeout connectTimeout, + final Object attachment, + final FutureCallback<ManagedAsyncClientConnection> callback) { + + final CompletableFuture<ManagedAsyncClientConnection> connectionFuture = new CompletableFuture<>(); + + final Timeout conTimeout = connectTimeout != null ? connectTimeout : timeout; + + resolveDnsAsync(host.getHostName()) + .thenCompose(inetAddresses -> { + final List<InetAddress> ipv4Addresses = new ArrayList<>(); + final List<InetAddress> ipv6Addresses = new ArrayList<>(); + + for (final InetAddress inetAddress : inetAddresses) { + if (inetAddress instanceof Inet4Address) { + ipv4Addresses.add(inetAddress); + } else if (inetAddress instanceof Inet6Address) { + ipv6Addresses.add(inetAddress); + } + } + + // Sort the array of addresses using the custom Comparator + Arrays.sort(inetAddresses, InetAddressComparator.INSTANCE); Review Comment: Is this a [stable sort](https://www.geeksforgeeks.org/stable-and-unstable-sorting-algorithms/)? It's important to not change the order of IP addresses that are equal under the comparison rules. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@hc.apache.org For additional commands, e-mail: dev-h...@hc.apache.org