/
DefaultHttp2Connection.java
1061 lines (920 loc) · 37.7 KB
/
DefaultHttp2Connection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2014 The Netty Project
*
* The Netty Project 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:
*
* https://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.
*/
package io.netty.handler.codec.http2;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http2.Http2Stream.State;
import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap;
import io.netty.util.collection.IntObjectMap.PrimitiveEntry;
import io.netty.util.concurrent.Promise;
import io.netty.util.concurrent.UnaryPromiseNotifier;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.UnstableApi;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Queue;
import java.util.Set;
import static io.netty.handler.codec.http2.Http2CodecUtil.CONNECTION_STREAM_ID;
import static io.netty.handler.codec.http2.Http2CodecUtil.DEFAULT_MAX_RESERVED_STREAMS;
import static io.netty.handler.codec.http2.Http2Error.INTERNAL_ERROR;
import static io.netty.handler.codec.http2.Http2Error.PROTOCOL_ERROR;
import static io.netty.handler.codec.http2.Http2Error.REFUSED_STREAM;
import static io.netty.handler.codec.http2.Http2Exception.closedStreamError;
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.handler.codec.http2.Http2Exception.streamError;
import static io.netty.handler.codec.http2.Http2Stream.State.CLOSED;
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_LOCAL;
import static io.netty.handler.codec.http2.Http2Stream.State.HALF_CLOSED_REMOTE;
import static io.netty.handler.codec.http2.Http2Stream.State.IDLE;
import static io.netty.handler.codec.http2.Http2Stream.State.OPEN;
import static io.netty.handler.codec.http2.Http2Stream.State.RESERVED_LOCAL;
import static io.netty.handler.codec.http2.Http2Stream.State.RESERVED_REMOTE;
import static io.netty.util.internal.ObjectUtil.checkPositiveOrZero;
import static java.lang.Integer.MAX_VALUE;
import static java.util.Objects.requireNonNull;
/**
* Simple implementation of {@link Http2Connection}.
*/
@UnstableApi
public class DefaultHttp2Connection implements Http2Connection {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(DefaultHttp2Connection.class);
// Fields accessed by inner classes
final IntObjectMap<Http2Stream> streamMap = new IntObjectHashMap<Http2Stream>();
final PropertyKeyRegistry propertyKeyRegistry = new PropertyKeyRegistry();
final ConnectionStream connectionStream = new ConnectionStream();
final DefaultEndpoint<Http2LocalFlowController> localEndpoint;
final DefaultEndpoint<Http2RemoteFlowController> remoteEndpoint;
/**
* We chose a {@link List} over a {@link Set} to avoid allocating an {@link Iterator} objects when iterating over
* the listeners.
* <p>
* Initial size of 4 because the default configuration currently has 3 listeners
* (local/remote flow controller and {@link StreamByteDistributor}) and we leave room for 1 extra.
* We could be more aggressive but the ArrayList resize will double the size if we are too small.
*/
final List<Listener> listeners = new ArrayList<>(4);
final ActiveStreams activeStreams;
Promise<Void> closePromise;
/**
* Creates a new connection with the given settings.
* @param server whether or not this end-point is the server-side of the HTTP/2 connection.
*/
public DefaultHttp2Connection(boolean server) {
this(server, DEFAULT_MAX_RESERVED_STREAMS);
}
/**
* Creates a new connection with the given settings.
* @param server whether or not this end-point is the server-side of the HTTP/2 connection.
* @param maxReservedStreams The maximum amount of streams which can exist in the reserved state for each endpoint.
*/
public DefaultHttp2Connection(boolean server, int maxReservedStreams) {
activeStreams = new ActiveStreams(listeners);
// Reserved streams are excluded from the SETTINGS_MAX_CONCURRENT_STREAMS limit according to [1] and the RFC
// doesn't define a way to communicate the limit on reserved streams. We rely upon the peer to send RST_STREAM
// in response to any locally enforced limits being exceeded [2].
// [1] https://tools.ietf.org/html/rfc7540#section-5.1.2
// [2] https://tools.ietf.org/html/rfc7540#section-8.2.2
localEndpoint = new DefaultEndpoint<>(server, server ? MAX_VALUE : maxReservedStreams);
remoteEndpoint = new DefaultEndpoint<>(!server, maxReservedStreams);
// Add the connection stream to the map.
streamMap.put(connectionStream.id(), connectionStream);
}
/**
* Determine if {@link #close(Promise)} has been called and no more streams are allowed to be created.
*/
final boolean isClosed() {
return closePromise != null;
}
@Override
public void close(final Promise<Void> promise) {
requireNonNull(promise, "promise");
// Since we allow this method to be called multiple times, we must make sure that all the promises are notified
// when all streams are removed and the close operation completes.
if (closePromise != null) {
if (closePromise == promise) {
// Do nothing
} else {
closePromise.asFuture().addListener(new UnaryPromiseNotifier<>(promise));
}
} else {
closePromise = promise;
}
if (isStreamMapEmpty()) {
promise.trySuccess(null);
return;
}
Iterator<PrimitiveEntry<Http2Stream>> itr = streamMap.entries().iterator();
// We must take care while iterating the streamMap as to not modify while iterating in case there are other code
// paths iterating over the active streams.
if (activeStreams.allowModifications()) {
activeStreams.incrementPendingIterations();
try {
while (itr.hasNext()) {
DefaultStream stream = (DefaultStream) itr.next().value();
if (stream.id() != CONNECTION_STREAM_ID) {
// If modifications of the activeStream map is allowed, then a stream close operation will also
// modify the streamMap. Pass the iterator in so that remove will be called to prevent
// concurrent modification exceptions.
stream.close(itr);
}
}
} finally {
activeStreams.decrementPendingIterations();
}
} else {
while (itr.hasNext()) {
Http2Stream stream = itr.next().value();
if (stream.id() != CONNECTION_STREAM_ID) {
// We are not allowed to make modifications, so the close calls will be executed after this
// iteration completes.
stream.close();
}
}
}
}
@Override
public void addListener(Listener listener) {
listeners.add(listener);
}
@Override
public void removeListener(Listener listener) {
listeners.remove(listener);
}
@Override
public boolean isServer() {
return localEndpoint.isServer();
}
@Override
public Http2Stream connectionStream() {
return connectionStream;
}
@Override
public Http2Stream stream(int streamId) {
return streamMap.get(streamId);
}
@Override
public boolean streamMayHaveExisted(int streamId) {
return remoteEndpoint.mayHaveCreatedStream(streamId) || localEndpoint.mayHaveCreatedStream(streamId);
}
@Override
public int numActiveStreams() {
return activeStreams.size();
}
@Override
public Http2Stream forEachActiveStream(Http2StreamVisitor visitor) throws Http2Exception {
return activeStreams.forEachActiveStream(visitor);
}
@Override
public Endpoint<Http2LocalFlowController> local() {
return localEndpoint;
}
@Override
public Endpoint<Http2RemoteFlowController> remote() {
return remoteEndpoint;
}
@Override
public boolean goAwayReceived() {
return localEndpoint.lastStreamKnownByPeer >= 0;
}
@Override
public void goAwayReceived(final int lastKnownStream, long errorCode, ByteBuf debugData) throws Http2Exception {
if (localEndpoint.lastStreamKnownByPeer() >= 0 && localEndpoint.lastStreamKnownByPeer() < lastKnownStream) {
throw connectionError(PROTOCOL_ERROR, "lastStreamId MUST NOT increase. Current value: %d new value: %d",
localEndpoint.lastStreamKnownByPeer(), lastKnownStream);
}
localEndpoint.lastStreamKnownByPeer(lastKnownStream);
for (int i = 0; i < listeners.size(); ++i) {
try {
listeners.get(i).onGoAwayReceived(lastKnownStream, errorCode, debugData);
} catch (Throwable cause) {
logger.error("Caught Throwable from listener onGoAwayReceived.", cause);
}
}
closeStreamsGreaterThanLastKnownStreamId(lastKnownStream, localEndpoint);
}
@Override
public boolean goAwaySent() {
return remoteEndpoint.lastStreamKnownByPeer >= 0;
}
@Override
public boolean goAwaySent(final int lastKnownStream, long errorCode, ByteBuf debugData) throws Http2Exception {
if (remoteEndpoint.lastStreamKnownByPeer() >= 0) {
// Protect against re-entrancy. Could happen if writing the frame fails, and error handling
// treating this is a connection handler and doing a graceful shutdown...
if (lastKnownStream == remoteEndpoint.lastStreamKnownByPeer()) {
return false;
}
if (lastKnownStream > remoteEndpoint.lastStreamKnownByPeer()) {
throw connectionError(PROTOCOL_ERROR, "Last stream identifier must not increase between " +
"sending multiple GOAWAY frames (was '%d', is '%d').",
remoteEndpoint.lastStreamKnownByPeer(), lastKnownStream);
}
}
remoteEndpoint.lastStreamKnownByPeer(lastKnownStream);
for (int i = 0; i < listeners.size(); ++i) {
try {
listeners.get(i).onGoAwaySent(lastKnownStream, errorCode, debugData);
} catch (Throwable cause) {
logger.error("Caught Throwable from listener onGoAwaySent.", cause);
}
}
closeStreamsGreaterThanLastKnownStreamId(lastKnownStream, remoteEndpoint);
return true;
}
private void closeStreamsGreaterThanLastKnownStreamId(final int lastKnownStream,
final DefaultEndpoint<?> endpoint) throws Http2Exception {
forEachActiveStream(stream -> {
if (stream.id() > lastKnownStream && endpoint.isValidStreamId(stream.id())) {
stream.close();
}
return true;
});
}
/**
* Determine if {@link #streamMap} only contains the connection stream.
*/
private boolean isStreamMapEmpty() {
return streamMap.size() == 1;
}
/**
* Remove a stream from the {@link #streamMap}.
* @param stream the stream to remove.
* @param itr an iterator that may be pointing to the stream during iteration and {@link Iterator#remove()} will be
* used if non-{@code null}.
*/
void removeStream(DefaultStream stream, Iterator<?> itr) {
final boolean removed;
if (itr == null) {
removed = streamMap.remove(stream.id()) != null;
} else {
itr.remove();
removed = true;
}
if (removed) {
for (int i = 0; i < listeners.size(); i++) {
try {
listeners.get(i).onStreamRemoved(stream);
} catch (Throwable cause) {
logger.error("Caught Throwable from listener onStreamRemoved.", cause);
}
}
if (closePromise != null && isStreamMapEmpty()) {
closePromise.trySuccess(null);
}
}
}
static State activeState(int streamId, State initialState, boolean isLocal, boolean halfClosed)
throws Http2Exception {
switch (initialState) {
case IDLE:
return halfClosed ? isLocal ? HALF_CLOSED_LOCAL : HALF_CLOSED_REMOTE : OPEN;
case RESERVED_LOCAL:
return HALF_CLOSED_REMOTE;
case RESERVED_REMOTE:
return HALF_CLOSED_LOCAL;
default:
throw streamError(streamId, PROTOCOL_ERROR, "Attempting to open a stream in an invalid state: "
+ initialState);
}
}
void notifyHalfClosed(Http2Stream stream) {
for (int i = 0; i < listeners.size(); i++) {
try {
listeners.get(i).onStreamHalfClosed(stream);
} catch (Throwable cause) {
logger.error("Caught Throwable from listener onStreamHalfClosed.", cause);
}
}
}
void notifyClosed(Http2Stream stream) {
for (int i = 0; i < listeners.size(); i++) {
try {
listeners.get(i).onStreamClosed(stream);
} catch (Throwable cause) {
logger.error("Caught Throwable from listener onStreamClosed.", cause);
}
}
}
@Override
public PropertyKey newKey() {
return propertyKeyRegistry.newKey();
}
/**
* Verifies that the key is valid and returns it as the internal {@link DefaultPropertyKey} type.
*
* @throws NullPointerException if the key is {@code null}.
* @throws ClassCastException if the key is not of type {@link DefaultPropertyKey}.
* @throws IllegalArgumentException if the key was not created by this connection.
*/
final DefaultPropertyKey verifyKey(PropertyKey key) {
return requireNonNull((DefaultPropertyKey) key, "key").verifyConnection(this);
}
/**
* Simple stream implementation. Streams can be compared to each other by priority.
*/
private class DefaultStream implements Http2Stream {
private static final byte META_STATE_SENT_RST = 1;
private static final byte META_STATE_SENT_HEADERS = 1 << 1;
private static final byte META_STATE_SENT_TRAILERS = 1 << 2;
private static final byte META_STATE_SENT_PUSHPROMISE = 1 << 3;
private static final byte META_STATE_RECV_HEADERS = 1 << 4;
private static final byte META_STATE_RECV_TRAILERS = 1 << 5;
private final int id;
private final PropertyMap properties = new PropertyMap();
private State state;
private byte metaState;
DefaultStream(int id, State state) {
this.id = id;
this.state = state;
}
@Override
public final int id() {
return id;
}
@Override
public final State state() {
return state;
}
@Override
public boolean isResetSent() {
return (metaState & META_STATE_SENT_RST) != 0;
}
@Override
public Http2Stream resetSent() {
metaState |= META_STATE_SENT_RST;
return this;
}
@Override
public Http2Stream headersSent(boolean isInformational) {
if (!isInformational) {
metaState |= isHeadersSent() ? META_STATE_SENT_TRAILERS : META_STATE_SENT_HEADERS;
}
return this;
}
@Override
public boolean isHeadersSent() {
return (metaState & META_STATE_SENT_HEADERS) != 0;
}
@Override
public boolean isTrailersSent() {
return (metaState & META_STATE_SENT_TRAILERS) != 0;
}
@Override
public Http2Stream headersReceived(boolean isInformational) {
if (!isInformational) {
metaState |= isHeadersReceived() ? META_STATE_RECV_TRAILERS : META_STATE_RECV_HEADERS;
}
return this;
}
@Override
public boolean isHeadersReceived() {
return (metaState & META_STATE_RECV_HEADERS) != 0;
}
@Override
public boolean isTrailersReceived() {
return (metaState & META_STATE_RECV_TRAILERS) != 0;
}
@Override
public Http2Stream pushPromiseSent() {
metaState |= META_STATE_SENT_PUSHPROMISE;
return this;
}
@Override
public boolean isPushPromiseSent() {
return (metaState & META_STATE_SENT_PUSHPROMISE) != 0;
}
@Override
public final <V> V setProperty(PropertyKey key, V value) {
return properties.add(verifyKey(key), value);
}
@Override
public final <V> V getProperty(PropertyKey key) {
return properties.get(verifyKey(key));
}
@Override
public final <V> V removeProperty(PropertyKey key) {
return properties.remove(verifyKey(key));
}
@Override
public Http2Stream open(boolean halfClosed) throws Http2Exception {
state = activeState(id, state, isLocal(), halfClosed);
final DefaultEndpoint<? extends Http2FlowController> endpoint = createdBy();
if (!endpoint.canOpenStream()) {
throw connectionError(PROTOCOL_ERROR, "Maximum active streams violated for this endpoint: " +
endpoint.maxActiveStreams());
}
activate();
return this;
}
void activate() {
// If the stream is opened in a half-closed state, the headers must have either
// been sent if this is a local stream, or received if it is a remote stream.
if (state == HALF_CLOSED_LOCAL) {
headersSent(/*isInformational*/ false);
} else if (state == HALF_CLOSED_REMOTE) {
headersReceived(/*isInformational*/ false);
}
activeStreams.activate(this);
}
Http2Stream close(Iterator<?> itr) {
if (state == CLOSED) {
return this;
}
state = CLOSED;
--createdBy().numStreams;
activeStreams.deactivate(this, itr);
return this;
}
@Override
public Http2Stream close() {
return close(null);
}
@Override
public Http2Stream closeLocalSide() {
switch (state) {
case OPEN:
state = HALF_CLOSED_LOCAL;
notifyHalfClosed(this);
break;
case HALF_CLOSED_LOCAL:
break;
default:
close();
break;
}
return this;
}
@Override
public Http2Stream closeRemoteSide() {
switch (state) {
case OPEN:
state = HALF_CLOSED_REMOTE;
notifyHalfClosed(this);
break;
case HALF_CLOSED_REMOTE:
break;
default:
close();
break;
}
return this;
}
DefaultEndpoint<? extends Http2FlowController> createdBy() {
return localEndpoint.isValidStreamId(id) ? localEndpoint : remoteEndpoint;
}
final boolean isLocal() {
return localEndpoint.isValidStreamId(id);
}
/**
* Provides the lazy initialization for the {@link DefaultStream} data map.
*/
private class PropertyMap {
Object[] values = EmptyArrays.EMPTY_OBJECTS;
<V> V add(DefaultPropertyKey key, V value) {
resizeIfNecessary(key.index);
@SuppressWarnings("unchecked")
V prevValue = (V) values[key.index];
values[key.index] = value;
return prevValue;
}
@SuppressWarnings("unchecked")
<V> V get(DefaultPropertyKey key) {
if (key.index >= values.length) {
return null;
}
return (V) values[key.index];
}
@SuppressWarnings("unchecked")
<V> V remove(DefaultPropertyKey key) {
V prevValue = null;
if (key.index < values.length) {
prevValue = (V) values[key.index];
values[key.index] = null;
}
return prevValue;
}
void resizeIfNecessary(int index) {
if (index >= values.length) {
values = Arrays.copyOf(values, propertyKeyRegistry.size());
}
}
}
}
/**
* Stream class representing the connection, itself.
*/
private final class ConnectionStream extends DefaultStream {
ConnectionStream() {
super(CONNECTION_STREAM_ID, IDLE);
}
@Override
public boolean isResetSent() {
return false;
}
@Override
DefaultEndpoint<? extends Http2FlowController> createdBy() {
return null;
}
@Override
public Http2Stream resetSent() {
throw new UnsupportedOperationException();
}
@Override
public Http2Stream open(boolean halfClosed) {
throw new UnsupportedOperationException();
}
@Override
public Http2Stream close() {
throw new UnsupportedOperationException();
}
@Override
public Http2Stream closeLocalSide() {
throw new UnsupportedOperationException();
}
@Override
public Http2Stream closeRemoteSide() {
throw new UnsupportedOperationException();
}
@Override
public Http2Stream headersSent(boolean isInformational) {
throw new UnsupportedOperationException();
}
@Override
public boolean isHeadersSent() {
throw new UnsupportedOperationException();
}
@Override
public Http2Stream pushPromiseSent() {
throw new UnsupportedOperationException();
}
@Override
public boolean isPushPromiseSent() {
throw new UnsupportedOperationException();
}
}
/**
* Simple endpoint implementation.
*/
private final class DefaultEndpoint<F extends Http2FlowController> implements Endpoint<F> {
private final boolean server;
/**
* The minimum stream ID allowed when creating the next stream. This only applies at the time the stream is
* created. If the ID of the stream being created is less than this value, stream creation will fail. Upon
* successful creation of a stream, this value is incremented to the next valid stream ID.
*/
private int nextStreamIdToCreate;
/**
* Used for reservation of stream IDs. Stream IDs can be reserved in advance by applications before the streams
* are actually created. For example, applications may choose to buffer stream creation attempts as a way of
* working around {@code SETTINGS_MAX_CONCURRENT_STREAMS}, in which case they will reserve stream IDs for each
* buffered stream.
*/
private int nextReservationStreamId;
private int lastStreamKnownByPeer = -1;
private boolean pushToAllowed;
private F flowController;
private int maxStreams;
private int maxActiveStreams;
private final int maxReservedStreams;
// Fields accessed by inner classes
int numActiveStreams;
int numStreams;
DefaultEndpoint(boolean server, int maxReservedStreams) {
this.server = server;
// Determine the starting stream ID for this endpoint. Client-initiated streams
// are odd and server-initiated streams are even. Zero is reserved for the
// connection. Stream 1 is reserved client-initiated stream for responding to an
// upgrade from HTTP 1.1.
if (server) {
nextStreamIdToCreate = 2;
nextReservationStreamId = 0;
} else {
nextStreamIdToCreate = 1;
// For manually created client-side streams, 1 is reserved for HTTP upgrade, so start at 3.
nextReservationStreamId = 1;
}
// Push is disallowed by default for servers and allowed for clients.
pushToAllowed = !server;
maxActiveStreams = MAX_VALUE;
this.maxReservedStreams = checkPositiveOrZero(maxReservedStreams, "maxReservedStreams");
updateMaxStreams();
}
@Override
public int incrementAndGetNextStreamId() {
return nextReservationStreamId >= 0 ? nextReservationStreamId += 2 : nextReservationStreamId;
}
private void incrementExpectedStreamId(int streamId) {
if (streamId > nextReservationStreamId && nextReservationStreamId >= 0) {
nextReservationStreamId = streamId;
}
nextStreamIdToCreate = streamId + 2;
++numStreams;
}
@Override
public boolean isValidStreamId(int streamId) {
return streamId > 0 && server == ((streamId & 1) == 0);
}
@Override
public boolean mayHaveCreatedStream(int streamId) {
return isValidStreamId(streamId) && streamId <= lastStreamCreated();
}
@Override
public boolean canOpenStream() {
return numActiveStreams < maxActiveStreams;
}
@Override
public DefaultStream createStream(int streamId, boolean halfClosed) throws Http2Exception {
State state = activeState(streamId, IDLE, isLocal(), halfClosed);
checkNewStreamAllowed(streamId, state);
// Create and initialize the stream.
DefaultStream stream = new DefaultStream(streamId, state);
incrementExpectedStreamId(streamId);
addStream(stream);
stream.activate();
return stream;
}
@Override
public boolean created(Http2Stream stream) {
return stream instanceof DefaultStream && ((DefaultStream) stream).createdBy() == this;
}
@Override
public boolean isServer() {
return server;
}
@Override
public DefaultStream reservePushStream(int streamId, Http2Stream parent) throws Http2Exception {
if (parent == null) {
throw connectionError(PROTOCOL_ERROR, "Parent stream missing");
}
if (isLocal() ? !parent.state().localSideOpen() : !parent.state().remoteSideOpen()) {
throw connectionError(PROTOCOL_ERROR, "Stream %d is not open for sending push promise", parent.id());
}
if (!opposite().allowPushTo()) {
throw connectionError(PROTOCOL_ERROR, "Server push not allowed to opposite endpoint");
}
State state = isLocal() ? RESERVED_LOCAL : RESERVED_REMOTE;
checkNewStreamAllowed(streamId, state);
// Create and initialize the stream.
DefaultStream stream = new DefaultStream(streamId, state);
incrementExpectedStreamId(streamId);
// Register the stream.
addStream(stream);
return stream;
}
private void addStream(DefaultStream stream) {
// Add the stream to the map and priority tree.
streamMap.put(stream.id(), stream);
// Notify the listeners of the event.
for (int i = 0; i < listeners.size(); i++) {
try {
listeners.get(i).onStreamAdded(stream);
} catch (Throwable cause) {
logger.error("Caught Throwable from listener onStreamAdded.", cause);
}
}
}
@Override
public void allowPushTo(boolean allow) {
if (allow && server) {
throw new IllegalArgumentException("Servers do not allow push");
}
pushToAllowed = allow;
}
@Override
public boolean allowPushTo() {
return pushToAllowed;
}
@Override
public int numActiveStreams() {
return numActiveStreams;
}
@Override
public int maxActiveStreams() {
return maxActiveStreams;
}
@Override
public void maxActiveStreams(int maxActiveStreams) {
this.maxActiveStreams = maxActiveStreams;
updateMaxStreams();
}
@Override
public int lastStreamCreated() {
return nextStreamIdToCreate > 1 ? nextStreamIdToCreate - 2 : 0;
}
@Override
public int lastStreamKnownByPeer() {
return lastStreamKnownByPeer;
}
private void lastStreamKnownByPeer(int lastKnownStream) {
lastStreamKnownByPeer = lastKnownStream;
}
@Override
public F flowController() {
return flowController;
}
@Override
public void flowController(F flowController) {
this.flowController = requireNonNull(flowController, "flowController");
}
@Override
public Endpoint<? extends Http2FlowController> opposite() {
return isLocal() ? remoteEndpoint : localEndpoint;
}
private void updateMaxStreams() {
maxStreams = (int) Math.min(MAX_VALUE, (long) maxActiveStreams + maxReservedStreams);
}
private void checkNewStreamAllowed(int streamId, State state) throws Http2Exception {
assert state != IDLE;
if (lastStreamKnownByPeer >= 0 && streamId > lastStreamKnownByPeer) {
throw streamError(streamId, REFUSED_STREAM,
"Cannot create stream %d greater than Last-Stream-ID %d from GOAWAY.",
streamId, lastStreamKnownByPeer);
}
if (!isValidStreamId(streamId)) {
if (streamId < 0) {
throw new Http2NoMoreStreamIdsException();
}
throw connectionError(PROTOCOL_ERROR, "Request stream %d is not correct for %s connection", streamId,
server ? "server" : "client");
}
// This check must be after all id validated checks, but before the max streams check because it may be
// recoverable to some degree for handling frames which can be sent on closed streams.
if (streamId < nextStreamIdToCreate) {
throw closedStreamError(PROTOCOL_ERROR, "Request stream %d is behind the next expected stream %d",
streamId, nextStreamIdToCreate);
}
if (nextStreamIdToCreate <= 0) {
// 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) {
throw streamError(streamId, REFUSED_STREAM, "Maximum active streams violated for this endpoint: " +
(isReserved ? maxStreams : maxActiveStreams));
}
if (isClosed()) {
throw connectionError(INTERNAL_ERROR, "Attempted to create stream id %d after connection was closed",
streamId);
}
}
private boolean isLocal() {
return this == localEndpoint;
}
}
/**
* Allows events which would modify the collection of active streams to be queued while iterating via {@link
* #forEachActiveStream(Http2StreamVisitor)}.
*/
interface Event {
/**
* Trigger the original intention of this event. Expect to modify the active streams list.
* <p/>
* If a {@link RuntimeException} object is thrown it will be logged and <strong>not propagated</strong>.
* Throwing from this method is not supported and is considered a programming error.
*/
void process();
}
/**
* Manages the list of currently active streams. Queues any {@link Event}s that would modify the list of
* active streams in order to prevent modification while iterating.
*/
private final class ActiveStreams {
private final List<Listener> listeners;
private final Queue<Event> pendingEvents = new ArrayDeque<>(4);
private final Set<Http2Stream> streams = new LinkedHashSet<>();
private int pendingIterations;
ActiveStreams(List<Listener> listeners) {
this.listeners = listeners;
}
public int size() {
return streams.size();
}
public void activate(final DefaultStream stream) {
if (allowModifications()) {
addToActiveStreams(stream);
} else {
pendingEvents.add(() -> addToActiveStreams(stream));
}
}
public void deactivate(final DefaultStream stream, final Iterator<?> itr) {
if (allowModifications() || itr != null) {
removeFromActiveStreams(stream, itr);
} else {
pendingEvents.add(() -> removeFromActiveStreams(stream, itr));
}
}
public Http2Stream forEachActiveStream(Http2StreamVisitor visitor) throws Http2Exception {
incrementPendingIterations();
try {
for (Http2Stream stream : streams) {
if (!visitor.visit(stream)) {
return stream;
}
}
return null;
} finally {
decrementPendingIterations();
}
}
void addToActiveStreams(DefaultStream stream) {
if (streams.add(stream)) {
// Update the number of active streams initiated by the endpoint.
stream.createdBy().numActiveStreams++;
for (int i = 0; i < listeners.size(); i++) {
try {
listeners.get(i).onStreamActive(stream);
} catch (Throwable cause) {
logger.error("Caught Throwable from listener onStreamActive.", cause);
}
}
}
}
void removeFromActiveStreams(DefaultStream stream, Iterator<?> itr) {
if (streams.remove(stream)) {
// Update the number of active streams initiated by the endpoint.
stream.createdBy().numActiveStreams--;
notifyClosed(stream);
}
removeStream(stream, itr);
}
boolean allowModifications() {
return pendingIterations == 0;
}
void incrementPendingIterations() {
++pendingIterations;
}