fix application bug

This commit is contained in:
Jeff
2019-05-13 11:19:18 +08:00
committed by zryfish
parent 996d6fe4c5
commit 5462f51e65
717 changed files with 87703 additions and 53426 deletions

View File

@@ -8,39 +8,44 @@ import (
"github.com/lucas-clemente/quic-go/internal/wire"
)
//go:generate genny -in $GOFILE -out streams_map_incoming_bidi.go gen "item=streamI Item=BidiStream"
//go:generate genny -in $GOFILE -out streams_map_incoming_uni.go gen "item=receiveStreamI Item=UniStream"
//go:generate genny -in $GOFILE -out streams_map_incoming_bidi.go gen "item=streamI Item=BidiStream streamTypeGeneric=protocol.StreamTypeBidi"
//go:generate genny -in $GOFILE -out streams_map_incoming_uni.go gen "item=receiveStreamI Item=UniStream streamTypeGeneric=protocol.StreamTypeUni"
type incomingItemsMap struct {
mutex sync.RWMutex
cond sync.Cond
streams map[protocol.StreamID]item
// 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) item
queueMaxStreamID func(*wire.MaxStreamIDFrame)
queueMaxStreamID func(*wire.MaxStreamsFrame)
closeErr error
}
func newIncomingItemsMap(
nextStream protocol.StreamID,
nextStreamToAccept protocol.StreamID,
initialMaxStreamID protocol.StreamID,
maxNumStreams int,
maxNumStreams uint64,
queueControlFrame func(wire.Frame),
newStream func(protocol.StreamID) item,
) *incomingItemsMap {
m := &incomingItemsMap{
streams: make(map[protocol.StreamID]item),
nextStream: nextStream,
maxStream: initialMaxStreamID,
maxNumStreams: maxNumStreams,
newStream: newStream,
queueMaxStreamID: func(f *wire.MaxStreamIDFrame) { queueControlFrame(f) },
streams: make(map[protocol.StreamID]item),
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
@@ -50,19 +55,28 @@ func (m *incomingItemsMap) AcceptStream() (item, error) {
m.mutex.Lock()
defer m.mutex.Unlock()
var id protocol.StreamID
var str item
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
}
@@ -75,8 +89,12 @@ func (m *incomingItemsMap) GetOrOpenStream(id protocol.StreamID) (item, error) {
// 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 item
// 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
}
@@ -86,17 +104,11 @@ func (m *incomingItemsMap) GetOrOpenStream(id protocol.StreamID) (item, error) {
// 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
@@ -106,14 +118,33 @@ func (m *incomingItemsMap) DeleteStream(id protocol.StreamID) error {
m.mutex.Lock()
defer m.mutex.Unlock()
return m.deleteStream(id)
}
func (m *incomingItemsMap) 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: streamTypeGeneric,
MaxStreams: m.maxStream.StreamNum(),
})
}
return nil
}