Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 7f0e87d

Browse files
authored
fix: Increase ICE timeouts (#392)
* fix: Increase ICE timeouts * Fix ping times
1 parent 08fe800 commit 7f0e87d

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

wsnet/dial.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ type Dialer struct {
137137
closedChan chan struct{}
138138
connClosers []io.Closer
139139
connClosersMut sync.Mutex
140+
pingMut sync.Mutex
140141
}
141142

142143
func (d *Dialer) negotiate() (err error) {
@@ -160,7 +161,7 @@ func (d *Dialer) negotiate() (err error) {
160161
return
161162
}
162163
d.rtc.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) {
163-
if pcs == webrtc.PeerConnectionStateConnected {
164+
if pcs != webrtc.PeerConnectionStateDisconnected {
164165
return
165166
}
166167

@@ -178,6 +179,7 @@ func (d *Dialer) negotiate() (err error) {
178179
default:
179180
}
180181
close(d.closedChan)
182+
_ = d.rtc.Close()
181183
})
182184
}()
183185

@@ -263,7 +265,7 @@ func (d *Dialer) Ping(ctx context.Context) error {
263265
// Since we control the client and server we could open this
264266
// data channel with `Negotiated` true to reduce traffic being
265267
// sent when the RTC connection is opened.
266-
err := waitForDataChannelOpen(context.Background(), d.ctrl)
268+
err := waitForDataChannelOpen(ctx, d.ctrl)
267269
if err != nil {
268270
return err
269271
}
@@ -273,13 +275,28 @@ func (d *Dialer) Ping(ctx context.Context) error {
273275
return err
274276
}
275277
}
278+
d.pingMut.Lock()
279+
defer d.pingMut.Unlock()
276280
_, err = d.ctrlrw.Write([]byte{'a'})
277281
if err != nil {
278282
return fmt.Errorf("write: %w", err)
279283
}
280-
b := make([]byte, 4)
281-
_, err = d.ctrlrw.Read(b)
282-
return err
284+
errCh := make(chan error)
285+
go func() {
286+
// There's a race in which connections can get lost-mid ping
287+
// in which case this would block forever.
288+
defer close(errCh)
289+
_, err = d.ctrlrw.Read(make([]byte, 4))
290+
errCh <- err
291+
}()
292+
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15)
293+
defer cancelFunc()
294+
select {
295+
case err := <-errCh:
296+
return err
297+
case <-ctx.Done():
298+
return ctx.Err()
299+
}
283300
}
284301

285302
// DialContext dials the network and address on the remote listener.

wsnet/rtc.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ func newPeerConnection(servers []webrtc.ICEServer, dialer proxy.Dialer) (*webrtc
160160
se.SetNetworkTypes([]webrtc.NetworkType{webrtc.NetworkTypeUDP4})
161161
se.SetSrflxAcceptanceMinWait(0)
162162
se.DetachDataChannels()
163-
se.SetICETimeouts(time.Second*3, time.Second*3, time.Second*2)
163+
// If the disconnect and keep-alive timeouts are too closely related, we'll
164+
// experience "random" connection failures.
165+
se.SetICETimeouts(time.Second*5, time.Second*25, time.Second*2)
164166
lf := logging.NewDefaultLoggerFactory()
165167
lf.DefaultLogLevel = logging.LogLevelDisabled
166168
se.LoggerFactory = lf

0 commit comments

Comments
 (0)