From aa0391a4272410f373ec452a6a2a301cbbfba8dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Strza=C5=82kowski?= Date: Thu, 26 Jan 2017 15:11:39 -0800 Subject: [PATCH] core: pass down addr to which client is connected This addresses #2613 --- netty/src/main/java/io/grpc/netty/ProtocolNegotiators.java | 6 ++++++ .../test/java/io/grpc/netty/NettyClientTransportTest.java | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) 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) {