diff options
author | Lukacs Berki <lberki@google.com> | 2016-04-28 08:32:04 +0000 |
---|---|---|
committer | Lukacs Berki <lberki@google.com> | 2016-04-28 08:53:01 +0000 |
commit | c8e7424ce63a08282dc0c7e1745abc6a99847792 (patch) | |
tree | 9ee850fad9418e926dec0c722d5fcf13cc78689e /src/main/java/com | |
parent | c6da03408969facade49f500fcdaaebf4e3ccfc5 (diff) |
Figure out the actual address of the command port a little differently, in a way that actually works.
--
MOS_MIGRATED_REVID=120997894
Diffstat (limited to 'src/main/java/com')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java index a621314c04..e7311f9d16 100644 --- a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java +++ b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java @@ -41,7 +41,6 @@ import io.grpc.stub.StreamObserver; import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.Field; -import java.net.InetAddress; import java.net.InetSocketAddress; import java.nio.channels.ServerSocketChannel; import java.nio.charset.Charset; @@ -266,8 +265,7 @@ public class GrpcServerImpl extends RPCServer implements CommandServerGrpc.Comma @Override public void serve() throws IOException { Preconditions.checkState(!serving); - InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); - server = NettyServerBuilder.forAddress(new InetSocketAddress(loopbackAddress, port)) + server = NettyServerBuilder.forAddress(new InetSocketAddress("localhost", port)) .addService(CommandServerGrpc.bindService(this)) .build(); @@ -285,11 +283,7 @@ public class GrpcServerImpl extends RPCServer implements CommandServerGrpc.Comma } serving = true; - if (port == 0) { - port = getActualServerPort(); - } - - writeServerFile(PORT_FILE, loopbackAddress.getHostAddress() + ":" + Integer.toString(port)); + writeServerFile(PORT_FILE, getAddressString()); writeServerFile(REQUEST_COOKIE_FILE, requestCookie); writeServerFile(RESPONSE_COOKIE_FILE, responseCookie); @@ -313,12 +307,16 @@ public class GrpcServerImpl extends RPCServer implements CommandServerGrpc.Comma * <p>The implementation is awful, but gRPC doesn't provide an official way to do this: * https://github.com/grpc/grpc-java/issues/72 */ - private int getActualServerPort() { + private String getAddressString() { try { ServerSocketChannel channel = (ServerSocketChannel) getField(server, "transportServer", "channel", "ch"); InetSocketAddress address = (InetSocketAddress) channel.getLocalAddress(); - return address.getPort(); + String host = address.getAddress().getHostAddress(); + if (host.contains(":")) { + host = "[" + host + "]"; + } + return host + ":" + address.getPort(); } catch (IllegalAccessException | NullPointerException | IOException e) { throw new IllegalStateException("Cannot read server socket address from gRPC"); } |