HTTPCLIENT-898: Improved multihome network support

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@932018 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2010-04-08 17:26:55 +00:00
parent 14a4e09aa8
commit 42b6aa2f93
1 changed files with 96 additions and 60 deletions

View File

@ -32,7 +32,10 @@ import java.net.ConnectException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.annotation.ThreadSafe; import org.apache.http.annotation.ThreadSafe;
import org.apache.http.HttpHost; import org.apache.http.HttpHost;
@ -40,6 +43,7 @@ import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException; import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.conn.OperatedClientConnection; import org.apache.http.conn.OperatedClientConnection;
import org.apache.http.conn.ClientConnectionOperator; import org.apache.http.conn.ClientConnectionOperator;
@ -49,8 +53,18 @@ import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.scheme.SchemeSocketFactory; import org.apache.http.conn.scheme.SchemeSocketFactory;
/** /**
* Default implementation of a {@link ClientConnectionOperator}. It uses * Default implementation of a {@link ClientConnectionOperator}. It uses a {@link SchemeRegistry}
* a {@link SchemeRegistry} to look up {@link SchemeSocketFactory} objects. * to look up {@link SchemeSocketFactory} objects.
* <p>
* This connection operator is multihome network aware and will attempt to retry failed connects
* against all known IP addresses sequentially until the connect is successful or all known
* addresses fail to respond. Please note the same
* {@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT} value will be used
* for each connection attempt, so in the worst case the total elapsed time before timeout
* can be <code>CONNECTION_TIMEOUT * n</code> where <code>n</code> is the number of IP addresses
* of the given host. One can disable multihome support by overriding
* the {@link #resolveHostname(String)} method and returning only one IP address for the given
* host name.
* <p> * <p>
* The following parameters can be used to customize the behavior of this * The following parameters can be used to customize the behavior of this
* class: * class:
@ -58,6 +72,7 @@ import org.apache.http.conn.scheme.SchemeSocketFactory;
* <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li> * <li>{@link org.apache.http.params.CoreProtocolPNames#HTTP_ELEMENT_CHARSET}</li>
* <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li> * <li>{@link org.apache.http.params.CoreConnectionPNames#TCP_NODELAY}</li>
* <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li> * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_TIMEOUT}</li>
* <li>{@link org.apache.http.params.CoreConnectionPNames#CONNECTION_TIMEOUT}</li>
* <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li> * <li>{@link org.apache.http.params.CoreConnectionPNames#SO_LINGER}</li>
* <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li> * <li>{@link org.apache.http.params.CoreConnectionPNames#SOCKET_BUFFER_SIZE}</li>
* <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li> * <li>{@link org.apache.http.params.CoreConnectionPNames#MAX_LINE_LENGTH}</li>
@ -68,6 +83,8 @@ import org.apache.http.conn.scheme.SchemeSocketFactory;
@ThreadSafe @ThreadSafe
public class DefaultClientConnectionOperator implements ClientConnectionOperator { public class DefaultClientConnectionOperator implements ClientConnectionOperator {
private final Log log = LogFactory.getLog(getClass());
/** The scheme registry for looking up socket factories. */ /** The scheme registry for looking up socket factories. */
protected final SchemeRegistry schemeRegistry; // @ThreadSafe protected final SchemeRegistry schemeRegistry; // @ThreadSafe
@ -76,92 +93,97 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator
* *
* @param schemes the scheme registry * @param schemes the scheme registry
*/ */
public DefaultClientConnectionOperator(SchemeRegistry schemes) { public DefaultClientConnectionOperator(final SchemeRegistry schemes) {
if (schemes == null) { if (schemes == null) {
throw new IllegalArgumentException throw new IllegalArgumentException("Scheme registry amy not be null");
("Scheme registry must not be null.");
} }
schemeRegistry = schemes; this.schemeRegistry = schemes;
} }
public OperatedClientConnection createConnection() { public OperatedClientConnection createConnection() {
return new DefaultClientConnection(); return new DefaultClientConnection();
} }
public void openConnection(OperatedClientConnection conn, public void openConnection(
HttpHost target, final OperatedClientConnection conn,
InetAddress local, final HttpHost target,
HttpContext context, final InetAddress local,
HttpParams params) final HttpContext context,
throws IOException { final HttpParams params) throws IOException {
if (conn == null) { if (conn == null) {
throw new IllegalArgumentException throw new IllegalArgumentException("Connection may not be null");
("Connection must not be null.");
} }
if (target == null) { if (target == null) {
throw new IllegalArgumentException throw new IllegalArgumentException("Target host may not be null");
("Target host must not be null.");
} }
// local address may be null
//@@@ is context allowed to be null?
if (params == null) { if (params == null) {
throw new IllegalArgumentException throw new IllegalArgumentException("Parameters may not be null");
("Parameters must not be null.");
} }
if (conn.isOpen()) { if (conn.isOpen()) {
throw new IllegalArgumentException throw new IllegalStateException("Connection must not be open");
("Connection must not be open.");
} }
Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
SchemeSocketFactory sf = schm.getSchemeSocketFactory(); SchemeSocketFactory sf = schm.getSchemeSocketFactory();
Socket sock = sf.createSocket(); InetAddress[] addresses = resolveHostname(target.getHostName());
conn.opening(sock, target);
InetAddress address = InetAddress.getByName(target.getHostName());
int port = schm.resolvePort(target.getPort()); int port = schm.resolvePort(target.getPort());
InetSocketAddress remoteAddress = new InetSocketAddress(address, port); for (int i = 0; i < addresses.length; i++) {
InetSocketAddress localAddress = null; InetAddress address = addresses[i];
if (local != null) { boolean last = i == addresses.length;
localAddress = new InetSocketAddress(local, 0);
} Socket sock = sf.createSocket();
try { conn.opening(sock, target);
Socket connsock = sf.connectSocket(sock, remoteAddress, localAddress, params);
if (sock != connsock) { InetSocketAddress remoteAddress = new InetSocketAddress(address, port);
sock = connsock; InetSocketAddress localAddress = null;
conn.opening(sock, target); if (local != null) {
localAddress = new InetSocketAddress(local, 0);
}
if (this.log.isDebugEnabled()) {
this.log.debug("Connecting to " + remoteAddress);
}
try {
Socket connsock = sf.connectSocket(sock, remoteAddress, localAddress, params);
if (sock != connsock) {
sock = connsock;
conn.opening(sock, target);
}
prepareSocket(sock, context, params);
conn.openCompleted(sf.isSecure(sock), params);
return;
} catch (ConnectException ex) {
if (last) {
throw new HttpHostConnectException(target, ex);
}
} catch (ConnectTimeoutException ex) {
if (last) {
throw ex;
}
}
if (this.log.isDebugEnabled()) {
this.log.debug("Connect to " + remoteAddress + " timed out. " +
"Connection will be retried using another IP address");
} }
} catch (ConnectException ex) {
throw new HttpHostConnectException(target, ex);
} }
prepareSocket(sock, context, params);
conn.openCompleted(sf.isSecure(sock), params);
} }
public void updateSecureConnection(OperatedClientConnection conn, public void updateSecureConnection(
HttpHost target, final OperatedClientConnection conn,
HttpContext context, final HttpHost target,
HttpParams params) final HttpContext context,
throws IOException { final HttpParams params) throws IOException {
if (conn == null) { if (conn == null) {
throw new IllegalArgumentException throw new IllegalArgumentException("Connection may not be null");
("Connection must not be null.");
} }
if (target == null) { if (target == null) {
throw new IllegalArgumentException throw new IllegalArgumentException("Target host may not be null");
("Target host must not be null.");
} }
if (params == null) { if (params == null) {
throw new IllegalArgumentException throw new IllegalArgumentException("Parameters may not be null");
("Parameters must not be null.");
} }
if (!conn.isOpen()) { if (!conn.isOpen()) {
throw new IllegalArgumentException throw new IllegalStateException("Connection must be open");
("Connection must be open.");
} }
final Scheme schm = schemeRegistry.getScheme(target.getSchemeName()); final Scheme schm = schemeRegistry.getScheme(target.getSchemeName());
@ -192,10 +214,10 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator
* *
* @throws IOException in case of an IO problem * @throws IOException in case of an IO problem
*/ */
protected void prepareSocket(Socket sock, HttpContext context, protected void prepareSocket(
HttpParams params) final Socket sock,
throws IOException { final HttpContext context,
final HttpParams params) throws IOException {
sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params)); sock.setTcpNoDelay(HttpConnectionParams.getTcpNoDelay(params));
sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params)); sock.setSoTimeout(HttpConnectionParams.getSoTimeout(params));
@ -205,5 +227,19 @@ public class DefaultClientConnectionOperator implements ClientConnectionOperator
} }
} }
/**
* Resolves the given host name to an array of corresponding IP addresses, based on the
* configured name service on the system.
*
* @param host host name to resolve
* @return array of IP addresses
* @exception UnknownHostException if no IP address for the host could be determined.
*
* @since 4.1
*/
protected InetAddress[] resolveHostname(final String host) throws UnknownHostException {
return InetAddress.getAllByName(host);
}
} }