diff --git a/connection.go b/connection.go index fd2b08c..6595991 100644 --- a/connection.go +++ b/connection.go @@ -12,6 +12,7 @@ import ( "os" "sort" "strings" + "sync" "unsafe" ) @@ -220,7 +221,7 @@ func (c *Connection) TransitionTo(version uint32, ALPN string) { c.CryptoStates = make(map[EncryptionLevel]*CryptoState) c.CryptoStreams = make(map[PNSpace]*Stream) c.CryptoStates[EncryptionLevelInitial] = NewInitialPacketProtection(c) - c.Streams = Streams{make(map[uint64]*Stream), &c.StreamInput} + c.Streams = Streams{streams: make(map[uint64]*Stream), lock: &sync.Mutex{}, input: &c.StreamInput} } func (c *Connection) CloseConnection(quicLayer bool, errCode uint16, reasonPhrase string) { if quicLayer { diff --git a/streams.go b/streams.go index 6f8ee12..40e8914 100644 --- a/streams.go +++ b/streams.go @@ -3,6 +3,7 @@ package quictracker import ( "fmt" "math" + "sync" ) type StreamsType bool @@ -45,13 +46,16 @@ type StreamInput struct { type Streams struct { streams map[uint64]*Stream - input *Broadcaster + lock *sync.Mutex + input *Broadcaster } func (s Streams) Get(streamId uint64) *Stream { + s.lock.Lock() if s.streams[streamId] == nil { s.streams[streamId] = NewStream() } + s.lock.Unlock() return s.streams[streamId] }