Skip to content

Commit

Permalink
fix StreamBufferingEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
dapengzhang0 committed Mar 26, 2021
1 parent 6408d66 commit 2b37b39
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions netty/src/main/java/io/grpc/netty/StreamBufferingEncoder.java
Expand Up @@ -42,9 +42,29 @@
import java.util.TreeMap;

/**
* This is a temporary copy of {@link io.netty.handler.codec.http2.DecoratingHttp2ConnectionEncoder}
* with a bug fix that is not available yet in the latest netty release.
* Implementation of a {@link Http2ConnectionEncoder} that dispatches all method call to another
* {@link Http2ConnectionEncoder}, until {@code SETTINGS_MAX_CONCURRENT_STREAMS} is reached.
* <p/>
* <p>When this limit is hit, instead of rejecting any new streams this implementation buffers newly
* created streams and their corresponding frames. Once an active stream gets closed or the maximum
* number of concurrent streams is increased, this encoder will automatically try to empty its
* buffer and create as many new streams as possible.
* <p/>
* <p>
* If a {@code GOAWAY} frame is received from the remote endpoint, all buffered writes for streams
* with an ID less than the specified {@code lastStreamId} will immediately fail with a
* {@link io.netty.handler.codec.http2.StreamBufferingEncoder.Http2GoAwayException}.
* <p/>
* <p>
* If the channel/encoder gets closed, all new and buffered writes will immediately fail with a
* {@link io.netty.handler.codec.http2.StreamBufferingEncoder.Http2ChannelClosedException}.
* </p>
* <p>This implementation makes the buffering mostly transparent and is expected to be used as a
* drop-in decorator of {@link io.netty.handler.codec.http2.DefaultHttp2ConnectionEncoder}.
* </p>
*/
// This is a temporary copy of {@link io.netty.handler.codec.http2.DecoratingHttp2ConnectionEncoder}
// with a bug fix that is not available yet in the latest netty release.
class StreamBufferingEncoder extends DecoratingHttp2ConnectionEncoder {

/**
Expand Down Expand Up @@ -138,11 +158,7 @@ public ChannelFuture writeHeaders(ChannelHandlerContext ctx, int streamId, Http2
if (closed) {
return promise.setFailure(new Http2ChannelClosedException());
}
if (isExistingStream(streamId) || connection().goAwayReceived()) {
return super.writeHeaders(ctx, streamId, headers, streamDependency, weight,
exclusive, padding, endOfStream, promise);
}
if (canCreateStream()) {
if (isExistingStream(streamId) || canCreateStream()) {
return super.writeHeaders(ctx, streamId, headers, streamDependency, weight,
exclusive, padding, endOfStream, promise);
}
Expand Down

0 comments on commit 2b37b39

Please sign in to comment.