fix application bug
This commit is contained in:
87
vendor/github.com/lucas-clemente/quic-go/streams_map_incoming_uni.go
generated
vendored
87
vendor/github.com/lucas-clemente/quic-go/streams_map_incoming_uni.go
generated
vendored
@@ -17,32 +17,37 @@ type incomingUniStreamsMap struct {
|
||||
cond sync.Cond
|
||||
|
||||
streams map[protocol.StreamID]receiveStreamI
|
||||
// When a stream is deleted before it was accepted, we can't delete it immediately.
|
||||
// We need to wait until the application accepts it, and delete it immediately then.
|
||||
streamsToDelete map[protocol.StreamID]struct{} // used as a set
|
||||
|
||||
nextStream protocol.StreamID // the next stream that will be returned by AcceptStream()
|
||||
highestStream protocol.StreamID // the highest stream that the peer openend
|
||||
maxStream protocol.StreamID // the highest stream that the peer is allowed to open
|
||||
maxNumStreams int // 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 openend
|
||||
maxStream protocol.StreamID // the highest stream that the peer is allowed to open
|
||||
maxNumStreams uint64 // maximum number of streams
|
||||
|
||||
newStream func(protocol.StreamID) receiveStreamI
|
||||
queueMaxStreamID func(*wire.MaxStreamIDFrame)
|
||||
queueMaxStreamID func(*wire.MaxStreamsFrame)
|
||||
|
||||
closeErr error
|
||||
}
|
||||
|
||||
func newIncomingUniStreamsMap(
|
||||
nextStream protocol.StreamID,
|
||||
nextStreamToAccept protocol.StreamID,
|
||||
initialMaxStreamID protocol.StreamID,
|
||||
maxNumStreams int,
|
||||
maxNumStreams uint64,
|
||||
queueControlFrame func(wire.Frame),
|
||||
newStream func(protocol.StreamID) receiveStreamI,
|
||||
) *incomingUniStreamsMap {
|
||||
m := &incomingUniStreamsMap{
|
||||
streams: make(map[protocol.StreamID]receiveStreamI),
|
||||
nextStream: nextStream,
|
||||
maxStream: initialMaxStreamID,
|
||||
maxNumStreams: maxNumStreams,
|
||||
newStream: newStream,
|
||||
queueMaxStreamID: func(f *wire.MaxStreamIDFrame) { queueControlFrame(f) },
|
||||
streams: make(map[protocol.StreamID]receiveStreamI),
|
||||
streamsToDelete: make(map[protocol.StreamID]struct{}),
|
||||
nextStreamToAccept: nextStreamToAccept,
|
||||
nextStreamToOpen: nextStreamToAccept,
|
||||
maxStream: initialMaxStreamID,
|
||||
maxNumStreams: maxNumStreams,
|
||||
newStream: newStream,
|
||||
queueMaxStreamID: func(f *wire.MaxStreamsFrame) { queueControlFrame(f) },
|
||||
}
|
||||
m.cond.L = &m.mutex
|
||||
return m
|
||||
@@ -52,19 +57,28 @@ func (m *incomingUniStreamsMap) AcceptStream() (receiveStreamI, error) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
|
||||
var id protocol.StreamID
|
||||
var str receiveStreamI
|
||||
for {
|
||||
id = m.nextStreamToAccept
|
||||
var ok bool
|
||||
if m.closeErr != nil {
|
||||
return nil, m.closeErr
|
||||
}
|
||||
str, ok = m.streams[m.nextStream]
|
||||
str, ok = m.streams[id]
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
m.cond.Wait()
|
||||
}
|
||||
m.nextStream += 4
|
||||
m.nextStreamToAccept += 4
|
||||
// If this stream was completed before being accepted, we can delete it now.
|
||||
if _, ok := m.streamsToDelete[id]; ok {
|
||||
delete(m.streamsToDelete, id)
|
||||
if err := m.deleteStream(id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return str, nil
|
||||
}
|
||||
|
||||
@@ -77,8 +91,12 @@ func (m *incomingUniStreamsMap) GetOrOpenStream(id protocol.StreamID) (receiveSt
|
||||
// if the id 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 id <= m.highestStream {
|
||||
s := m.streams[id]
|
||||
if id < m.nextStreamToOpen {
|
||||
var s receiveStreamI
|
||||
// If the stream was already queued for deletion, and is just waiting to be accepted, don't return it.
|
||||
if _, ok := m.streamsToDelete[id]; !ok {
|
||||
s = m.streams[id]
|
||||
}
|
||||
m.mutex.RUnlock()
|
||||
return s, nil
|
||||
}
|
||||
@@ -88,17 +106,11 @@ func (m *incomingUniStreamsMap) GetOrOpenStream(id protocol.StreamID) (receiveSt
|
||||
// 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
|
||||
var start protocol.StreamID
|
||||
if m.highestStream == 0 {
|
||||
start = m.nextStream
|
||||
} else {
|
||||
start = m.highestStream + 4
|
||||
}
|
||||
for newID := start; newID <= id; newID += 4 {
|
||||
for newID := m.nextStreamToOpen; newID <= id; newID += 4 {
|
||||
m.streams[newID] = m.newStream(newID)
|
||||
m.cond.Signal()
|
||||
}
|
||||
m.highestStream = id
|
||||
m.nextStreamToOpen = id + 4
|
||||
s := m.streams[id]
|
||||
m.mutex.Unlock()
|
||||
return s, nil
|
||||
@@ -108,14 +120,33 @@ func (m *incomingUniStreamsMap) DeleteStream(id protocol.StreamID) error {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
|
||||
return m.deleteStream(id)
|
||||
}
|
||||
|
||||
func (m *incomingUniStreamsMap) deleteStream(id protocol.StreamID) error {
|
||||
if _, ok := m.streams[id]; !ok {
|
||||
return fmt.Errorf("Tried to delete unknown 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 id >= m.nextStreamToAccept {
|
||||
if _, ok := m.streamsToDelete[id]; ok {
|
||||
return fmt.Errorf("Tried to delete stream %d multiple times", id)
|
||||
}
|
||||
m.streamsToDelete[id] = struct{}{}
|
||||
return nil
|
||||
}
|
||||
|
||||
delete(m.streams, id)
|
||||
// queue a MAX_STREAM_ID frame, giving the peer the option to open a new stream
|
||||
if numNewStreams := m.maxNumStreams - len(m.streams); numNewStreams > 0 {
|
||||
m.maxStream = m.highestStream + protocol.StreamID(numNewStreams*4)
|
||||
m.queueMaxStreamID(&wire.MaxStreamIDFrame{StreamID: m.maxStream})
|
||||
if m.maxNumStreams > uint64(len(m.streams)) {
|
||||
numNewStreams := m.maxNumStreams - uint64(len(m.streams))
|
||||
m.maxStream = m.nextStreamToOpen + protocol.StreamID((numNewStreams-1)*4)
|
||||
m.queueMaxStreamID(&wire.MaxStreamsFrame{
|
||||
Type: protocol.StreamTypeUni,
|
||||
MaxStreams: m.maxStream.StreamNum(),
|
||||
})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user