Source:
<code>private void writeHeadersFrame( final ChannelHandlerContext ctx, Http2HeadersFrame headersFrame, final ChannelPromise promise) { if (isStreamIdValid(headersFrame.stream().id())) { encoder().writeHeaders(ctx, headersFrame.stream().id(), headersFrame.headers(), headersFrame.padding(), headersFrame.isEndStream(), promise); } else { final DefaultHttp2FrameStream stream = (DefaultHttp2FrameStream) headersFrame.stream(); final Http2Connection connection = connection(); final int streamId = connection.local().incrementAndGetNextStreamId(); if (streamId < 0) { promise.setFailure(new Http2NoMoreStreamIdsException()); onHttp2Frame(ctx, new DefaultHttp2GoAwayFrame(connection.isServer() ? Integer.MAX_VALUE : Integer.MAX_VALUE - 1, NO_ERROR.code(), writeAscii(ctx.alloc(), "Stream IDs exhausted on local stream creation"))); return; } stream.id = streamId; Object old = frameStreamToInitializeMap.put(streamId, stream); assert old == null; ChannelPromise writeHeadersPromise = ctx.newPromise(); writeHeadersPromise.addListener(new GenericFutureListener<Future<Void>>() { @Override public void operationComplete(Future<Void> operationFuture) { <START> if (!operationFuture.isSuccess()) { <END> frameStreamToInitializeMap.remove(streamId); promise.setFailure(operationFuture.cause()); } else { promise.setSuccess(operationFuture.getNow()); } } }); encoder().writeHeaders(ctx, streamId, headersFrame.headers(), headersFrame.padding(), headersFrame.isEndStream(), writeHeadersPromise); if (!promise.isDone()) { numBufferedStreams++; promise.addListener(bufferedStreamsListener); } } }</code><technical_language>nit: reduce volatile reads done internally future using: java Throwable = operationFuture.cause(); if (cause != null) { frameStreamToInitializeMap.remove(streamId); promise.setFailure(cause); } else { promise.setSuccess(operationFuture.getNow()); }</technical_language>

Target:
private void writeHeadersFrame( final ChannelHandlerContext ctx, Http2HeadersFrame headersFrame, final ChannelPromise promise) { if (isStreamIdValid(headersFrame.stream().id())) { encoder().writeHeaders(ctx, headersFrame.stream().id(), headersFrame.headers(), headersFrame.padding(), headersFrame.isEndStream(), promise); } else { final DefaultHttp2FrameStream stream = (DefaultHttp2FrameStream) headersFrame.stream(); final Http2Connection connection = connection(); final int streamId = connection.local().incrementAndGetNextStreamId(); if (streamId < 0) { promise.setFailure(new Http2NoMoreStreamIdsException()); onHttp2Frame(ctx, new DefaultHttp2GoAwayFrame(connection.isServer() ? Integer.MAX_VALUE : Integer.MAX_VALUE - 1, NO_ERROR.code(), writeAscii(ctx.alloc(), "Stream IDs exhausted on local stream creation"))); return; } stream.id = streamId; Object old = frameStreamToInitializeMap.put(streamId, stream); assert old == null; promise.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) { if (!channelFuture.isSuccess()) { frameStreamToInitializeMap.remove(streamId); } } }); encoder().writeHeaders(ctx, streamId, headersFrame.headers(), headersFrame.padding(), headersFrame.isEndStream(), promise); if (!promise.isDone()) { numBufferedStreams++; promise.addListener(bufferedStreamsListener); } } }