You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "arturobernalg (via GitHub)" <gi...@apache.org> on 2023/04/04 16:47:59 UTC

[GitHub] [httpcomponents-client] arturobernalg commented on a diff in pull request #430: Add support for proxy failover

arturobernalg commented on code in PR #430:
URL: https://github.com/apache/httpcomponents-client/pull/430#discussion_r1157517970


##########
httpclient5/src/main/java/org/apache/hc/client5/http/impl/routing/FailoverProxySelector.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * ====================================================================
+ * 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;
+
+/**
+ * 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 int currentIndex = 0;

Review Comment:
   Yes, you are correct. If multiple threads access the currentIndex variable in the FailoverProxySelector class, then it should be made thread-safe.
   Changed. 



-- 
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-unsubscribe@hc.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org