1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty5.handler.codec.http2;
16
17 import io.netty5.util.internal.UnstableApi;
18
19 import java.util.ArrayDeque;
20 import java.util.Deque;
21
22 import static io.netty5.handler.codec.http2.Http2CodecUtil.DEFAULT_MIN_ALLOCATION_CHUNK;
23 import static io.netty5.handler.codec.http2.Http2CodecUtil.streamableBytes;
24 import static io.netty5.handler.codec.http2.Http2Error.INTERNAL_ERROR;
25 import static io.netty5.handler.codec.http2.Http2Exception.connectionError;
26 import static io.netty5.util.internal.ObjectUtil.checkPositive;
27 import static java.lang.Math.max;
28 import static java.lang.Math.min;
29 import static java.util.Objects.requireNonNull;
30
31
32
33
34
35
36
37 @UnstableApi
38 public final class UniformStreamByteDistributor implements StreamByteDistributor {
39 private final Http2Connection.PropertyKey stateKey;
40 private final Deque<State> queue = new ArrayDeque<>(4);
41
42
43
44
45
46 private int minAllocationChunk = DEFAULT_MIN_ALLOCATION_CHUNK;
47 private long totalStreamableBytes;
48
49 public UniformStreamByteDistributor(Http2Connection connection) {
50
51 stateKey = connection.newKey();
52 Http2Stream connectionStream = connection.connectionStream();
53 connectionStream.setProperty(stateKey, new State(connectionStream));
54
55
56 connection.addListener(new Http2ConnectionAdapter() {
57 @Override
58 public void onStreamAdded(Http2Stream stream) {
59 stream.setProperty(stateKey, new State(stream));
60 }
61
62 @Override
63 public void onStreamClosed(Http2Stream stream) {
64 state(stream).close();
65 }
66 });
67 }
68
69
70
71
72
73
74
75 public void minAllocationChunk(int minAllocationChunk) {
76 checkPositive(minAllocationChunk, "minAllocationChunk");
77 this.minAllocationChunk = minAllocationChunk;
78 }
79
80 @Override
81 public void updateStreamableBytes(StreamState streamState) {
82 state(streamState.stream()).updateStreamableBytes(streamableBytes(streamState),
83 streamState.hasFrame(),
84 streamState.windowSize());
85 }
86
87 @Override
88 public void updateDependencyTree(int childStreamId, int parentStreamId, short weight, boolean exclusive) {
89
90 }
91
92 @Override
93 public boolean distribute(int maxBytes, Writer writer) throws Http2Exception {
94 final int size = queue.size();
95 if (size == 0) {
96 return totalStreamableBytes > 0;
97 }
98
99 final int chunkSize = max(minAllocationChunk, maxBytes / size);
100
101 State state = queue.pollFirst();
102 do {
103 state.enqueued = false;
104 if (state.windowNegative) {
105 continue;
106 }
107 if (maxBytes == 0 && state.streamableBytes > 0) {
108
109
110
111 queue.addFirst(state);
112 state.enqueued = true;
113 break;
114 }
115
116
117 int chunk = min(chunkSize, min(maxBytes, state.streamableBytes));
118 maxBytes -= chunk;
119
120
121 state.write(chunk, writer);
122 } while ((state = queue.pollFirst()) != null);
123
124 return totalStreamableBytes > 0;
125 }
126
127 private State state(Http2Stream stream) {
128 return requireNonNull(stream, "stream").getProperty(stateKey);
129 }
130
131
132
133
134 private final class State {
135 final Http2Stream stream;
136 int streamableBytes;
137 boolean windowNegative;
138 boolean enqueued;
139 boolean writing;
140
141 State(Http2Stream stream) {
142 this.stream = stream;
143 }
144
145 void updateStreamableBytes(int newStreamableBytes, boolean hasFrame, int windowSize) {
146 assert hasFrame || newStreamableBytes == 0 :
147 "hasFrame: " + hasFrame + " newStreamableBytes: " + newStreamableBytes;
148
149 int delta = newStreamableBytes - streamableBytes;
150 if (delta != 0) {
151 streamableBytes = newStreamableBytes;
152 totalStreamableBytes += delta;
153 }
154
155
156
157
158
159
160
161 windowNegative = windowSize < 0;
162 if (hasFrame && (windowSize > 0 || windowSize == 0 && !writing)) {
163 addToQueue();
164 }
165 }
166
167
168
169
170
171 void write(int numBytes, Writer writer) throws Http2Exception {
172 writing = true;
173 try {
174
175 writer.write(stream, numBytes);
176 } catch (Throwable t) {
177 throw connectionError(INTERNAL_ERROR, t, "byte distribution write error");
178 } finally {
179 writing = false;
180 }
181 }
182
183 void addToQueue() {
184 if (!enqueued) {
185 enqueued = true;
186 queue.addLast(this);
187 }
188 }
189
190 void removeFromQueue() {
191 if (enqueued) {
192 enqueued = false;
193 queue.remove(this);
194 }
195 }
196
197 void close() {
198
199 removeFromQueue();
200
201
202 updateStreamableBytes(0, false, 0);
203 }
204 }
205 }