simplify stream ID handling in the incoming streams map (#5207)

* simplify stream ID handling in the incoming streams map

No functional change expected.

* protocol: move stream ID constant definition
This commit is contained in:
Marten Seemann
2025-06-08 18:25:36 +08:00
committed by GitHub
parent 3a53b81692
commit 576aa1747f
6 changed files with 214 additions and 138 deletions

View File

@@ -2,6 +2,7 @@ package quic
import (
"context"
"fmt"
"sync"
"github.com/quic-go/quic-go/internal/protocol"
@@ -24,14 +25,14 @@ type incomingStreamsMap[T incomingStream] struct {
newStreamChan chan struct{}
streamType protocol.StreamType
streams map[protocol.StreamNum]incomingStreamEntry[T]
streams map[protocol.StreamID]incomingStreamEntry[T]
nextStreamToAccept protocol.StreamNum // the next stream that will be returned by AcceptStream()
nextStreamToOpen protocol.StreamNum // the highest stream that the peer opened
maxStream protocol.StreamNum // the highest stream that the peer is allowed to open
maxNumStreams uint64 // maximum number of streams
nextStreamToAccept protocol.StreamID // the next stream that will be returned by AcceptStream()
nextStreamToOpen protocol.StreamID // the highest stream that the peer opened
maxStream protocol.StreamID // the highest stream that the peer is allowed to open
maxNumStreams uint64 // maximum number of streams
newStream func(protocol.StreamNum) T
newStream func(protocol.StreamID) T
queueMaxStreamID func(*wire.MaxStreamsFrame)
closeErr error
@@ -39,19 +40,31 @@ type incomingStreamsMap[T incomingStream] struct {
func newIncomingStreamsMap[T incomingStream](
streamType protocol.StreamType,
newStream func(protocol.StreamNum) T,
newStream func(protocol.StreamID) T,
maxStreams uint64,
queueControlFrame func(wire.Frame),
pers protocol.Perspective,
) *incomingStreamsMap[T] {
var nextStreamToAccept protocol.StreamID
switch {
case streamType == protocol.StreamTypeBidi && pers == protocol.PerspectiveServer:
nextStreamToAccept = protocol.FirstIncomingBidiStreamServer
case streamType == protocol.StreamTypeBidi && pers == protocol.PerspectiveClient:
nextStreamToAccept = protocol.FirstIncomingBidiStreamClient
case streamType == protocol.StreamTypeUni && pers == protocol.PerspectiveServer:
nextStreamToAccept = protocol.FirstIncomingUniStreamServer
case streamType == protocol.StreamTypeUni && pers == protocol.PerspectiveClient:
nextStreamToAccept = protocol.FirstIncomingUniStreamClient
}
return &incomingStreamsMap[T]{
newStreamChan: make(chan struct{}, 1),
streamType: streamType,
streams: make(map[protocol.StreamNum]incomingStreamEntry[T]),
maxStream: protocol.StreamNum(maxStreams),
streams: make(map[protocol.StreamID]incomingStreamEntry[T]),
maxStream: protocol.StreamNum(maxStreams).StreamID(streamType, pers.Opposite()),
maxNumStreams: maxStreams,
newStream: newStream,
nextStreamToOpen: 1,
nextStreamToAccept: 1,
nextStreamToOpen: nextStreamToAccept,
nextStreamToAccept: nextStreamToAccept,
queueMaxStreamID: func(f *wire.MaxStreamsFrame) { queueControlFrame(f) },
}
}
@@ -65,16 +78,16 @@ func (m *incomingStreamsMap[T]) AcceptStream(ctx context.Context) (T, error) {
m.mutex.Lock()
var num protocol.StreamNum
var id protocol.StreamID
var entry incomingStreamEntry[T]
for {
num = m.nextStreamToAccept
id = m.nextStreamToAccept
if m.closeErr != nil {
m.mutex.Unlock()
return *new(T), m.closeErr
}
var ok bool
entry, ok = m.streams[num]
entry, ok = m.streams[id]
if ok {
break
}
@@ -86,10 +99,10 @@ func (m *incomingStreamsMap[T]) AcceptStream(ctx context.Context) (T, error) {
}
m.mutex.Lock()
}
m.nextStreamToAccept++
m.nextStreamToAccept += 4
// If this stream was completed before being accepted, we can delete it now.
if entry.shouldDelete {
if err := m.deleteStream(num); err != nil {
if err := m.deleteStream(id); err != nil {
m.mutex.Unlock()
return *new(T), err
}
@@ -98,22 +111,19 @@ func (m *incomingStreamsMap[T]) AcceptStream(ctx context.Context) (T, error) {
return entry.stream, nil
}
func (m *incomingStreamsMap[T]) GetOrOpenStream(num protocol.StreamNum) (T, error) {
func (m *incomingStreamsMap[T]) GetOrOpenStream(id protocol.StreamID) (T, error) {
m.mutex.RLock()
if num > m.maxStream {
if id > m.maxStream {
m.mutex.RUnlock()
return *new(T), streamError{
message: "peer tried to open stream %d (current limit: %d)",
nums: []protocol.StreamNum{num, m.maxStream},
}
return *new(T), fmt.Errorf("peer tried to open stream %d (current limit: %d)", id, m.maxStream)
}
// if the num is smaller than the highest we accepted
// * this stream exists in the map, and we can return it, or
// * this stream was already closed, then we can return the nil
if num < m.nextStreamToOpen {
if id < m.nextStreamToOpen {
var s T
// If the stream was already queued for deletion, and is just waiting to be accepted, don't return it.
if entry, ok := m.streams[num]; ok && !entry.shouldDelete {
if entry, ok := m.streams[id]; ok && !entry.shouldDelete {
s = entry.stream
}
m.mutex.RUnlock()
@@ -125,59 +135,53 @@ func (m *incomingStreamsMap[T]) GetOrOpenStream(num protocol.StreamNum) (T, erro
// no need to check the two error conditions from above again
// * maxStream can only increase, so if the id was valid before, it definitely is valid now
// * highestStream is only modified by this function
for newNum := m.nextStreamToOpen; newNum <= num; newNum++ {
for newNum := m.nextStreamToOpen; newNum <= id; newNum += 4 {
m.streams[newNum] = incomingStreamEntry[T]{stream: m.newStream(newNum)}
select {
case m.newStreamChan <- struct{}{}:
default:
}
}
m.nextStreamToOpen = num + 1
entry := m.streams[num]
m.nextStreamToOpen = id + 4
entry := m.streams[id]
m.mutex.Unlock()
return entry.stream, nil
}
func (m *incomingStreamsMap[T]) DeleteStream(num protocol.StreamNum) error {
func (m *incomingStreamsMap[T]) DeleteStream(id protocol.StreamID) error {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.deleteStream(num)
return m.deleteStream(id)
}
func (m *incomingStreamsMap[T]) deleteStream(num protocol.StreamNum) error {
if _, ok := m.streams[num]; !ok {
return streamError{
message: "tried to delete unknown incoming stream %d",
nums: []protocol.StreamNum{num},
}
func (m *incomingStreamsMap[T]) deleteStream(id protocol.StreamID) error {
if _, ok := m.streams[id]; !ok {
return fmt.Errorf("tried to delete unknown incoming stream %d", id)
}
// Don't delete this stream yet, if it was not yet accepted.
// Just save it to streamsToDelete map, to make sure it is deleted as soon as it gets accepted.
if num >= m.nextStreamToAccept {
entry, ok := m.streams[num]
if id >= m.nextStreamToAccept {
entry, ok := m.streams[id]
if ok && entry.shouldDelete {
return streamError{
message: "tried to delete incoming stream %d multiple times",
nums: []protocol.StreamNum{num},
}
return fmt.Errorf("tried to delete incoming stream %d multiple times", id)
}
entry.shouldDelete = true
m.streams[num] = entry // can't assign to struct in map, so we need to reassign
m.streams[id] = entry // can't assign to struct in map, so we need to reassign
return nil
}
delete(m.streams, num)
delete(m.streams, id)
// queue a MAX_STREAM_ID frame, giving the peer the option to open a new stream
if m.maxNumStreams > uint64(len(m.streams)) {
maxStream := m.nextStreamToOpen + protocol.StreamNum(m.maxNumStreams-uint64(len(m.streams))) - 1
// Never send a value larger than protocol.MaxStreamCount.
if maxStream <= protocol.MaxStreamCount {
maxStream := m.nextStreamToOpen + 4*protocol.StreamID(m.maxNumStreams-uint64(len(m.streams))-1)
// never send a value larger than the maximum value for a stream number
if maxStream <= protocol.MaxStreamID {
m.maxStream = maxStream
m.queueMaxStreamID(&wire.MaxStreamsFrame{
Type: m.streamType,
MaxStreamNum: m.maxStream,
MaxStreamNum: m.maxStream.StreamNum(),
})
}
}