core: pass down addr to which client is connected

This addresses #2613
This commit is contained in:
Łukasz Strzałkowski 2017-01-26 15:11:39 -08:00 committed by ZHANG Dapeng
parent 89bc2cd3b2
commit aa0391a427
2 changed files with 8 additions and 1 deletions

View File

@ -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 {

View File

@ -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) {