improve max dynamic streams calculation in streamsMap

This commit is contained in:
Lucas Clemente
2016-08-18 16:40:30 +02:00
parent c3832965d0
commit 993dd2cd5f
2 changed files with 16 additions and 15 deletions

View File

@@ -7,10 +7,7 @@ import (
"github.com/lucas-clemente/quic-go/protocol" "github.com/lucas-clemente/quic-go/protocol"
"github.com/lucas-clemente/quic-go/qerr" "github.com/lucas-clemente/quic-go/qerr"
) "github.com/lucas-clemente/quic-go/utils"
const (
maxNumStreams = int(float32(protocol.MaxStreamsPerConnection) * protocol.MaxStreamsMultiplier)
) )
type streamsMap struct { type streamsMap struct {
@@ -18,6 +15,7 @@ type streamsMap struct {
openStreams []protocol.StreamID openStreams []protocol.StreamID
mutex sync.RWMutex mutex sync.RWMutex
newStream newStreamLambda newStream newStreamLambda
maxNumStreams int
roundRobinIndex int roundRobinIndex int
} }
@@ -30,10 +28,13 @@ var (
) )
func newStreamsMap(newStream newStreamLambda) *streamsMap { func newStreamsMap(newStream newStreamLambda) *streamsMap {
maxNumStreams := utils.Max(int(float32(protocol.MaxIncomingDynamicStreams)*protocol.MaxStreamsMultiplier), int(protocol.MaxIncomingDynamicStreams))
return &streamsMap{ return &streamsMap{
streams: map[protocol.StreamID]*stream{}, streams: map[protocol.StreamID]*stream{},
openStreams: make([]protocol.StreamID, 0, maxNumStreams), openStreams: make([]protocol.StreamID, 0, maxNumStreams),
newStream: newStream, newStream: newStream,
maxNumStreams: maxNumStreams,
} }
} }
@@ -54,7 +55,7 @@ func (m *streamsMap) GetOrOpenStream(id protocol.StreamID) (*stream, error) {
if ok { if ok {
return s, nil return s, nil
} }
if len(m.openStreams) == maxNumStreams { if len(m.openStreams) == m.maxNumStreams {
return nil, qerr.TooManyOpenStreams return nil, qerr.TooManyOpenStreams
} }
if id%2 == 0 { if id%2 == 0 {

View File

@@ -60,16 +60,16 @@ var _ = Describe("Streams Map", func() {
Context("counting streams", func() { Context("counting streams", func() {
It("errors when too many streams are opened", func() { It("errors when too many streams are opened", func() {
for i := 0; i < maxNumStreams; i++ { for i := 0; i < m.maxNumStreams; i++ {
_, err := m.GetOrOpenStream(protocol.StreamID(i*2 + 1)) _, err := m.GetOrOpenStream(protocol.StreamID(i*2 + 1))
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
} }
_, err := m.GetOrOpenStream(protocol.StreamID(maxNumStreams)) _, err := m.GetOrOpenStream(protocol.StreamID(m.maxNumStreams))
Expect(err).To(MatchError(qerr.TooManyOpenStreams)) Expect(err).To(MatchError(qerr.TooManyOpenStreams))
}) })
It("does not error when many streams are opened and closed", func() { It("does not error when many streams are opened and closed", func() {
for i := 2; i < 10*maxNumStreams; i++ { for i := 2; i < 10*m.maxNumStreams; i++ {
_, err := m.GetOrOpenStream(protocol.StreamID(i*2 + 1)) _, err := m.GetOrOpenStream(protocol.StreamID(i*2 + 1))
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
m.RemoveStream(protocol.StreamID(i*2 + 1)) m.RemoveStream(protocol.StreamID(i*2 + 1))