-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathHttp2ConnectionState.java
More file actions
117 lines (100 loc) · 3.59 KB
/
Http2ConnectionState.java
File metadata and controls
117 lines (100 loc) · 3.59 KB
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
/*
* Copyright (c) 2014-2026 AsyncHttpClient Project. All rights reserved.
*
* Licensed 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.
*/
package org.asynchttpclient.netty.channel;
import io.netty.util.AttributeKey;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Tracks per-connection HTTP/2 state: active stream count, max concurrent streams,
* draining status (from GOAWAY), and pending stream openers.
*/
public class Http2ConnectionState {
public static final AttributeKey<Http2ConnectionState> HTTP2_STATE_KEY =
AttributeKey.valueOf("http2ConnectionState");
private final AtomicInteger activeStreams = new AtomicInteger(0);
private volatile int maxConcurrentStreams = Integer.MAX_VALUE;
private final AtomicBoolean draining = new AtomicBoolean(false);
private volatile int lastGoAwayStreamId = Integer.MAX_VALUE;
private final ConcurrentLinkedQueue<Runnable> pendingOpeners = new ConcurrentLinkedQueue<>();
private final Object pendingLock = new Object();
private volatile Object partitionKey;
public boolean tryAcquireStream() {
if (draining.get()) {
return false;
}
while (true) {
int current = activeStreams.get();
if (current >= maxConcurrentStreams) {
return false;
}
if (activeStreams.compareAndSet(current, current + 1)) {
return true;
}
}
}
public void releaseStream() {
activeStreams.decrementAndGet();
drainPendingOpeners();
}
public void addPendingOpener(Runnable opener) {
synchronized (pendingLock) {
if (tryAcquireStream()) {
opener.run();
} else {
pendingOpeners.add(opener);
}
}
}
private void drainPendingOpeners() {
synchronized (pendingLock) {
Runnable pending = pendingOpeners.poll();
if (pending != null) {
if (tryAcquireStream()) {
pending.run();
} else {
// Put it back — another releaseStream() will pick it up
pendingOpeners.offer(pending);
}
}
}
}
public void updateMaxConcurrentStreams(int maxConcurrentStreams) {
this.maxConcurrentStreams = maxConcurrentStreams;
}
public int getMaxConcurrentStreams() {
return maxConcurrentStreams;
}
public int getActiveStreams() {
return activeStreams.get();
}
public boolean isDraining() {
return draining.get();
}
public void setDraining(int lastStreamId) {
this.lastGoAwayStreamId = lastStreamId;
this.draining.set(true);
}
public int getLastGoAwayStreamId() {
return lastGoAwayStreamId;
}
public void setPartitionKey(Object partitionKey) {
this.partitionKey = partitionKey;
}
public Object getPartitionKey() {
return partitionKey;
}
}