Skip to content

Commit

Permalink
feat: only check address at ice connecting stage
Browse files Browse the repository at this point in the history
  • Loading branch information
AshishKumar4 committed Dec 13, 2023
1 parent 5139807 commit 81df285
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions udp_muxed_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import (
"io"
"net"
"sync"
"sync/atomic"
"time"

"github.com/pion/logging"
"github.com/pion/transport/v3/packetio"
)

const (
iceConnectedTimeout = 25 * time.Second
)

type udpMuxedConnParams struct {
Mux *UDPMuxDefault
AddrPool *sync.Pool
Expand All @@ -33,13 +38,17 @@ type udpMuxedConn struct {
closedChan chan struct{}
closeOnce sync.Once
mu sync.Mutex

startAt time.Time
iceConnected atomic.Bool
}

func newUDPMuxedConn(params *udpMuxedConnParams) *udpMuxedConn {
p := &udpMuxedConn{
params: params,
buf: packetio.NewBuffer(),
closedChan: make(chan struct{}),
startAt: time.Now(),
}

return p
Expand Down Expand Up @@ -80,10 +89,18 @@ func (c *udpMuxedConn) WriteTo(buf []byte, rAddr net.Addr) (n int, err error) {
if c.isClosed() {
return 0, io.ErrClosedPipe
}
// Each time we write to a new address, we'll register it with the mux
addr := rAddr.String()
if !c.containsAddress(addr) {
c.addAddress(addr)

// Only check the address at the ICE connecting stage to reduce the check cost
if !c.iceConnected.Load() {
if time.Since(c.startAt) > iceConnectedTimeout {
c.iceConnected.Store(true)
} else {
// Each time we write to a new address, we'll register it with the mux
addr := rAddr.String()
if !c.containsAddress(addr) {
c.addAddress(addr)
}
}
}

return c.params.Mux.writeTo(buf, rAddr)
Expand Down

0 comments on commit 81df285

Please sign in to comment.