Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions netty/src/main/java/io/grpc/netty/NettyClientTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException;
Expand Down Expand Up @@ -165,14 +166,20 @@ public ClientStream newStream(MethodDescriptor<?, ?> method, Metadata headers) {
public Runnable start(Listener transportListener) {
lifecycleManager = new ClientTransportLifecycleManager(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you forget to replace the lifecycleManager field with a local variable? Ditto for the keepaliveManager field if it is not exposed to test.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lifecycleManager is used in statusFromFailedFuture, so it must remain a field. I left keepaliveManager because it is used by the test... although I agree that is questionable given the test didn't catch this bug.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, I see lifecycleManager is also used in statusFromFailedFuture. But why did you add lifecycleManager as an extra argument of newHandler?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought my comment explained that:

Only use fields initialized before start()

Since lifecycleManager is initialized in start(), the flow in start() is more obvious when being passed as an argument. Otherwise you could move newHandler() within start() and not notice that a field is no longer initialized.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I think move it inside start() is cleaner since it's only one line of code and used only once.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I think move it inside start() is cleaner since it's only one line of code and used only once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done.

Preconditions.checkNotNull(transportListener, "listener"));
EventLoop eventLoop = group.next();
if (enableKeepAlive) {
keepAliveManager = new KeepAliveManager(
new ClientKeepAlivePinger(this), eventLoop, keepAliveDelayNanos, keepAliveTimeoutNanos,
false);
}

handler = newHandler();
handler = newHandler(lifecycleManager, keepAliveManager);
HandlerSettings.setAutoWindow(handler);

negotiationHandler = negotiator.newHandler(handler);

Bootstrap b = new Bootstrap();
b.group(group);
b.group(eventLoop);
b.channel(channelType);
if (NioSocketChannel.class.isAssignableFrom(channelType)) {
b.option(SO_KEEPALIVE, true);
Expand Down Expand Up @@ -231,10 +238,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
}
});

if (enableKeepAlive) {
keepAliveManager = new KeepAliveManager(
new ClientKeepAlivePinger(this), channel.eventLoop(), keepAliveDelayNanos,
keepAliveTimeoutNanos, false);
if (keepAliveManager != null) {
keepAliveManager.onTransportStarted();
}

Expand Down Expand Up @@ -308,7 +312,10 @@ private Status statusFromFailedFuture(ChannelFuture f) {
return Utils.statusFromThrowable(t);
}

private NettyClientHandler newHandler() {
private NettyClientHandler newHandler(ClientTransportLifecycleManager lifecycleManager,
KeepAliveManager keepAliveManager) {
// Only use fields initialized before start(); have fields initialized in start() be passed in.
// Otherwise it is hard to ensure the fields are initialized in time.
return NettyClientHandler.newHandler(lifecycleManager, keepAliveManager, flowControlWindow,
maxHeaderListSize, Ticker.systemTicker());
}
Expand Down