GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge commit from fork · lltopk/netty@fd6f314

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http2/src
    • main/java/io/netty/handler/codec/http2
      • AbstractHttp2ConnectionHandlerBuilder.java
    • test/java/io/netty/handler/codec/http2
      • DefaultHttp2ConnectionTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http2/src/main/java/io/netty/handler/codec/http2/AbstractHttp2ConnectionHandlerBuilder.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/main/java/io/netty/handler/codec/http2/AbstractHttp2ConnectionHandlerBuilder.java
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ protected T build() {
593593
}
594594

595595
private T buildFromConnection(Http2Connection connection) {
596+
// Enforce the advertised maxConcurrentStreams limit on the remote endpoint immediately,
597+
// without waiting for the SETTINGS_ACK round-trip.
598+
enforceMaxActiveStreams(connection, initialSettings);
599+
596600
Long maxHeaderListSize = initialSettings.maxHeaderListSize();
597601
Http2FrameReader reader = new DefaultHttp2FrameReader(new DefaultHttp2HeadersDecoder(isValidateHeaders(),
598602
maxHeaderListSize == null ? DEFAULT_HEADER_LIST_SIZE : maxHeaderListSize,
@@ -644,6 +648,10 @@ private T buildFromConnection(Http2Connection connection) {
644648
}
645649

646650
private T buildFromCodec(Http2ConnectionDecoder decoder, Http2ConnectionEncoder encoder) {
651+
// Enforce the advertised maxConcurrentStreams limit on the remote endpoint immediately,
652+
// without waiting for the SETTINGS_ACK round-trip.
653+
enforceMaxActiveStreams(encoder.connection(), initialSettings);
654+
647655
int maxConsecutiveEmptyDataFrames = decoderEnforceMaxConsecutiveEmptyDataFrames();
648656
if (maxConsecutiveEmptyDataFrames > 0) {
649657
decoder = new Http2EmptyDataFrameConnectionDecoder(decoder, maxConsecutiveEmptyDataFrames);
@@ -680,6 +688,13 @@ private T buildFromCodec(Http2ConnectionDecoder decoder, Http2ConnectionEncoder
680688
return handler;
681689
}
682690

691+
private static void enforceMaxActiveStreams(Http2Connection connection, Http2Settings initialSettings) {
692+
Long maxConcurrentStreams = initialSettings.maxConcurrentStreams();
693+
if (maxConcurrentStreams != null) {
694+
connection.remote().maxActiveStreams((int) Math.min(maxConcurrentStreams, Integer.MAX_VALUE));
695+
}
696+
}
697+
683698
/**
684699
* Implement this method to create a new {@link Http2ConnectionHandler} or its subtype instance.
685700
* <p>
Collapse file

‎codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionTest.java‎

Copy file name to clipboardExpand all lines: codec-http2/src/test/java/io/netty/handler/codec/http2/DefaultHttp2ConnectionTest.java
+180Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.netty.buffer.Unpooled;
2020
import io.netty.channel.EventLoopGroup;
2121
import io.netty.channel.MultiThreadIoEventLoopGroup;
22+
import io.netty.channel.embedded.EmbeddedChannel;
2223
import io.netty.channel.local.LocalIoHandler;
2324
import io.netty.handler.codec.http2.Http2Connection.Endpoint;
2425
import io.netty.handler.codec.http2.Http2Stream.State;
@@ -34,11 +35,13 @@
3435
import org.mockito.invocation.InvocationOnMock;
3536
import org.mockito.stubbing.Answer;
3637

38+
import javax.annotation.Nonnull;
3739
import java.util.concurrent.CountDownLatch;
3840
import java.util.concurrent.TimeUnit;
3941

4042
import static java.lang.Integer.MAX_VALUE;
4143
import static org.junit.jupiter.api.Assertions.assertEquals;
44+
import static org.junit.jupiter.api.Assertions.assertFalse;
4245
import static org.junit.jupiter.api.Assertions.assertNotNull;
4346
import static org.junit.jupiter.api.Assertions.assertNull;
4447
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -700,6 +703,183 @@ public void execute() throws Throwable {
700703
});
701704
}
702705

706+
@Test
707+
public void defaultSettingsShouldEnforceMaxConcurrentStreamsOnRemoteEndpoint() throws Exception {
708+
// Build a server handler using default settings (no explicit maxConcurrentStreams override).
709+
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
710+
.frameListener(new Http2FrameAdapter())
711+
.build();
712+
713+
EmbeddedChannel channel = new EmbeddedChannel(handler);
714+
try {
715+
// Feed the client connection preface.
716+
assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()));
717+
718+
ByteBuf clientSettings = clientSettingsWithoutMaxConcurrentStreams();
719+
assertFalse(channel.writeInbound(clientSettings));
720+
721+
Http2Connection connection = handler.connection();
722+
723+
// The server's default (SMALLEST_MAX_CONCURRENT_STREAMS = 100) must be
724+
// enforced on the remote endpoint — the one that tracks client-initiated streams.
725+
assertEquals(Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS,
726+
connection.remote().maxActiveStreams());
727+
728+
// Create exactly the maximum allowed client-initiated (odd-numbered) streams.
729+
for (int id = 1; id < Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS * 2; id += 2) {
730+
connection.remote().createStream(id, true);
731+
}
732+
assertEquals(Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS,
733+
connection.numActiveStreams());
734+
735+
// The next stream must be refused.
736+
final int nextStreamId = Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS * 2 + 1;
737+
Http2Exception e = assertThrows(Http2Exception.class, new Executable() {
738+
@Override
739+
public void execute() throws Throwable {
740+
handler.connection().remote().createStream(nextStreamId, true);
741+
}
742+
});
743+
assertEquals(Http2Error.REFUSED_STREAM, e.error());
744+
} finally {
745+
channel.finishAndReleaseAll();
746+
}
747+
}
748+
749+
@Test
750+
public void customMaxConcurrentStreamsShouldBeEnforcedOnRemoteEndpoint() throws Exception {
751+
final int maxConcurrentStreams = 150;
752+
753+
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
754+
.frameListener(new Http2FrameAdapter())
755+
.initialSettings(new Http2Settings().maxConcurrentStreams(maxConcurrentStreams))
756+
.build();
757+
758+
EmbeddedChannel channel = new EmbeddedChannel(handler);
759+
try {
760+
assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()));
761+
762+
ByteBuf clientSettings = clientSettingsWithoutMaxConcurrentStreams();
763+
assertFalse(channel.writeInbound(clientSettings));
764+
765+
Http2Connection connection = handler.connection();
766+
767+
assertEquals(maxConcurrentStreams, connection.remote().maxActiveStreams());
768+
769+
// Create exactly the configured limit of client-initiated streams.
770+
for (int id = 1; id < maxConcurrentStreams * 2; id += 2) {
771+
connection.remote().createStream(id, true);
772+
}
773+
assertEquals(maxConcurrentStreams, connection.numActiveStreams());
774+
775+
// The next stream must be refused.
776+
final int nextStreamId = maxConcurrentStreams * 2 + 1;
777+
Http2Exception e = assertThrows(Http2Exception.class, new Executable() {
778+
@Override
779+
public void execute() throws Throwable {
780+
handler.connection().remote().createStream(nextStreamId, true);
781+
}
782+
});
783+
assertEquals(Http2Error.REFUSED_STREAM, e.error());
784+
} finally {
785+
channel.finishAndReleaseAll();
786+
}
787+
}
788+
789+
@Test
790+
public void defaultSettingsShouldEnforceMaxConcurrentStreamsOnRemoteEndpointWithCodec()
791+
throws Exception {
792+
DefaultHttp2Connection connection = new DefaultHttp2Connection(true);
793+
DefaultHttp2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
794+
Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
795+
Http2ConnectionDecoder decoder =
796+
new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader());
797+
798+
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
799+
.frameListener(new Http2FrameAdapter())
800+
.codec(decoder, encoder)
801+
.build();
802+
803+
EmbeddedChannel channel = new EmbeddedChannel(handler);
804+
try {
805+
assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()));
806+
assertFalse(channel.writeInbound(clientSettingsWithoutMaxConcurrentStreams()));
807+
808+
assertEquals(Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS,
809+
connection.remote().maxActiveStreams());
810+
811+
for (int id = 1; id < Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS * 2; id += 2) {
812+
connection.remote().createStream(id, true);
813+
}
814+
assertEquals(Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS, connection.numActiveStreams());
815+
816+
final int nextStreamId = Http2CodecUtil.SMALLEST_MAX_CONCURRENT_STREAMS * 2 + 1;
817+
Http2Exception e = assertThrows(Http2Exception.class, new Executable() {
818+
@Override
819+
public void execute() throws Throwable {
820+
connection.remote().createStream(nextStreamId, true);
821+
}
822+
});
823+
assertEquals(Http2Error.REFUSED_STREAM, e.error());
824+
} finally {
825+
channel.finishAndReleaseAll();
826+
}
827+
}
828+
829+
@Test
830+
public void customMaxConcurrentStreamsShouldBeEnforcedOnRemoteEndpointWithCodec()
831+
throws Exception {
832+
final int maxConcurrentStreams = 150;
833+
834+
DefaultHttp2Connection connection = new DefaultHttp2Connection(true);
835+
DefaultHttp2FrameWriter frameWriter = new DefaultHttp2FrameWriter();
836+
Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
837+
Http2ConnectionDecoder decoder =
838+
new DefaultHttp2ConnectionDecoder(connection, encoder, new DefaultHttp2FrameReader());
839+
840+
Http2ConnectionHandler handler = new Http2ConnectionHandlerBuilder()
841+
.frameListener(new Http2FrameAdapter())
842+
.initialSettings(new Http2Settings().maxConcurrentStreams(maxConcurrentStreams))
843+
.codec(decoder, encoder)
844+
.build();
845+
846+
EmbeddedChannel channel = new EmbeddedChannel(handler);
847+
try {
848+
assertFalse(channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()));
849+
assertFalse(channel.writeInbound(clientSettingsWithoutMaxConcurrentStreams()));
850+
851+
assertEquals(maxConcurrentStreams, connection.remote().maxActiveStreams());
852+
853+
for (int id = 1; id < maxConcurrentStreams * 2; id += 2) {
854+
connection.remote().createStream(id, true);
855+
}
856+
assertEquals(maxConcurrentStreams, connection.numActiveStreams());
857+
858+
final int nextStreamId = maxConcurrentStreams * 2 + 1;
859+
Http2Exception e = assertThrows(Http2Exception.class, new Executable() {
860+
@Override
861+
public void execute() throws Throwable {
862+
connection.remote().createStream(nextStreamId, true);
863+
}
864+
});
865+
assertEquals(Http2Error.REFUSED_STREAM, e.error());
866+
} finally {
867+
channel.finishAndReleaseAll();
868+
}
869+
}
870+
871+
@Nonnull
872+
private static ByteBuf clientSettingsWithoutMaxConcurrentStreams() {
873+
ByteBuf clientSettings = Unpooled.buffer();
874+
clientSettings.writeMedium(6); // Payload length: one 6-byte setting
875+
clientSettings.writeByte(0x4); // Frame type: SETTINGS
876+
clientSettings.writeByte(0x0); // Flags
877+
clientSettings.writeInt(0x0); // Stream 0
878+
clientSettings.writeShort(0x4); // SETTINGS_INITIAL_WINDOW_SIZE
879+
clientSettings.writeInt(65535);
880+
return clientSettings;
881+
}
882+
703883
private static void incrementAndGetStreamShouldSucceed(Endpoint<?> endpoint) throws Http2Exception {
704884
Http2Stream streamA = endpoint.createStream(endpoint.incrementAndGetNextStreamId(), true);
705885
Http2Stream streamB = endpoint.createStream(streamA.id() + 2, true);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.