Skip to content

Commit f83101b

Browse files
committed
Add H2 multiplexing connection pool
Implement an HTTP/2 stream-capacity-aware connection pool with per-route and global connection limits, reservation-aware leasing, GOAWAY draining, disconnect handling, and route-level wakeup logic. Optimize uncontended lease paths and add coverage for the main pooling and lifecycle edge cases.
1 parent 35e7d53 commit f83101b

22 files changed

Lines changed: 4088 additions & 123 deletions

‎httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2IOEventHandler.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,16 @@ class AbstractH2IOEventHandler implements HttpConnectionEventHandler {
4747
final AbstractH2StreamMultiplexer streamMultiplexer;
4848

4949
AbstractH2IOEventHandler(final AbstractH2StreamMultiplexer streamMultiplexer) {
50+
this(streamMultiplexer, null);
51+
}
52+
53+
AbstractH2IOEventHandler(
54+
final AbstractH2StreamMultiplexer streamMultiplexer,
55+
final H2PoolSessionSupport sessionSupport) {
5056
this.streamMultiplexer = Args.notNull(streamMultiplexer, "Stream multiplexer");
57+
if (sessionSupport != null) {
58+
streamMultiplexer.setPoolSessionSupport(sessionSupport);
59+
}
5160
}
5261

5362
@Override

‎httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ enum SettingsHandshake { READY, TRANSMITTED, ACKED }
143143

144144
private EndpointDetails endpointDetails;
145145
private boolean goAwayReceived;
146+
private volatile H2PoolSessionSupport poolSessionSupport;
146147

147148
private volatile boolean peerNoRfc7540Priorities;
148149

@@ -193,6 +194,7 @@ enum SettingsHandshake { READY, TRANSMITTED, ACKED }
193194
this.frameFactory = Args.notNull(frameFactory, "Frame factory");
194195
this.httpProcessor = Args.notNull(httpProcessor, "HTTP processor");
195196
this.streams = new H2Streams(idGenerator);
197+
this.streams.setLocalStreamChangeCallback(this::fireCapacityAvailable);
196198
this.localConfig = h2Config != null ? h2Config : H2Config.DEFAULT;
197199
this.inputMetrics = new BasicH2TransportMetrics();
198200
this.outputMetrics = new BasicH2TransportMetrics();
@@ -674,6 +676,7 @@ public final void onDisconnect() {
674676
}
675677
streams.shutdownAndReleaseAll();
676678
CommandSupport.cancelCommands(ioSession);
679+
fireSessionClosed();
677680
}
678681

679682
private void executeShutdown(final ShutdownCommand shutdownCommand) throws IOException {
@@ -1011,6 +1014,11 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
10111014
final RawFrame response = frameFactory.createSettingsAck();
10121015
commitFrame(response);
10131016
remoteSettingState = SettingsHandshake.ACKED;
1017+
final H2PoolSessionSupport support = this.poolSessionSupport;
1018+
if (support != null) {
1019+
support.updatePeerSettings(remoteConfig.getMaxConcurrentStreams());
1020+
}
1021+
fireCapacityAvailable();
10141022
}
10151023
}
10161024
break;
@@ -1099,6 +1107,7 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio
10991107
streams.shutdownAndReleaseAll();
11001108
connState = ConnectionHandshake.SHUTDOWN;
11011109
}
1110+
fireDraining();
11021111
}
11031112
ioSession.setEvent(SelectionKey.OP_WRITE);
11041113
break;
@@ -1358,6 +1367,7 @@ private void applyRemoteSettings(final H2Config config) throws H2ConnectionExcep
13581367
}
13591368
}
13601369
}
1370+
fireCapacityAvailable();
13611371
}
13621372

13631373
private void applyLocalSettings() throws H2ConnectionException {
@@ -1438,6 +1448,62 @@ public SocketAddress getLocalAddress() {
14381448
return ioSession.getLocalAddress();
14391449
}
14401450

1451+
int getActiveLocalStreams() {
1452+
return streams.getLocalCount();
1453+
}
1454+
1455+
int getPeerMaxConcurrentStreams() {
1456+
return remoteConfig.getMaxConcurrentStreams();
1457+
}
1458+
1459+
boolean isGoAwayReceived() {
1460+
return goAwayReceived;
1461+
}
1462+
1463+
boolean isShutdown() {
1464+
return connState.compareTo(ConnectionHandshake.GRACEFUL_SHUTDOWN) >= 0;
1465+
}
1466+
1467+
void setPoolSessionSupport(final H2PoolSessionSupport poolSessionSupport) {
1468+
this.poolSessionSupport = poolSessionSupport;
1469+
if (poolSessionSupport != null) {
1470+
poolSessionSupport.updateActiveLocalStreams(streams.getLocalCount());
1471+
if (remoteSettingState == SettingsHandshake.ACKED) {
1472+
poolSessionSupport.updatePeerSettings(remoteConfig.getMaxConcurrentStreams());
1473+
}
1474+
poolSessionSupport.updateGoAwayReceived(goAwayReceived);
1475+
poolSessionSupport.updateShutdown(
1476+
connState.compareTo(ConnectionHandshake.GRACEFUL_SHUTDOWN) >= 0);
1477+
}
1478+
}
1479+
1480+
void fireCapacityAvailable() {
1481+
final H2PoolSessionSupport support = this.poolSessionSupport;
1482+
if (support != null) {
1483+
support.updateActiveLocalStreams(streams.getLocalCount());
1484+
support.updateShutdown(connState.compareTo(ConnectionHandshake.GRACEFUL_SHUTDOWN) >= 0);
1485+
support.fireCapacityAvailable();
1486+
}
1487+
}
1488+
1489+
void fireDraining() {
1490+
final H2PoolSessionSupport support = this.poolSessionSupport;
1491+
if (support != null) {
1492+
support.updateGoAwayReceived(true);
1493+
support.updateShutdown(connState.compareTo(ConnectionHandshake.GRACEFUL_SHUTDOWN) >= 0);
1494+
support.fireDraining();
1495+
}
1496+
}
1497+
1498+
void fireSessionClosed() {
1499+
final H2PoolSessionSupport support = this.poolSessionSupport;
1500+
if (support != null) {
1501+
support.updateActiveLocalStreams(0);
1502+
support.updateShutdown(true);
1503+
support.fireSessionClosed();
1504+
}
1505+
}
1506+
14411507
void appendState(final StringBuilder buf) {
14421508
buf.append("connState=").append(connState)
14431509
.append(", connInputWindow=").append(connInputWindow)

‎httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientH2IOEventHandler.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public ClientH2IOEventHandler(final ClientH2StreamMultiplexer streamMultiplexer)
4242
super(streamMultiplexer);
4343
}
4444

45+
ClientH2IOEventHandler(
46+
final ClientH2StreamMultiplexer streamMultiplexer,
47+
final H2PoolSessionSupport sessionSupport) {
48+
super(streamMultiplexer, sessionSupport);
49+
}
50+
4551
@Override
4652
public String toString() {
4753
final StringBuilder buf = new StringBuilder();

‎httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientH2PrefaceHandler.java‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public class ClientH2PrefaceHandler extends PrefaceHandlerBase {
6161

6262
private final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory;
6363
private final boolean strictALPNHandshake;
64+
private final H2PoolSessionSupport sessionSupport;
6465
private final AtomicBoolean initialized;
6566

6667
private volatile ByteBuffer preface;
@@ -83,9 +84,23 @@ public ClientH2PrefaceHandler(
8384
final boolean strictALPNHandshake,
8485
final FutureCallback<ProtocolIOSession> resultCallback,
8586
final Callback<Exception> exceptionCallback) {
87+
this(ioSession, http2StreamHandlerFactory, strictALPNHandshake, null, resultCallback, exceptionCallback);
88+
}
89+
90+
/**
91+
* @since 5.5
92+
*/
93+
public ClientH2PrefaceHandler(
94+
final ProtocolIOSession ioSession,
95+
final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory,
96+
final boolean strictALPNHandshake,
97+
final H2PoolSessionSupport sessionSupport,
98+
final FutureCallback<ProtocolIOSession> resultCallback,
99+
final Callback<Exception> exceptionCallback) {
86100
super(ioSession, resultCallback, exceptionCallback);
87101
this.http2StreamHandlerFactory = Args.notNull(http2StreamHandlerFactory, "HTTP/2 stream handler factory");
88102
this.strictALPNHandshake = strictALPNHandshake;
103+
this.sessionSupport = sessionSupport;
89104
this.initialized = new AtomicBoolean();
90105
}
91106

@@ -117,7 +132,8 @@ private boolean writeOutPreface(final IOSession session, final ByteBuffer prefac
117132
if (!preface.hasRemaining()) {
118133
session.clearEvent(SelectionKey.OP_WRITE);
119134
final ByteBuffer data = inBuf != null ? inBuf.data() : null;
120-
startProtocol(new ClientH2IOEventHandler(http2StreamHandlerFactory.create(ioSession)), data);
135+
startProtocol(new ClientH2IOEventHandler(
136+
http2StreamHandlerFactory.create(ioSession), sessionSupport), data);
121137
if (inBuf != null) {
122138
inBuf.clear();
123139
}
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
package org.apache.hc.core5.http2.impl.nio;
28+
29+
import java.util.concurrent.atomic.AtomicInteger;
30+
import java.util.concurrent.atomic.AtomicReference;
31+
32+
import org.apache.hc.core5.annotation.Internal;
33+
34+
/**
35+
* Attachment-based bridge between the HTTP/2 multiplexing connection
36+
* pool and the stream multiplexer. The pool creates an instance and
37+
* passes it as the {@code attachment} argument to the connection
38+
* initiator; the I/O event handler detects it and wires it to the
39+
* multiplexer. The multiplexer pushes state updates into this object
40+
* and fires observer callbacks through it; the pool reads state and
41+
* registers its observer here.
42+
*
43+
* @since 5.5
44+
*/
45+
@Internal
46+
public final class H2PoolSessionSupport {
47+
48+
/**
49+
* Callback for capacity, draining and session-closure notifications.
50+
* Implementations are invoked from the I/O reactor thread
51+
* and must not block.
52+
*/
53+
public interface Observer {
54+
55+
/** Stream capacity became available on the connection. */
56+
void onCapacityAvailable();
57+
58+
/** Connection entered a draining state. */
59+
void onDraining();
60+
61+
/**
62+
* Underlying session has been fully disconnected. Fired exactly once
63+
* after the multiplexer has finalised its disconnect bookkeeping, so
64+
* the observer may treat reservations and active streams on this
65+
* session as void. Default is a no-op to preserve source and binary
66+
* compatibility with existing observers.
67+
*
68+
* @since 5.5
69+
*/
70+
default void onSessionClosed() {
71+
}
72+
73+
}
74+
75+
private final AtomicInteger activeLocalStreams;
76+
private final AtomicInteger peerMaxConcurrentStreams;
77+
private final AtomicReference<Observer> observerRef;
78+
79+
private volatile boolean peerSettingsReceived;
80+
private volatile boolean goAwayReceived;
81+
private volatile boolean shutdown;
82+
83+
public H2PoolSessionSupport() {
84+
this.activeLocalStreams = new AtomicInteger();
85+
this.peerMaxConcurrentStreams = new AtomicInteger(Integer.MAX_VALUE);
86+
this.observerRef = new AtomicReference<>();
87+
}
88+
89+
/** Returns the number of locally initiated streams currently open. */
90+
public int getActiveLocalStreams() {
91+
return activeLocalStreams.get();
92+
}
93+
94+
/** Returns the peer's {@code MAX_CONCURRENT_STREAMS} value. */
95+
public int getPeerMaxConcurrentStreams() {
96+
return peerMaxConcurrentStreams.get();
97+
}
98+
99+
/** Returns {@code true} if a {@code GOAWAY} frame has been received. */
100+
/**
101+
* Returns {@code true} once the peer's initial SETTINGS frame has been
102+
* received and processed.
103+
*/
104+
public boolean isPeerSettingsReceived() {
105+
return peerSettingsReceived;
106+
}
107+
108+
public boolean isGoAwayReceived() {
109+
return goAwayReceived;
110+
}
111+
112+
/** Returns {@code true} if the session is shutting down. */
113+
public boolean isShutdown() {
114+
return shutdown;
115+
}
116+
117+
/** Registers the observer for capacity change notifications. */
118+
public void setObserver(final Observer observer) {
119+
observerRef.set(observer);
120+
}
121+
122+
/** Pushes the current active local stream count. */
123+
public void updateActiveLocalStreams(final int count) {
124+
activeLocalStreams.set(count);
125+
}
126+
127+
/** Pushes the peer's {@code MAX_CONCURRENT_STREAMS} value. */
128+
public void updatePeerMaxConcurrentStreams(final int max) {
129+
peerMaxConcurrentStreams.set(max);
130+
}
131+
132+
/**
133+
* Publishes the peer's effective {@code MAX_CONCURRENT_STREAMS} value
134+
* and marks the initial peer SETTINGS as received.
135+
*/
136+
public void updatePeerSettings(final int maxConcurrentStreams) {
137+
peerMaxConcurrentStreams.set(maxConcurrentStreams);
138+
peerSettingsReceived = true;
139+
}
140+
141+
/** Pushes the GOAWAY-received flag. */
142+
public void updateGoAwayReceived(final boolean value) {
143+
goAwayReceived = value;
144+
}
145+
146+
/** Pushes the shutdown flag. */
147+
public void updateShutdown(final boolean value) {
148+
shutdown = value;
149+
}
150+
151+
/** Fires the capacity-available notification. */
152+
public void fireCapacityAvailable() {
153+
final Observer observer = observerRef.get();
154+
if (observer != null) {
155+
observer.onCapacityAvailable();
156+
}
157+
}
158+
159+
/** Fires the draining notification. */
160+
public void fireDraining() {
161+
final Observer observer = observerRef.get();
162+
if (observer != null) {
163+
observer.onDraining();
164+
}
165+
}
166+
167+
/**
168+
* Fires the session-closed notification. Intended to be invoked exactly
169+
* once per session after the multiplexer has reset its local stream
170+
* state, so the pool sees a coherent final picture.
171+
*
172+
* @since 5.5
173+
*/
174+
public void fireSessionClosed() {
175+
final Observer observer = observerRef.get();
176+
if (observer != null) {
177+
observer.onSessionClosed();
178+
}
179+
}
180+
181+
}

0 commit comments

Comments
 (0)