@@ -8,12 +8,17 @@ import (
88 "io"
99 "net"
1010 "sync"
11+ "sync/atomic"
1112 "time"
1213
1314 "github.com/pion/logging"
1415 "github.com/pion/transport/v3/packetio"
1516)
1617
18+ const (
19+ iceConnectedTimeout = 25 * time .Second
20+ )
21+
1722type udpMuxedConnParams struct {
1823 Mux * UDPMuxDefault
1924 AddrPool * sync.Pool
@@ -33,13 +38,17 @@ type udpMuxedConn struct {
3338 closedChan chan struct {}
3439 closeOnce sync.Once
3540 mu sync.Mutex
41+
42+ startAt time.Time
43+ iceConnected atomic.Bool
3644}
3745
3846func newUDPMuxedConn (params * udpMuxedConnParams ) * udpMuxedConn {
3947 p := & udpMuxedConn {
4048 params : params ,
4149 buf : packetio .NewBuffer (),
4250 closedChan : make (chan struct {}),
51+ startAt : time .Now (),
4352 }
4453
4554 return p
@@ -80,10 +89,18 @@ func (c *udpMuxedConn) WriteTo(buf []byte, rAddr net.Addr) (n int, err error) {
8089 if c .isClosed () {
8190 return 0 , io .ErrClosedPipe
8291 }
83- // Each time we write to a new address, we'll register it with the mux
84- addr := rAddr .String ()
85- if ! c .containsAddress (addr ) {
86- c .addAddress (addr )
92+
93+ // Only check the address at the ICE connecting stage to reduce the check cost
94+ if ! c .iceConnected .Load () {
95+ if time .Since (c .startAt ) > iceConnectedTimeout {
96+ c .iceConnected .Store (true )
97+ } else {
98+ // Each time we write to a new address, we'll register it with the mux
99+ addr := rAddr .String ()
100+ if ! c .containsAddress (addr ) {
101+ c .addAddress (addr )
102+ }
103+ }
87104 }
88105
89106 return c .params .Mux .writeTo (buf , rAddr )
0 commit comments