|
| 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