From f6c1c0ea9cbb58c1a7b210998774fb69e917fde0 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sat, 16 Jan 2021 09:55:26 +0100 Subject: [PATCH] Use GracefulShutdown when stream space is exhausted (#10946) Motivation: We should use GracefulShutdown when we try to create a stream and fail it because the stream space is exhausted as we may still want to process the active streams. Modifications: - Use graceful shutdown - Add unit test Result: More graceful handling of stream creation failure due stream space exhaustation --- .../handler/codec/http2/DefaultHttp2Connection.java | 5 ++++- .../codec/http2/DefaultHttp2ConnectionTest.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Connection.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Connection.java index 957f2c8b8e..7c2a62bb5c 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Connection.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/DefaultHttp2Connection.java @@ -886,7 +886,10 @@ public class DefaultHttp2Connection implements Http2Connection { streamId, nextStreamIdToCreate); } if (nextStreamIdToCreate <= 0) { - throw connectionError(REFUSED_STREAM, "Stream IDs are exhausted for this endpoint."); + // We exhausted the stream id space that we can use. Let's signal this back but also signal that + // we still may want to process active streams. + throw new Http2Exception(REFUSED_STREAM, "Stream IDs are exhausted for this endpoint.", + Http2Exception.ShutdownHint.GRACEFUL_SHUTDOWN); } boolean isReserved = state == RESERVED_LOCAL || state == RESERVED_REMOTE; if (!isReserved && !canOpenStream() || isReserved && numStreams >= maxStreams) { diff --git a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionTest.java b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionTest.java index 9cc072e199..777bea5957 100644 --- a/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionTest.java +++ b/codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionTest.java @@ -351,6 +351,18 @@ public class DefaultHttp2ConnectionTest { incrementAndGetStreamShouldRespectOverflow(client.local(), MAX_VALUE); } + @Test + public void clientLocalCreateStreamExhaustedSpace() throws Http2Exception { + client.local().createStream(MAX_VALUE, true); + try { + client.local().createStream(MAX_VALUE, true); + fail(); + } catch (Http2Exception expected) { + assertEquals(Http2Error.REFUSED_STREAM, expected.error()); + assertEquals(Http2Exception.ShutdownHint.GRACEFUL_SHUTDOWN, expected.shutdownHint()); + } + } + @Test(expected = Http2Exception.class) public void newStreamBehindExpectedShouldThrow() throws Http2Exception { server.local().createStream(0, true);