Skip to content

Commit

Permalink
udpmux: rwlock for muxer:routes map
Browse files Browse the repository at this point in the history
add fast read path to fetch existing routes
  • Loading branch information
ignoramous committed Aug 23, 2024
1 parent b568050 commit a0ad8a0
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions intra/udpmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type muxer struct {
cb func() // muxer.stop() callback (in a new goroutine)
vnd netstack.DemuxerFn // for new routes in netstack

rmu sync.Mutex // protects routes
rmu sync.RWMutex // protects routes
routes map[netip.AddrPort]*demuxconn // remote addr -> demuxed conn

dxconnWG *sync.WaitGroup // wait group for demuxed conns
Expand Down Expand Up @@ -104,7 +104,7 @@ func newMuxer(cid, pid string, conn net.PacketConn, vnd netstack.DemuxerFn, f fu
mxconn: conn,
stats: &stats{start: time.Now()},
routes: make(map[netip.AddrPort]*demuxconn),
rmu: sync.Mutex{},
rmu: sync.RWMutex{},
dxconns: make(chan *demuxconn),
doneCh: make(chan struct{}),
dxconnWG: &sync.WaitGroup{},
Expand All @@ -124,7 +124,7 @@ func (x *muxer) awaiters() {
log.D("udp: mux: %s awaiter: watching %s => %s", x.cid, c.laddr, c.raddr)
x.dxconnWG.Add(1) // accept
core.Gx("udpmux.vend.close", func() {
<-c.closed
<-c.closed // conn closed
x.unroute(c)
x.dxconnWG.Done() // unaccept
})
Expand Down Expand Up @@ -216,15 +216,25 @@ func (x *muxer) readers() {
}
}

func (x *muxer) route(to netip.AddrPort) *demuxconn {
x.rmu.Lock()
defer x.rmu.Unlock()
func (x *muxer) routeFast(to netip.AddrPort) *demuxconn {
x.rmu.RLock()
defer x.rmu.RUnlock()
return x.routes[to]
}

func (x *muxer) route(to netip.AddrPort) *demuxconn {
if !to.IsValid() {
log.W("udp: mux: %s route: invalid addr %s", x.cid, to)
return nil
}

if conn := x.routeFast(to); conn != nil {
return conn
}

x.rmu.Lock()
defer x.rmu.Unlock()

conn := x.routes[to]
if conn == nil {
// new routes created here won't really exist in netstack if
Expand Down

0 comments on commit a0ad8a0

Please sign in to comment.