Merge pull request #579 from zsurocking/master

Use hostname instead of InetAddress for Socket creation.
This commit is contained in:
zsurocking 2015-06-30 21:52:13 -07:00
commit af70397d88
3 changed files with 27 additions and 27 deletions

View File

@ -43,7 +43,6 @@ import io.grpc.SharedResourceHolder;
import io.grpc.SharedResourceHolder.Resource; import io.grpc.SharedResourceHolder.Resource;
import io.grpc.transport.ClientTransportFactory; import io.grpc.transport.ClientTransportFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -84,18 +83,20 @@ public final class OkHttpChannelBuilder extends AbstractChannelBuilder<OkHttpCha
/** Creates a new builder for the given server host and port. */ /** Creates a new builder for the given server host and port. */
public static OkHttpChannelBuilder forAddress(String host, int port) { public static OkHttpChannelBuilder forAddress(String host, int port) {
return new OkHttpChannelBuilder(new InetSocketAddress(host, port), host); return new OkHttpChannelBuilder(host, port);
} }
private final InetSocketAddress serverAddress;
private ExecutorService transportExecutor; private ExecutorService transportExecutor;
private String host; private final String host;
private final int port;
private String authorityHost;
private SSLSocketFactory sslSocketFactory; private SSLSocketFactory sslSocketFactory;
private ConnectionSpec connectionSpec = DEFAULT_CONNECTION_SPEC; private ConnectionSpec connectionSpec = DEFAULT_CONNECTION_SPEC;
private OkHttpChannelBuilder(InetSocketAddress serverAddress, String host) { private OkHttpChannelBuilder(String host, int port) {
this.serverAddress = Preconditions.checkNotNull(serverAddress, "serverAddress"); this.host = Preconditions.checkNotNull(host);
this.host = host; this.port = port;
this.authorityHost = host;
} }
/** /**
@ -116,7 +117,7 @@ public final class OkHttpChannelBuilder extends AbstractChannelBuilder<OkHttpCha
* <p>Should only used by tests. * <p>Should only used by tests.
*/ */
public OkHttpChannelBuilder overrideHostForAuthority(String host) { public OkHttpChannelBuilder overrideHostForAuthority(String host) {
this.host = host; this.authorityHost = host;
return this; return this;
} }
@ -144,7 +145,7 @@ public final class OkHttpChannelBuilder extends AbstractChannelBuilder<OkHttpCha
final ExecutorService executor = (transportExecutor == null) final ExecutorService executor = (transportExecutor == null)
? SharedResourceHolder.get(DEFAULT_TRANSPORT_THREAD_POOL) : transportExecutor; ? SharedResourceHolder.get(DEFAULT_TRANSPORT_THREAD_POOL) : transportExecutor;
ClientTransportFactory transportFactory = new OkHttpClientTransportFactory( ClientTransportFactory transportFactory = new OkHttpClientTransportFactory(
serverAddress, host, executor, sslSocketFactory, connectionSpec); host, port, authorityHost, executor, sslSocketFactory, connectionSpec);
Runnable terminationRunnable = null; Runnable terminationRunnable = null;
// We shut down the executor only if we created it. // We shut down the executor only if we created it.
if (transportExecutor == null) { if (transportExecutor == null) {

View File

@ -67,7 +67,6 @@ import okio.ByteString;
import okio.Okio; import okio.Okio;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -128,7 +127,8 @@ class OkHttpClientTransport implements ClientTransport {
ERROR_CODE_TO_STATUS = Collections.unmodifiableMap(errorToStatus); ERROR_CODE_TO_STATUS = Collections.unmodifiableMap(errorToStatus);
} }
private final InetSocketAddress address; private final String host;
private final int port;
private final String authorityHost; private final String authorityHost;
private final String defaultAuthority; private final String defaultAuthority;
private final Random random = new Random(); private final Random random = new Random();
@ -162,11 +162,12 @@ class OkHttpClientTransport implements ClientTransport {
private LinkedList<PendingStream> pendingStreams = new LinkedList<PendingStream>(); private LinkedList<PendingStream> pendingStreams = new LinkedList<PendingStream>();
private final ConnectionSpec connectionSpec; private final ConnectionSpec connectionSpec;
OkHttpClientTransport(InetSocketAddress address, String authorityHost, Executor executor, OkHttpClientTransport(String host, int port, String authorityHost, Executor executor,
@Nullable SSLSocketFactory sslSocketFactory, ConnectionSpec connectionSpec) { @Nullable SSLSocketFactory sslSocketFactory, ConnectionSpec connectionSpec) {
this.address = Preconditions.checkNotNull(address); this.host = Preconditions.checkNotNull(host);
this.port = port;
this.authorityHost = authorityHost; this.authorityHost = authorityHost;
defaultAuthority = authorityHost + ":" + address.getPort(); defaultAuthority = authorityHost + ":" + port;
this.executor = Preconditions.checkNotNull(executor); this.executor = Preconditions.checkNotNull(executor);
// Client initiated streams are odd, server initiated ones are even. Server should not need to // Client initiated streams are odd, server initiated ones are even. Server should not need to
// use it. We start clients at 3 to avoid conflicting with HTTP negotiation. // use it. We start clients at 3 to avoid conflicting with HTTP negotiation.
@ -191,7 +192,8 @@ class OkHttpClientTransport implements ClientTransport {
@VisibleForTesting @VisibleForTesting
OkHttpClientTransport(Executor executor, FrameReader frameReader, AsyncFrameWriter frameWriter, OkHttpClientTransport(Executor executor, FrameReader frameReader, AsyncFrameWriter frameWriter,
int nextStreamId, Socket socket, Ticker ticker) { int nextStreamId, Socket socket, Ticker ticker) {
address = null; host = null;
port = 0;
authorityHost = null; authorityHost = null;
defaultAuthority = "notarealauthority:80"; defaultAuthority = "notarealauthority:80";
this.executor = Preconditions.checkNotNull(executor); this.executor = Preconditions.checkNotNull(executor);
@ -318,18 +320,14 @@ class OkHttpClientTransport implements ClientTransport {
public void start(Listener listener) { public void start(Listener listener) {
this.listener = Preconditions.checkNotNull(listener, "listener"); this.listener = Preconditions.checkNotNull(listener, "listener");
// We set host to null for test. // We set host to null for test.
if (address != null) { if (host != null) {
BufferedSource source; BufferedSource source;
BufferedSink sink; BufferedSink sink;
try { try {
if (address.isUnresolved()) { socket = new Socket(host, port);
socket = new Socket(address.getHostName(), address.getPort());
} else {
socket = new Socket(address.getAddress(), address.getPort());
}
if (sslSocketFactory != null) { if (sslSocketFactory != null) {
socket = OkHttpTlsUpgrader.upgrade( socket = OkHttpTlsUpgrader.upgrade(
sslSocketFactory, socket, authorityHost, address.getPort(), connectionSpec); sslSocketFactory, socket, authorityHost, port, connectionSpec);
} }
socket.setTcpNoDelay(true); socket.setTcpNoDelay(true);
source = Okio.buffer(Okio.source(socket)); source = Okio.buffer(Okio.source(socket));

View File

@ -38,7 +38,6 @@ import com.squareup.okhttp.ConnectionSpec;
import io.grpc.transport.ClientTransport; import io.grpc.transport.ClientTransport;
import io.grpc.transport.ClientTransportFactory; import io.grpc.transport.ClientTransportFactory;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.SSLSocketFactory;
@ -47,15 +46,17 @@ import javax.net.ssl.SSLSocketFactory;
* Factory that manufactures instances of {@link OkHttpClientTransport}. * Factory that manufactures instances of {@link OkHttpClientTransport}.
*/ */
class OkHttpClientTransportFactory implements ClientTransportFactory { class OkHttpClientTransportFactory implements ClientTransportFactory {
private final InetSocketAddress address; private final String host;
private final int port;
private final ExecutorService executor; private final ExecutorService executor;
private final String authorityHost; private final String authorityHost;
private final SSLSocketFactory sslSocketFactory; private final SSLSocketFactory sslSocketFactory;
private final ConnectionSpec connectionSpec; private final ConnectionSpec connectionSpec;
public OkHttpClientTransportFactory(InetSocketAddress address, String authorityHost, public OkHttpClientTransportFactory(String host, int port, String authorityHost,
ExecutorService executor, SSLSocketFactory factory, ConnectionSpec connectionSpec) { ExecutorService executor, SSLSocketFactory factory, ConnectionSpec connectionSpec) {
this.address = Preconditions.checkNotNull(address, "address"); this.host = Preconditions.checkNotNull(host, "host");
this.port = port;
this.executor = Preconditions.checkNotNull(executor, "executor"); this.executor = Preconditions.checkNotNull(executor, "executor");
this.authorityHost = Preconditions.checkNotNull(authorityHost, "authorityHost"); this.authorityHost = Preconditions.checkNotNull(authorityHost, "authorityHost");
this.sslSocketFactory = factory; this.sslSocketFactory = factory;
@ -65,7 +66,7 @@ class OkHttpClientTransportFactory implements ClientTransportFactory {
@Override @Override
public ClientTransport newClientTransport() { public ClientTransport newClientTransport() {
return new OkHttpClientTransport( return new OkHttpClientTransport(
address, authorityHost, executor, sslSocketFactory, connectionSpec); host, port, authorityHost, executor, sslSocketFactory, connectionSpec);
} }
} }