From b769ec09346e227b9e33d581f26e2cb0b22911a7 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Tue, 23 Jan 2018 09:15:37 +0100 Subject: [PATCH] Reduce overhead of cancel flowcontrolled writes. Motivation: When we cancel the flowcontrolled writes we did create a new StreamException for each write that was enqueued. Creating Exceptions is very expensive due of filling the stacktrace. Modifications: Only create the StreamException once and reuse the same for all the flowcontrolled writes (per stream). Result: Less expensive to cancel flowcontrolled writes. --- .../http2/DefaultHttp2RemoteFlowController.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2RemoteFlowController.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2RemoteFlowController.java index 6e02e7cecf..9f2875036a 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2RemoteFlowController.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2RemoteFlowController.java @@ -480,13 +480,15 @@ public class DefaultHttp2RemoteFlowController implements Http2RemoteFlowControll return; } - for (;;) { - FlowControlled frame = pendingWriteQueue.poll(); - if (frame == null) { - break; - } - writeError(frame, streamError(stream.id(), INTERNAL_ERROR, cause, - "Stream closed before write could take place")); + FlowControlled frame = pendingWriteQueue.poll(); + if (frame != null) { + // Only create exception once and reuse to reduce overhead of filling in the stacktrace. + final Http2Exception exception = streamError(stream.id(), INTERNAL_ERROR, cause, + "Stream closed before write could take place"); + do { + writeError(frame, exception); + frame = pendingWriteQueue.poll(); + } while (frame != null); } streamByteDistributor.updateStreamableBytes(this);