Skip to content

Commit 017cdd2

Browse files
committed
netty: Remove connectionError and just use goAwayStatus
When connectionError was set, goAwayStatus was also set, so we shouldn't lose any errors. NettyClientTransport doesn't really need a Throwable, it just needs a Status. Passing a Status out of NettyClientHandler reduces the number of places that need to do transport-specific translation of Throwables into Status codes.
1 parent e1c348c commit 017cdd2

3 files changed

Lines changed: 21 additions & 28 deletions

File tree

netty/src/main/java/io/grpc/transport/netty/NettyClientHandler.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ class NettyClientHandler extends Http2ConnectionHandler {
8888
private WriteQueue clientWriteQueue;
8989
private int flowControlWindow;
9090
private Http2Settings initialSettings = new Http2Settings();
91-
private Throwable connectionError;
9291
private Http2Ping ping;
9392
private Status goAwayStatus;
9493
private ChannelHandlerContext ctx;
@@ -132,8 +131,8 @@ public void onGoAwayReceived(int lastStreamId, long errorCode, ByteBuf debugData
132131
}
133132

134133
@Nullable
135-
public Throwable connectionError() {
136-
return connectionError;
134+
public Status errorStatus() {
135+
return goAwayStatus;
137136
}
138137

139138
@Override
@@ -253,9 +252,7 @@ protected void onConnectionError(ChannelHandlerContext ctx, Throwable cause,
253252
Http2Exception http2Ex) {
254253
logger.log(Level.FINE, "Caught a connection error", cause);
255254

256-
// Save the error.
257-
connectionError = cause;
258-
goAwayStatus(Status.fromThrowable(connectionError));
255+
goAwayStatus(Status.fromThrowable(cause));
259256
cancelPing();
260257

261258
super.onConnectionError(ctx, cause, http2Ex);
@@ -429,9 +426,7 @@ private void cancelPing() {
429426
}
430427

431428
private Throwable getPingFailure() {
432-
if (connectionError != null) {
433-
return connectionError;
434-
} else if (goAwayStatus != null) {
429+
if (goAwayStatus != null) {
435430
return goAwayStatus.asException();
436431
} else {
437432
return Status.UNAVAILABLE.withDescription("Connection closed").asException();

netty/src/main/java/io/grpc/transport/netty/NettyClientTransport.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,13 @@
6565
import java.net.InetSocketAddress;
6666
import java.net.SocketAddress;
6767
import java.util.concurrent.Executor;
68-
import java.util.logging.Level;
69-
import java.util.logging.Logger;
7068

7169
import javax.annotation.concurrent.GuardedBy;
7270

7371
/**
7472
* A Netty-based {@link ClientTransport} implementation.
7573
*/
7674
class NettyClientTransport implements ClientTransport {
77-
private static final Logger log = Logger.getLogger(NettyClientTransport.class.getName());
78-
7975
private final SocketAddress address;
8076
private final Class<? extends Channel> channelType;
8177
private final EventLoopGroup group;
@@ -183,34 +179,36 @@ public void operationComplete(ChannelFuture future) throws Exception {
183179
} else {
184180
// Need to notify of this failure, because handler.connectionError() is not guaranteed to
185181
// have seen this cause.
186-
notifyTerminated(future.cause());
182+
notifyTerminated(Status.fromThrowable(future.cause()));
187183
}
188184
}
189185
});
190186
// Handle transport shutdown when the channel is closed.
191187
channel.closeFuture().addListener(new ChannelFutureListener() {
192188
@Override
193189
public void operationComplete(ChannelFuture future) throws Exception {
194-
notifyTerminated(handler.connectionError());
190+
Status status = handler.errorStatus();
191+
if (status == null) {
192+
// We really only expect this to happen if shutdown() was called, but in that case this
193+
// status is ignored.
194+
status = Status.INTERNAL.withDescription("Connection closed with unknown cause");
195+
}
196+
notifyTerminated(status);
195197
}
196198
});
197199
}
198200

199201
@Override
200202
public void shutdown() {
201-
notifyShutdown(null);
203+
notifyShutdown(Status.OK.withDescription("Channel requested transport to shut down"));
202204
// Notifying of termination is automatically done when the channel closes.
203205
if (channel != null && channel.isOpen()) {
204206
channel.close();
205207
}
206208
}
207209

208-
private void notifyShutdown(Throwable t) {
209-
Status s = Status.OK.withDescription("Transport is shutting down");
210-
if (t != null) {
211-
log.log(Level.SEVERE, "Transport failed", t);
212-
s = Status.INTERNAL.withCause(t).withDescription("Transport failed");
213-
}
210+
private void notifyShutdown(Status status) {
211+
Preconditions.checkNotNull(status, "status");
214212
boolean notifyShutdown;
215213
synchronized (this) {
216214
notifyShutdown = !shutdown;
@@ -219,12 +217,12 @@ private void notifyShutdown(Throwable t) {
219217
}
220218
}
221219
if (notifyShutdown) {
222-
listener.transportShutdown(s);
220+
listener.transportShutdown(status);
223221
}
224222
}
225223

226-
private void notifyTerminated(Throwable t) {
227-
notifyShutdown(t);
224+
private void notifyTerminated(Status status) {
225+
notifyShutdown(status);
228226
boolean notifyTerminated;
229227
synchronized (this) {
230228
notifyTerminated = !terminated;

netty/src/test/java/io/grpc/transport/netty/NettyClientHandlerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,11 +522,11 @@ public void ping_failsOnConnectionError() throws Exception {
522522
sendPing(callback, MoreExecutors.directExecutor());
523523
assertEquals(0, callback.invocationCount);
524524

525-
Throwable connectionError = new Throwable();
526-
handler.onConnectionError(ctx, connectionError, null);
525+
Status failureStatus = Status.INTERNAL.withDescription("forced failure");
526+
handler.onConnectionError(ctx, failureStatus.asRuntimeException(), null);
527527
// ping failed on connection error
528528
assertEquals(1, callback.invocationCount);
529-
assertSame(connectionError, callback.failureCause);
529+
assertSame(failureStatus, Status.fromThrowable(callback.failureCause));
530530
}
531531

532532
private void sendPing(PingCallback callback, Executor executor) {

0 commit comments

Comments
 (0)