@@ -8,12 +8,17 @@ import (
8
8
"io"
9
9
"net"
10
10
"sync"
11
+ "sync/atomic"
11
12
"time"
12
13
13
14
"github.com/pion/logging"
14
15
"github.com/pion/transport/v3/packetio"
15
16
)
16
17
18
+ const (
19
+ iceConnectedTimeout = 25 * time .Second
20
+ )
21
+
17
22
type udpMuxedConnParams struct {
18
23
Mux * UDPMuxDefault
19
24
AddrPool * sync.Pool
@@ -33,13 +38,17 @@ type udpMuxedConn struct {
33
38
closedChan chan struct {}
34
39
closeOnce sync.Once
35
40
mu sync.Mutex
41
+
42
+ startAt time.Time
43
+ iceConnected atomic.Bool
36
44
}
37
45
38
46
func newUDPMuxedConn (params * udpMuxedConnParams ) * udpMuxedConn {
39
47
p := & udpMuxedConn {
40
48
params : params ,
41
49
buf : packetio .NewBuffer (),
42
50
closedChan : make (chan struct {}),
51
+ startAt : time .Now (),
43
52
}
44
53
45
54
return p
@@ -80,10 +89,18 @@ func (c *udpMuxedConn) WriteTo(buf []byte, rAddr net.Addr) (n int, err error) {
80
89
if c .isClosed () {
81
90
return 0 , io .ErrClosedPipe
82
91
}
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
+ }
87
104
}
88
105
89
106
return c .params .Mux .writeTo (buf , rAddr )
0 commit comments