ok2c commented on code in PR #430: URL: https://github.com/apache/httpcomponents-client/pull/430#discussion_r1160485123
########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/FailoverProxySelector.java: ########## @@ -0,0 +1,127 @@ +/* + * ==================================================================== + * 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.routing; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.Proxy; +import java.net.ProxySelector; +import java.net.SocketAddress; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * A ProxySelector implementation that selects proxies using a failover strategy. + * <p> + * This implementation maintains a list of other ProxySelectors, and selects a proxy + * by delegating to the next available ProxySelector in the list, until a non-empty + * proxy list is returned. If a ProxySelector fails with an exception, the next + * ProxySelector in the list is tried. Once a non-empty proxy list is returned by + * a ProxySelector, it becomes the active ProxySelector and is used for subsequent + * calls to select(). If a connection fails, connectFailed() is called on the + * active ProxySelector. + * <p> + * This implementation is useful in environments where multiple proxy servers may be + * used, and where it is desirable to failover to a different proxy if the current + * one fails. + * + * @since 5.3 + */ +public class FailoverProxySelector extends ProxySelector { + + private static final Logger LOG = LoggerFactory.getLogger(FailoverProxySelector.class); + + private final List<ProxySelector> selectors; + private final AtomicInteger currentIndex; + + + /** + * Constructs a FailoverProxySelector from the given list of ProxySelectors. + * + * @param selectors the list of ProxySelectors to use. + * @throws IllegalArgumentException if the list is null or empty. + */ + public FailoverProxySelector(final List<ProxySelector> selectors) { + if (selectors == null || selectors.isEmpty()) { + throw new IllegalArgumentException("At least one ProxySelector is required"); + } + this.selectors = new ArrayList<>(selectors); + currentIndex = new AtomicInteger(0); + + } + + /** + * Selects a list of proxies for the given URI by delegating to the next + * available ProxySelector in the list, until a non-empty proxy list is + * returned. Once a non-empty proxy list is returned, it becomes the active + * ProxySelector and is used for subsequent calls to select(). + * + * @param uri the URI to select a proxy for. + * @return a list of proxies for the given URI. + */ + @Override + public List<Proxy> select(final URI uri) { + List<Proxy> result = Collections.emptyList(); + for (int i = 0; i < selectors.size(); i++) { + final ProxySelector selector = selectors.get((i + currentIndex.get()) % selectors.size()); + try { + result = selector.select(uri); + if (!result.isEmpty()) { + currentIndex.set((i + currentIndex.get()) % selectors.size()); + break; + } + } catch (final Exception e) { + // ignore and try the next selector + if (LOG.isDebugEnabled()) { + LOG.debug("Exception caught while selecting proxy for URI {}: {}", uri, e.getMessage()); + + } + } + } + return result; + } + + + /** + * Notifies the active ProxySelector that a connection to the given URI + * using the given proxy has failed. + * + * @param uri the URI that failed to connect. + * @param sa the SocketAddress of the proxy that failed to connect. + * @param ioe the IOException that resulted from the failed connection. + */ + @Override + public void connectFailed(final URI uri, final SocketAddress sa, final IOException ioe) { + final ProxySelector selector = selectors.get(currentIndex.get()); Review Comment: @arturobernalg This approach can produce unexpected results in the selector is being used concurrently by multiple threads. The current index can easily change from another thread while the `#select` method is still being called. As much as I hate `ThreadLocal`s I suppose this implementation must maintain a separate state per each thread of execution. ########## httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/FailoverProxySelector.java: ########## @@ -0,0 +1,127 @@ +/* + * ==================================================================== + * 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.routing; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.net.Proxy; +import java.net.ProxySelector; +import java.net.SocketAddress; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * A ProxySelector implementation that selects proxies using a failover strategy. + * <p> + * This implementation maintains a list of other ProxySelectors, and selects a proxy + * by delegating to the next available ProxySelector in the list, until a non-empty + * proxy list is returned. If a ProxySelector fails with an exception, the next + * ProxySelector in the list is tried. Once a non-empty proxy list is returned by + * a ProxySelector, it becomes the active ProxySelector and is used for subsequent + * calls to select(). If a connection fails, connectFailed() is called on the + * active ProxySelector. + * <p> + * This implementation is useful in environments where multiple proxy servers may be + * used, and where it is desirable to failover to a different proxy if the current + * one fails. + * + * @since 5.3 + */ +public class FailoverProxySelector extends ProxySelector { + + private static final Logger LOG = LoggerFactory.getLogger(FailoverProxySelector.class); + + private final List<ProxySelector> selectors; + private final AtomicInteger currentIndex; + + + /** + * Constructs a FailoverProxySelector from the given list of ProxySelectors. + * + * @param selectors the list of ProxySelectors to use. + * @throws IllegalArgumentException if the list is null or empty. + */ + public FailoverProxySelector(final List<ProxySelector> selectors) { + if (selectors == null || selectors.isEmpty()) { + throw new IllegalArgumentException("At least one ProxySelector is required"); + } + this.selectors = new ArrayList<>(selectors); + currentIndex = new AtomicInteger(0); + + } + + /** + * Selects a list of proxies for the given URI by delegating to the next + * available ProxySelector in the list, until a non-empty proxy list is + * returned. Once a non-empty proxy list is returned, it becomes the active + * ProxySelector and is used for subsequent calls to select(). + * + * @param uri the URI to select a proxy for. + * @return a list of proxies for the given URI. + */ + @Override + public List<Proxy> select(final URI uri) { Review Comment: @arturobernalg What is exactly the intent of this implementation? Pick a backup selector in case the primary selector fails to produce a result or distribute the load randomly across multiple selectors? I am not quite sure I understand the logic of this method -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
