Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netty: improve server handling of writes to reset streams #10258

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Changes from all commits
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
58 changes: 34 additions & 24 deletions netty/src/main/java/io/grpc/netty/NettyServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,7 @@
return serverWriteQueue;
}

/**
* Handler for commands sent from the stream.
*/
/** Handler for commands sent from the stream. */
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
throws Exception {
Expand Down Expand Up @@ -720,31 +718,45 @@
}
}

private void closeStreamWhenDone(ChannelPromise promise, int streamId) throws Http2Exception {
final NettyServerStream.TransportState stream = serverStream(requireHttp2Stream(streamId));
if (stream != null) {
promise.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
stream.complete();
}
});
}
private void closeStreamWhenDone(ChannelPromise promise, Http2Stream stream) {
promise.addListener(
new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) {
serverStream(stream).complete();
}
});
}

/**
* Sends the given gRPC frame to the client.
*/
private void sendGrpcFrame(ChannelHandlerContext ctx, SendGrpcFrameCommand cmd,
ChannelPromise promise) throws Http2Exception {
private static void streamGone(int streamId, ChannelPromise promise) {
promise.setFailure(
new IllegalStateException(
"attempting to write to stream " + streamId + " that no longer exists") {
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
});
}

/** Sends the given gRPC frame to the client. */
private void sendGrpcFrame(
ChannelHandlerContext ctx, SendGrpcFrameCommand cmd, ChannelPromise promise)
throws Http2Exception {
try (TaskCloseable ignore = PerfMark.traceTask("NettyServerHandler.sendGrpcFrame")) {
PerfMark.attachTag(cmd.stream().tag());
PerfMark.linkIn(cmd.getLink());
int streamId = cmd.stream().id();
Http2Stream stream = connection().stream(streamId);
if (stream == null) {
streamGone(streamId, promise);
return;
}
if (cmd.endStream()) {
closeStreamWhenDone(promise, cmd.stream().id());
closeStreamWhenDone(promise, stream);

Check warning on line 756 in netty/src/main/java/io/grpc/netty/NettyServerHandler.java

View check run for this annotation

Codecov / codecov/patch

netty/src/main/java/io/grpc/netty/NettyServerHandler.java#L756

Added line #L756 was not covered by tests
}
// Call the base class to write the HTTP/2 DATA frame.
encoder().writeData(ctx, cmd.stream().id(), cmd.content(), 0, cmd.endStream(), promise);
encoder().writeData(ctx, streamId, cmd.content(), 0, cmd.endStream(), promise);
}
}

Expand All @@ -756,16 +768,14 @@
try (TaskCloseable ignore = PerfMark.traceTask("NettyServerHandler.sendResponseHeaders")) {
PerfMark.attachTag(cmd.stream().tag());
PerfMark.linkIn(cmd.getLink());
// TODO(carl-mastrangelo): remove this check once https://github.com/netty/netty/issues/6296
// is fixed.
int streamId = cmd.stream().id();
Http2Stream stream = connection().stream(streamId);
if (stream == null) {
resetStream(ctx, streamId, Http2Error.CANCEL.code(), promise);
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
streamGone(streamId, promise);
return;
}
if (cmd.endOfStream()) {
closeStreamWhenDone(promise, streamId);
closeStreamWhenDone(promise, stream);
}
encoder().writeHeaders(ctx, streamId, cmd.headers(), 0, cmd.endOfStream(), promise);
}
Expand Down
Loading