mirror of https://github.com/grpc/grpc-java.git
Simplify connection callback handling in Netty
Channel is available immediately after connect(), so register callbacks immediately instead of delaying. Setting channel is now delayed until it is actually safe to use.
This commit is contained in:
parent
e6a7b6c8a9
commit
c84ef8332e
|
|
@ -178,7 +178,8 @@ class NettyClientTransport extends AbstractClientTransport {
|
||||||
b.handler(negotiation.initializer());
|
b.handler(negotiation.initializer());
|
||||||
|
|
||||||
// Start the connection operation to the server.
|
// Start the connection operation to the server.
|
||||||
b.connect(address).addListener(new ChannelFutureListener() {
|
final ChannelFuture connectFuture = b.connect(address);
|
||||||
|
connectFuture.addListener(new ChannelFutureListener() {
|
||||||
@Override
|
@Override
|
||||||
public void operationComplete(ChannelFuture future) throws Exception {
|
public void operationComplete(ChannelFuture future) throws Exception {
|
||||||
if (!future.isSuccess()) {
|
if (!future.isSuccess()) {
|
||||||
|
|
@ -188,16 +189,27 @@ class NettyClientTransport extends AbstractClientTransport {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Connected successfully, start the protocol negotiation.
|
// Connected successfully, start the protocol negotiation.
|
||||||
channel = future.channel();
|
|
||||||
negotiation.onConnected(channel);
|
negotiation.onConnected(channel);
|
||||||
|
}
|
||||||
final ListenableFuture<Void> negotiationFuture = negotiation.completeFuture();
|
});
|
||||||
Futures.addCallback(negotiationFuture, new FutureCallback<Void>() {
|
Futures.addCallback(negotiation.completeFuture(), new FutureCallback<Void>() {
|
||||||
@Override
|
@Override
|
||||||
public void onSuccess(Void result) {
|
public void onSuccess(Void result) {
|
||||||
// The negotiation was successful.
|
// The negotiation was successful.
|
||||||
|
// We should not send on the channel until negotiation completes. This is a hard requirement
|
||||||
|
// by SslHandler but is appropriate for HTTP/1.1 Upgrade as well.
|
||||||
|
channel = connectFuture.channel();
|
||||||
notifyStarted();
|
notifyStarted();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onFailure(Throwable t) {
|
||||||
|
// The negotiation failed.
|
||||||
|
notifyFailed(t);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
Channel channel = connectFuture.channel();
|
||||||
// Handle transport shutdown when the channel is closed.
|
// Handle transport shutdown when the channel is closed.
|
||||||
channel.closeFuture().addListener(new ChannelFutureListener() {
|
channel.closeFuture().addListener(new ChannelFutureListener() {
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -219,16 +231,6 @@ class NettyClientTransport extends AbstractClientTransport {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure(Throwable t) {
|
|
||||||
// The negotiation failed.
|
|
||||||
notifyFailed(t);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doStop() {
|
protected void doStop() {
|
||||||
// No explicit call to notifyStopped() here, since this is automatically done when the
|
// No explicit call to notifyStopped() here, since this is automatically done when the
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue