You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2025/09/13 04:59:20 UTC
(httpcomponents-core) 02/02: RFC 9113 conformance: use command queue to activate streams reserved with a push promise; improved test cases
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch rfc_9113_conformance
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
commit 24e1c3b354bd3ca59b973deffaca4385104dcc63
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Tue Sep 9 14:06:28 2025 +0200
RFC 9113 conformance: use command queue to activate streams reserved with a push promise; improved test cases
---
.../impl/nio/AbstractH2StreamMultiplexer.java | 31 +++-
.../apache/hc/core5/http2/impl/nio/H2Stream.java | 6 +-
.../apache/hc/core5/http2/impl/nio/H2Streams.java | 12 +-
.../http2/nio/command/PushResponseCommand.java | 65 +++++++++
.../hc/core5/testing/nio/H2IntegrationTest.java | 159 ++++++++++++---------
5 files changed, 193 insertions(+), 80 deletions(-)
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
index 3d35f6655..99b6c7491 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java
@@ -82,6 +82,7 @@
import org.apache.hc.core5.http2.impl.BasicH2TransportMetrics;
import org.apache.hc.core5.http2.nio.AsyncPingHandler;
import org.apache.hc.core5.http2.nio.command.PingCommand;
+import org.apache.hc.core5.http2.nio.command.PushResponseCommand;
import org.apache.hc.core5.io.CloseMode;
import org.apache.hc.core5.reactor.Command;
import org.apache.hc.core5.reactor.ProtocolIOSession;
@@ -499,7 +500,7 @@ public final void onOutput() throws HttpException, IOException {
}
if (connState.compareTo(ConnectionHandshake.ACTIVE) <= 0 && remoteSettingState == SettingsHandshake.ACKED) {
- while (streams.size() < remoteConfig.getMaxConcurrentStreams()) {
+ while (streams.getLocalCount() < remoteConfig.getMaxConcurrentStreams()) {
final Command command = ioSession.poll();
if (command == null) {
break;
@@ -510,6 +511,8 @@ public final void onOutput() throws HttpException, IOException {
executePing((PingCommand) command);
} else if (command instanceof RequestExecutionCommand) {
executeRequest((RequestExecutionCommand) command);
+ } else if (command instanceof PushResponseCommand) {
+ executePush((PushResponseCommand) command);
}
if (!outputQueue.isEmpty()) {
return;
@@ -638,6 +641,23 @@ private void executeRequest(final RequestExecutionCommand requestExecutionComman
}
}
+ private void executePush(final PushResponseCommand pushResponseCommand) throws IOException, HttpException {
+ if (pushResponseCommand.isCancelled()) {
+ return;
+ }
+ final H2Stream stream = streams.lookupSeen(pushResponseCommand.getStreamId());
+ if (stream != null && stream.isReserved()) {
+ if (!stream.isLocalClosed()) {
+ stream.activate();
+ if (stream.isOutputReady()) {
+ stream.produceOutput();
+ }
+ } else {
+ stream.abort();
+ }
+ }
+ }
+
public final void onException(final Exception cause) {
try {
for (;;) {
@@ -812,7 +832,7 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
if (streamId == 0) {
throw new H2ConnectionException(H2Error.PROTOCOL_ERROR, "Illegal stream id: " + streamId);
}
- final H2Stream stream = streams.lookupValidOrNull(streamId);
+ final H2Stream stream = streams.lookupSeen(streamId);
if (stream != null) {
final ByteBuffer payload = frame.getPayload();
if (payload == null || payload.remaining() != 4) {
@@ -1247,7 +1267,8 @@ void appendState(final StringBuilder buf) {
.append(", connInputWindow=").append(connInputWindow)
.append(", connOutputWindow=").append(connOutputWindow)
.append(", outputQueue=").append(outputQueue.size())
- .append(", streams.size=").append(streams.size())
+ .append(", streams.localCoubt=").append(streams.getLocalCount())
+ .append(", streams.remoteCount=").append(streams.getRemoteCount())
.append(", streams.lastLocal=").append(streams.getLastLocalId())
.append(", streams.lastRemote=").append(streams.getLastRemoteId());
}
@@ -1362,9 +1383,11 @@ public void push(final List<Header> headers, final AsyncPushProducer pushProduce
ensureNotClosed();
final int promisedStreamId = streams.generateStreamId();
final H2StreamChannel channel = createChannel(promisedStreamId);
- streams.createReserved(channel, outgoingPushPromise(channel, pushProducer));
+ final H2Stream stream = streams.createReserved(channel, outgoingPushPromise(channel, pushProducer));
commitPushPromise(id, promisedStreamId, headers);
+ stream.markRemoteClosed();
+ submitCommand(new PushResponseCommand(promisedStreamId));
} finally {
ioSession.getLock().unlock();
}
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
index 7ec30a035..676d929e9 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Stream.java
@@ -120,6 +120,10 @@ boolean isRemoteClosed() {
return remoteClosed;
}
+ void markRemoteClosed() {
+ remoteClosed = true;
+ }
+
boolean isLocalClosed() {
return channel.isLocalClosed();
}
@@ -170,7 +174,7 @@ void consumeData(final ByteBuffer src, final boolean endOfStream) throws HttpExc
}
boolean isOutputReady() {
- return !channel.isLocalClosed() && handler.isOutputReady();
+ return !reserved && !channel.isLocalClosed() && handler.isOutputReady();
}
void produceOutput() throws HttpException, IOException {
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
index 62b348e4c..f602ebf98 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2Streams.java
@@ -60,10 +60,6 @@ public H2Streams(final StreamIdGenerator idGenerator) {
this.remoteCount = new AtomicInteger(0);
}
- public int size() {
- return streams.size();
- }
-
public boolean isEmpty() {
return streams.isEmpty();
}
@@ -80,6 +76,14 @@ public int getLastRemoteId() {
return lastRemoteId.get();
}
+ public int getLocalCount() {
+ return localCount.get();
+ }
+
+ public int getRemoteCount() {
+ return remoteCount.get();
+ }
+
private H2Stream createStream(final H2StreamChannel channel, final H2StreamHandler streamHandler) {
final int streamId = channel.getId();
final boolean remoteStream = isOtherSide(streamId);
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/command/PushResponseCommand.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/command/PushResponseCommand.java
new file mode 100644
index 000000000..c5b22997f
--- /dev/null
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/command/PushResponseCommand.java
@@ -0,0 +1,65 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http2.nio.command;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.apache.hc.core5.annotation.Internal;
+import org.apache.hc.core5.reactor.Command;
+import org.apache.hc.core5.util.Args;
+
+/**
+ * Activates the stream reserved with a push promise
+ *
+ * @since 5.4
+ */
+@Internal
+public final class PushResponseCommand implements Command {
+
+ private final int streamId;
+ private final AtomicBoolean cancelled;
+
+ public PushResponseCommand(final int streamId) {
+ this.streamId = Args.positive(streamId, "Stream Id");
+ this.cancelled = new AtomicBoolean();
+ }
+
+ public int getStreamId() {
+ return streamId;
+ }
+
+ public boolean isCancelled() {
+ return cancelled.get();
+ }
+
+ @Override
+ public boolean cancel() {
+ return cancelled.compareAndSet(false, true);
+ }
+
+}
diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
index e825eae5a..9a5f4afba 100644
--- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
+++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java
@@ -30,20 +30,15 @@
import static org.hamcrest.MatcherAssert.assertThat;
import java.io.IOException;
-import java.io.InterruptedIOException;
import java.net.InetSocketAddress;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
-import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
-import java.util.concurrent.LinkedBlockingDeque;
-import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
-import org.apache.hc.core5.function.Supplier;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.EndpointDetails;
import org.apache.hc.core5.http.HeaderElements;
@@ -58,7 +53,6 @@
import org.apache.hc.core5.http.ProtocolException;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.message.BasicHttpRequest;
-import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
import org.apache.hc.core5.http.nio.AsyncServerRequestHandler;
import org.apache.hc.core5.http.nio.entity.AsyncEntityProducers;
import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer;
@@ -169,53 +163,70 @@ protected void handle(
.build());
client.start();
- final BlockingQueue<Message<HttpResponse, String>> pushMessageQueue = new LinkedBlockingDeque<>();
+ final int reqNo = 200;
final Future<ClientSessionEndpoint> connectFuture = client.connect(target, TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
- final BasicHttpRequest request = BasicRequestBuilder.get()
- .setHttpHost(target)
- .setPath("/hello")
- .build();
+ final CountDownLatch latch = new CountDownLatch(reqNo);
+ final Queue<Future<Message<HttpResponse, String>>> responseQueue = new LinkedList<>();
+ final Queue<Message<HttpResponse, String>> pushMessageQueue = new LinkedList<>();
- final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(
- new BasicRequestProducer(request, null),
- new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
- (r, c) -> new AbstractAsyncPushHandler<Message<HttpResponse, String>>(new BasicResponseConsumer<>(new StringAsyncEntityConsumer())) {
+ for (int i = 0; i < reqNo; i++) {
+ final BasicHttpRequest request = BasicRequestBuilder.get()
+ .setHttpHost(target)
+ .setPath("/hello")
+ .build();
- @Override
- protected void handleResponse(
- final HttpRequest promise,
- final Message<HttpResponse, String> responseMessage) throws IOException, HttpException {
- try {
- pushMessageQueue.put(responseMessage);
- } catch (final InterruptedException ex) {
- Thread.currentThread().interrupt();
- throw new InterruptedIOException(ex.getMessage());
+ responseQueue.add(streamEndpoint.execute(
+ new BasicRequestProducer(request, null),
+ new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
+ (r, c) -> new AbstractAsyncPushHandler<Message<HttpResponse, String>>(new BasicResponseConsumer<>(new StringAsyncEntityConsumer())) {
+
+ @Override
+ protected void handleResponse(
+ final HttpRequest promise,
+ final Message<HttpResponse, String> responseMessage) throws IOException, HttpException {
+ pushMessageQueue.add(responseMessage);
+ latch.countDown();
}
- }
- },
- null,
- null);
- final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
- Assertions.assertNotNull(result1);
- final HttpResponse response1 = result1.getHead();
- final String entity1 = result1.getBody();
- Assertions.assertNotNull(response1);
- Assertions.assertEquals(200, response1.getCode());
- Assertions.assertEquals("Hi there", entity1);
+ @Override
+ protected void handleError(final HttpRequest promise, final Exception cause) {
+ latch.countDown();
+ }
+ },
+ null,
+ null));
+ }
- final Message<HttpResponse, String> result2 = pushMessageQueue.poll(5, TimeUnit.SECONDS);
- Assertions.assertNotNull(result2);
- final HttpResponse response2 = result2.getHead();
- final String entity2 = result2.getBody();
- Assertions.assertEquals(200, response2.getCode());
- Assertions.assertNotNull(entity2);
- final StringTokenizer t1 = new StringTokenizer(entity2, "\r\n");
- while (t1.hasMoreTokens()) {
- Assertions.assertEquals("Pushing lots of stuff", t1.nextToken());
+ Assertions.assertTrue(latch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+
+ Assertions.assertEquals(reqNo, responseQueue.size());
+ Assertions.assertEquals(reqNo, pushMessageQueue.size());
+
+ while (!responseQueue.isEmpty()) {
+ final Future<Message<HttpResponse, String>> future = responseQueue.remove();
+ final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
+ Assertions.assertNotNull(result);
+ final HttpResponse response1 = result.getHead();
+ final String entity = result.getBody();
+ Assertions.assertNotNull(response1);
+ Assertions.assertEquals(200, response1.getCode());
+ Assertions.assertEquals("Hi there", entity);
+ }
+
+ while (!pushMessageQueue.isEmpty()) {
+ final Message<HttpResponse, String> pushMessage = pushMessageQueue.remove();
+ Assertions.assertNotNull(pushMessage);
+ final HttpResponse response = pushMessage.getHead();
+ final String entity = pushMessage.getBody();
+ Assertions.assertEquals(200, response.getCode());
+ Assertions.assertNotNull(entity);
+ final StringTokenizer t1 = new StringTokenizer(entity, "\r\n");
+ while (t1.hasMoreTokens()) {
+ Assertions.assertEquals("Pushing lots of stuff", t1.nextToken());
+ }
}
}
@@ -224,12 +235,10 @@ void testPushRefused() throws Exception {
final H2TestServer server = resources.server();
final H2TestClient client = resources.client();
- final BlockingQueue<Exception> pushResultQueue = new LinkedBlockingDeque<>();
- server.register("/hello", new Supplier<AsyncServerExchangeHandler>() {
-
- @Override
- public AsyncServerExchangeHandler get() {
- return new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {
+ final CountDownLatch latch = new CountDownLatch(2);
+ final Queue<Exception> pushResultQueue = new LinkedList<>();
+ server.register("/hello", () ->
+ new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {
@Override
protected void handle(
@@ -239,34 +248,35 @@ protected void handle(
responseTrigger.pushPromise(
new BasicHttpRequest(Method.GET, new HttpHost(scheme.id, "localhost"), "/stuff"),
- context, new BasicPushProducer(AsyncEntityProducers.create("Pushing all sorts of stuff")) {
+ context,
+ new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 50000)) {
- @Override
- public void failed(final Exception cause) {
- pushResultQueue.add(cause);
- super.failed(cause);
- }
+ @Override
+ public void failed(final Exception cause) {
+ pushResultQueue.add(cause);
+ latch.countDown();
+ }
+
+ });
- });
responseTrigger.pushPromise(
new BasicHttpRequest(Method.GET, new HttpHost(scheme.id, "localhost"), "/more-stuff"),
- context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)) {
+ context,
+ new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 50000)) {
+
+ @Override
+ public void failed(final Exception cause) {
+ pushResultQueue.add(cause);
+ latch.countDown();
+ }
- @Override
- public void failed(final Exception cause) {
- pushResultQueue.add(cause);
- super.failed(cause);
- }
+ });
- });
responseTrigger.submitResponse(
new BasicResponseProducer(HttpStatus.SC_OK, AsyncEntityProducers.create("Hi there")),
context);
}
- };
- }
-
- });
+ });
final InetSocketAddress serverEndpoint = server.start();
final HttpHost target = target(serverEndpoint);
@@ -286,7 +296,10 @@ public void failed(final Exception cause) {
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(
new BasicRequestProducer(request1, null),
- new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
+ new BasicResponseConsumer<>(new StringAsyncEntityConsumer()),
+ (r, c) -> null, // refuse all push messages
+ null,
+ null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
@@ -295,12 +308,16 @@ public void failed(final Exception cause) {
Assertions.assertEquals(200, response1.getCode());
Assertions.assertEquals("Hi there", entity1);
- final Object result2 = pushResultQueue.poll(5, TimeUnit.SECONDS);
+ Assertions.assertTrue(latch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
+
+ Assertions.assertEquals(2, pushResultQueue.size());
+
+ final Object result2 = pushResultQueue.poll();
Assertions.assertNotNull(result2);
Assertions.assertTrue(result2 instanceof H2StreamResetException);
Assertions.assertEquals(H2Error.REFUSED_STREAM.getCode(), ((H2StreamResetException) result2).getCode());
- final Object result3 = pushResultQueue.poll(5, TimeUnit.SECONDS);
+ final Object result3 = pushResultQueue.poll();
Assertions.assertNotNull(result3);
Assertions.assertTrue(result3 instanceof H2StreamResetException);
Assertions.assertEquals(H2Error.REFUSED_STREAM.getCode(), ((H2StreamResetException) result3).getCode());