identify connections by their local addr when adding to the multiplexer

This commit is contained in:
Marten Seemann 2019-09-03 19:40:02 +07:00
parent d689f9a392
commit 81be522bf3
4 changed files with 33 additions and 6 deletions

View file

@ -25,6 +25,7 @@ type mockPacketConn struct {
func newMockPacketConn() *mockPacketConn {
return &mockPacketConn{
addr: &net.UDPAddr{IP: net.IPv6zero, Port: 0x42},
dataToRead: make(chan []byte, 1000),
dataWritten: make(chan mockPacketConnWrite, 1000),
}

View file

@ -30,7 +30,7 @@ type connManager struct {
type connMultiplexer struct {
mutex sync.Mutex
conns map[net.PacketConn]connManager
conns map[string] /* LocalAddr().String() */ connManager
newPacketHandlerManager func(net.PacketConn, int, []byte, utils.Logger) packetHandlerManager // so it can be replaced in the tests
logger utils.Logger
@ -41,7 +41,7 @@ var _ multiplexer = &connMultiplexer{}
func getMultiplexer() multiplexer {
connMuxerOnce.Do(func() {
connMuxer = &connMultiplexer{
conns: make(map[net.PacketConn]connManager),
conns: make(map[string]connManager),
logger: utils.DefaultLogger.WithPrefix("muxer"),
newPacketHandlerManager: newPacketHandlerMap,
}
@ -57,7 +57,8 @@ func (m *connMultiplexer) AddConn(
m.mutex.Lock()
defer m.mutex.Unlock()
p, ok := m.conns[c]
laddr := c.LocalAddr().String()
p, ok := m.conns[laddr]
if !ok {
manager := m.newPacketHandlerManager(c, connIDLen, statelessResetKey, m.logger)
p = connManager{
@ -65,7 +66,7 @@ func (m *connMultiplexer) AddConn(
statelessResetKey: statelessResetKey,
manager: manager,
}
m.conns[c] = p
m.conns[laddr] = p
}
if p.connIDLen != connIDLen {
return nil, fmt.Errorf("cannot use %d byte connection IDs on a connection that is already using %d byte connction IDs", connIDLen, p.connIDLen)
@ -80,10 +81,11 @@ func (m *connMultiplexer) RemoveConn(c net.PacketConn) error {
m.mutex.Lock()
defer m.mutex.Unlock()
if _, ok := m.conns[c]; !ok {
laddr := c.LocalAddr().String()
if _, ok := m.conns[laddr]; !ok {
return fmt.Errorf("cannote remove connection, connection is unknown")
}
delete(m.conns, c)
delete(m.conns, laddr)
return nil
}

View file

@ -1,10 +1,17 @@
package quic
import (
"net"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
type testConn struct {
counter int
net.PacketConn
}
var _ = Describe("Client Multiplexer", func() {
It("adds a new packet conn ", func() {
conn := newMockPacketConn()
@ -12,6 +19,18 @@ var _ = Describe("Client Multiplexer", func() {
Expect(err).ToNot(HaveOccurred())
})
It("recognizes when the same connection is added twice", func() {
pconn := newMockPacketConn()
pconn.addr = &net.UDPAddr{IP: net.IPv4(1, 2, 3, 4), Port: 4321}
conn := testConn{PacketConn: pconn}
_, err := getMultiplexer().AddConn(conn, 8, nil)
Expect(err).ToNot(HaveOccurred())
conn.counter++
_, err = getMultiplexer().AddConn(conn, 8, nil)
Expect(err).ToNot(HaveOccurred())
Expect(getMultiplexer().(*connMultiplexer).conns).To(HaveLen(1))
})
It("errors when adding an existing conn with a different connection ID length", func() {
conn := newMockPacketConn()
_, err := getMultiplexer().AddConn(conn, 5, nil)

View file

@ -1,6 +1,8 @@
package quic
import (
"sync"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@ -17,6 +19,9 @@ var mockCtrl *gomock.Controller
var _ = BeforeEach(func() {
mockCtrl = gomock.NewController(GinkgoT())
// reset the sync.Once
connMuxerOnce = *new(sync.Once)
})
var _ = AfterEach(func() {