diff --git a/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java b/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java index 72c316d2ed..0c77024cf2 100644 --- a/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java +++ b/netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java @@ -91,6 +91,10 @@ public final class ProtocolNegotiators { class PlaintextHandler extends ChannelHandlerAdapter implements Handler { @Override public void handlerAdded(ChannelHandlerContext ctx) throws Exception { + // Set sttributes before replace to be sure we pass it before accepting any requests. + handler.handleProtocolNegotiationCompleted(Attributes.newBuilder() + .set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, ctx.channel().remoteAddress()) + .build()); // Just replace this handler with the gRPC handler. ctx.pipeline().replace(this, null, handler); } @@ -155,6 +159,7 @@ public final class ProtocolNegotiators { Attributes.newBuilder() .set(Grpc.TRANSPORT_ATTR_SSL_SESSION, sslHandler(ctx.pipeline()).engine().getSession()) + .set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, ctx.channel().remoteAddress()) .build()); // Replace this handler with the GRPC handler. ctx.pipeline().replace(this, null, grpcHandler); @@ -509,6 +514,7 @@ public final class ProtocolNegotiators { grpcHandler.handleProtocolNegotiationCompleted( Attributes.newBuilder() .set(Grpc.TRANSPORT_ATTR_SSL_SESSION, handler.engine().getSession()) + .set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, ctx.channel().remoteAddress()) .build()); writeBufferedAndRemove(ctx); } else { diff --git a/netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java b/netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java index 2a220b8814..e99fa859d3 100644 --- a/netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java +++ b/netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java @@ -335,7 +335,7 @@ public class NettyClientTransportTest { } @Test - public void clientStreamGetsSslSessionAttributes() throws Exception { + public void clientStreamGetsAttributes() throws Exception { startServer(); NettyClientTransport transport = newTransport(newNegotiator()); transport.start(clientTransportListener); @@ -343,6 +343,7 @@ public class NettyClientTransportTest { rpc.waitForResponse(); assertNotNull(rpc.stream.getAttributes().get(Grpc.TRANSPORT_ATTR_SSL_SESSION)); + assertEquals(address, rpc.stream.getAttributes().get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)); } private Throwable getRootCause(Throwable t) {