remove closed clients from the multiplexer

This commit is contained in:
Marten Seemann 2018-07-02 14:56:12 +07:00
parent 7658f10a51
commit 0928e91e4d
5 changed files with 90 additions and 27 deletions

View file

@ -2,6 +2,7 @@ package quic
import (
"bytes"
"errors"
"net"
"strings"
"sync"
@ -22,7 +23,8 @@ var (
type clientMultiplexer struct {
mutex sync.Mutex
conns map[net.PacketConn]packetHandlerManager
conns map[net.PacketConn]packetHandlerManager
newPacketHandlerManager func() packetHandlerManager // so it can be replaced in the tests
logger utils.Logger
}
@ -30,29 +32,35 @@ type clientMultiplexer struct {
func getClientMultiplexer() *clientMultiplexer {
clientMuxerOnce.Do(func() {
clientMuxer = &clientMultiplexer{
conns: make(map[net.PacketConn]packetHandlerManager),
logger: utils.DefaultLogger.WithPrefix("client muxer"),
conns: make(map[net.PacketConn]packetHandlerManager),
logger: utils.DefaultLogger.WithPrefix("client muxer"),
newPacketHandlerManager: newPacketHandlerMap,
}
})
return clientMuxer
}
func (m *clientMultiplexer) Add(c net.PacketConn, connID protocol.ConnectionID, handler packetHandler) {
func (m *clientMultiplexer) AddConn(c net.PacketConn) packetHandlerManager {
m.mutex.Lock()
defer m.mutex.Unlock()
sessions, ok := m.conns[c]
if !ok {
sessions = newPacketHandlerMap()
sessions = m.newPacketHandlerManager()
m.conns[c] = sessions
// If we didn't know this packet conn before, listen for incoming packets
// and dispatch them to the right sessions.
go m.listen(c, sessions)
}
return sessions
}
func (m *clientMultiplexer) AddHandler(c net.PacketConn, connID protocol.ConnectionID, handler packetHandler) error {
sessions, ok := m.conns[c]
if !ok {
return errors.New("unknown packet conn %s")
}
sessions.Add(connID, handler)
if ok {
return
}
// If we didn't know this packet conn before, listen for incoming packets
// and dispatch them to the right sessions.
go m.listen(c, sessions)
return nil
}
func (m *clientMultiplexer) listen(c net.PacketConn, sessions packetHandlerManager) {
@ -83,6 +91,10 @@ func (m *clientMultiplexer) listen(c net.PacketConn, sessions packetHandlerManag
m.logger.Debugf("received a packet with an unexpected connection ID %s", iHdr.DestConnectionID)
continue
}
if client == nil {
// Late packet for closed session
continue
}
hdr, err := iHdr.Parse(r, protocol.PerspectiveServer, client.GetVersion())
if err != nil {
m.logger.Debugf("error parsing header from %s: %s", addr, err)