Repository navigation
Expand file tree
/
Copy pathinitializer.go
More file actions
362 lines (328 loc) · 9.82 KB
/
Copy pathinitializer.go
File metadata and controls
362 lines (328 loc) · 9.82 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
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
package http3
import (
"sync"
"gnalloy.org/gnalloy/channel"
)
// PipelineInitializer 是 bootstrap Channel 初始化回调的函数签名别名。
type PipelineInitializer = func(ch channel.Channel) error
// RequestStreamInitializer 创建 HTTP/3 bidirectional request stream 初始化器。
func RequestStreamInitializer(cfg PipelineConfig) PipelineInitializer {
return func(ch channel.Channel) error {
if ch == nil {
return ErrInvalidPipeline
}
return ApplyRequestStreamPipeline(ch.Pipeline(), cfg)
}
}
// RemoteControlStreamInitializer 创建 HTTP/3 对端 control stream 初始化器。
func RemoteControlStreamInitializer(cfg PipelineConfig) PipelineInitializer {
return func(ch channel.Channel) error {
if ch == nil {
return ErrInvalidPipeline
}
return ApplyRemoteControlStreamPipeline(ch.Pipeline(), cfg)
}
}
// LocalControlStreamInitializer 创建 HTTP/3 本端 control stream 初始化器。
func LocalControlStreamInitializer(cfg PipelineConfig) PipelineInitializer {
return func(ch channel.Channel) error {
if ch == nil {
return ErrInvalidPipeline
}
return ApplyLocalControlStreamPipeline(ch.Pipeline(), cfg)
}
}
// QPACKEncoderStreamInitializer 创建本端 QPACK encoder stream 初始化器。
func QPACKEncoderStreamInitializer() PipelineInitializer {
return func(ch channel.Channel) error {
if ch == nil {
return ErrInvalidPipeline
}
return ApplyQPACKEncoderStreamPipeline(ch.Pipeline())
}
}
// QPACKDecoderStreamInitializer 创建本端 QPACK decoder stream 初始化器。
func QPACKDecoderStreamInitializer() PipelineInitializer {
return func(ch channel.Channel) error {
if ch == nil {
return ErrInvalidPipeline
}
return ApplyQPACKDecoderStreamPipeline(ch.Pipeline())
}
}
type qpackDynamicEntry struct {
field HeaderField
size uint64
index uint64
}
// QPACKDynamicTable 维护 QPACK 动态表的容量、插入计数和驱逐顺序。
type QPACKDynamicTable struct {
capacity uint64
size uint64
insertCount uint64
entries []qpackDynamicEntry
}
// NewQPACKDynamicTable 创建动态表,容量为字节数。
func NewQPACKDynamicTable(capacity uint64) *QPACKDynamicTable {
return &QPACKDynamicTable{capacity: capacity}
}
// Capacity 返回当前动态表容量。
func (t *QPACKDynamicTable) Capacity() uint64 {
if t == nil {
return 0
}
return t.capacity
}
// Size 返回当前动态表占用字节数。
func (t *QPACKDynamicTable) Size() uint64 {
if t == nil {
return 0
}
return t.size
}
// InsertCount 返回 RFC 语义上的已插入条目数。
func (t *QPACKDynamicTable) InsertCount() uint64 {
if t == nil {
return 0
}
return t.insertCount
}
// SetCapacity 更新动态表容量,并立即按新容量驱逐旧条目。
func (t *QPACKDynamicTable) SetCapacity(capacity uint64) error {
if t == nil || capacity > maxVarInt {
return ErrInvalidVarInt
}
t.capacity = capacity
t.evict()
return nil
}
// Insert 写入一个新字段,返回该条目的 absolute index。
func (t *QPACKDynamicTable) Insert(field HeaderField) (uint64, error) {
if t == nil {
return 0, ErrQPACKInvalidInstruction
}
size := qpackEntrySize(field)
if size > t.capacity {
return 0, ErrQPACKEntryTooLarge
}
index := t.insertCount
t.insertCount++
t.entries = append([]qpackDynamicEntry{{field: field, size: size, index: index}}, t.entries...)
t.size += size
t.evict()
return index, nil
}
// Duplicate 复制 relative index 指向的条目,并把副本插入表头。
func (t *QPACKDynamicTable) Duplicate(relativeIndex uint64) (uint64, error) {
field, ok := t.GetRelative(relativeIndex)
if !ok {
return 0, ErrQPACKInvalidIndex
}
return t.Insert(field)
}
// GetRelative 按 QPACK relative index 获取字段,0 表示最新条目。
func (t *QPACKDynamicTable) GetRelative(index uint64) (HeaderField, bool) {
if t == nil || index >= uint64(len(t.entries)) {
return HeaderField{}, false
}
return t.entries[index].field, true
}
// GetAbsolute 按 absolute index 获取字段。
func (t *QPACKDynamicTable) GetAbsolute(index uint64) (HeaderField, bool) {
if t == nil {
return HeaderField{}, false
}
for i := range t.entries {
entry := t.entries[i]
if entry.index == index {
return entry.field, true
}
}
return HeaderField{}, false
}
func (t *QPACKDynamicTable) evict() {
for t.size > t.capacity && len(t.entries) > 0 {
last := len(t.entries) - 1
t.size -= t.entries[last].size
t.entries[last] = qpackDynamicEntry{}
t.entries = t.entries[:last]
}
}
func qpackEntrySize(field HeaderField) uint64 {
return uint64(len(field.Name) + len(field.Value) + 32)
}
// QPACKDynamicStateConfig 描述 QPACK 动态表和阻塞流限制。
type QPACKDynamicStateConfig struct {
MaxTableCapacity uint64
MaxBlockedStreams uint64
}
// QPACKDynamicState 串联 encoder/decoder stream 指令和动态表状态。
type QPACKDynamicState struct {
mu sync.Mutex
table QPACKDynamicTable
maxTableCapacity uint64
maxBlockedStreams uint64
blockedSections map[uint64]uint64
trackedSections map[uint64][]uint64
knownReceivedCount uint64
acknowledgedStreams map[uint64]struct{}
}
// NewQPACKDynamicState 创建 QPACK 动态状态机。
func NewQPACKDynamicState(cfg QPACKDynamicStateConfig) *QPACKDynamicState {
return &QPACKDynamicState{
table: *NewQPACKDynamicTable(cfg.MaxTableCapacity),
maxTableCapacity: cfg.MaxTableCapacity,
maxBlockedStreams: cfg.MaxBlockedStreams,
}
}
// DynamicTable 返回动态表快照。
func (s *QPACKDynamicState) DynamicTable() QPACKDynamicTable {
s.mu.Lock()
defer s.mu.Unlock()
return s.table
}
// KnownReceivedCount 返回 decoder stream 已确认的插入计数。
func (s *QPACKDynamicState) KnownReceivedCount() uint64 {
s.mu.Lock()
defer s.mu.Unlock()
return s.knownReceivedCount
}
// BlockedStreams 返回当前被动态表依赖阻塞的 stream 数量。
func (s *QPACKDynamicState) BlockedStreams() uint64 {
s.mu.Lock()
defer s.mu.Unlock()
return uint64(len(s.blockedSections))
}
// StartFieldSection 注册一个将要解码的 header section,并返回它是否需要等待动态表。
func (s *QPACKDynamicState) StartFieldSection(streamID uint64, requiredInsertCount uint64) (bool, error) {
s.mu.Lock()
defer s.mu.Unlock()
if requiredInsertCount == 0 || requiredInsertCount <= s.table.InsertCount() {
return false, nil
}
if s.blockedSections == nil {
s.blockedSections = make(map[uint64]uint64, 4)
}
if _, exists := s.blockedSections[streamID]; !exists && s.maxBlockedStreams > 0 && uint64(len(s.blockedSections)) >= s.maxBlockedStreams {
return false, ErrQPACKBlockedStreamsExceeded
}
s.blockedSections[streamID] = requiredInsertCount
return true, nil
}
// TrackFieldSection 记录本端已发送的 header section,用于处理 Section Acknowledgment。
func (s *QPACKDynamicState) TrackFieldSection(streamID uint64, requiredInsertCount uint64) error {
s.mu.Lock()
defer s.mu.Unlock()
if requiredInsertCount > s.table.InsertCount() {
return ErrQPACKInvalidInstruction
}
if requiredInsertCount == 0 {
return nil
}
if s.trackedSections == nil {
s.trackedSections = make(map[uint64][]uint64, 4)
}
s.trackedSections[streamID] = append(s.trackedSections[streamID], requiredInsertCount)
return nil
}
// ApplyEncoderInstruction 应用对端 encoder stream 指令。
func (s *QPACKDynamicState) ApplyEncoderInstruction(inst any) error {
s.mu.Lock()
defer s.mu.Unlock()
switch v := inst.(type) {
case QPACKSetDynamicTableCapacity:
if v.Capacity > s.maxTableCapacity {
return ErrQPACKCapacityExceeded
}
return s.table.SetCapacity(v.Capacity)
case QPACKInsertWithNameRef:
field, err := s.resolveName(v.Static, v.NameIndex)
if err != nil {
return err
}
field.Value = v.Value
if _, err := s.table.Insert(field); err != nil {
return err
}
s.releaseUnblocked()
return nil
case QPACKInsertWithoutNameRef:
if _, err := s.table.Insert(v.Field); err != nil {
return err
}
s.releaseUnblocked()
return nil
case QPACKDuplicate:
if _, err := s.table.Duplicate(v.Index); err != nil {
return err
}
s.releaseUnblocked()
return nil
default:
return ErrQPACKInvalidInstruction
}
}
// ApplyDecoderInstruction 应用对端 decoder stream 反馈指令。
func (s *QPACKDynamicState) ApplyDecoderInstruction(inst any) error {
s.mu.Lock()
defer s.mu.Unlock()
switch v := inst.(type) {
case QPACKSectionAcknowledgment:
return s.ackSection(v.StreamID)
case QPACKStreamCancellation:
delete(s.trackedSections, v.StreamID)
return nil
case QPACKInsertCountIncrement:
if v.Increment == 0 || s.knownReceivedCount+v.Increment > s.table.InsertCount() {
return ErrQPACKInvalidInstruction
}
s.knownReceivedCount += v.Increment
return nil
default:
return ErrQPACKInvalidInstruction
}
}
func (s *QPACKDynamicState) resolveName(static bool, index uint64) (HeaderField, error) {
if static {
field, ok := qpackStaticField(index)
if !ok {
return HeaderField{}, ErrQPACKInvalidIndex
}
return HeaderField{Name: field.Name}, nil
}
field, ok := s.table.GetRelative(index)
if !ok {
return HeaderField{}, ErrQPACKInvalidIndex
}
return HeaderField{Name: field.Name}, nil
}
func (s *QPACKDynamicState) releaseUnblocked() {
count := s.table.InsertCount()
for streamID, required := range s.blockedSections {
if required <= count {
delete(s.blockedSections, streamID)
}
}
}
func (s *QPACKDynamicState) ackSection(streamID uint64) error {
sections := s.trackedSections[streamID]
if len(sections) == 0 {
if s.acknowledgedStreams == nil {
s.acknowledgedStreams = make(map[uint64]struct{}, 4)
}
s.acknowledgedStreams[streamID] = struct{}{}
return nil
}
required := sections[0]
if len(sections) == 1 {
delete(s.trackedSections, streamID)
} else {
copy(sections, sections[1:])
sections[len(sections)-1] = 0
s.trackedSections[streamID] = sections[:len(sections)-1]
}
if required > s.knownReceivedCount {
s.knownReceivedCount = required
}
return nil
}