Skip to content

Commit 7cd375c

Browse files
authored
improve connection retries. (#862)
- exposing connectTimeout (for a single attempt) - reducing delay when trying subsequent regions (it used to take 1s)
1 parent a8ed4bd commit 7cd375c

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

room.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ type ConnectInfo struct {
103103

104104
type ConnectOption func(*signalling.ConnectParams)
105105

106+
func WithConnectTimeout(timeout time.Duration) ConnectOption {
107+
return func(p *signalling.ConnectParams) {
108+
p.ConnectTimeout = timeout
109+
}
110+
}
111+
106112
// WithAutoSubscribe sets whether the participant should automatically subscribe to tracks.
107113
// Default is true.
108114
func WithAutoSubscribe(val bool) ConnectOption {
@@ -337,7 +343,8 @@ func (r *Room) JoinWithToken(url, token string, opts ...ConnectOption) error {
337343
ctx := context.TODO()
338344

339345
params := &signalling.ConnectParams{
340-
AutoSubscribe: true,
346+
AutoSubscribe: true,
347+
ConnectTimeout: 3 * time.Second,
341348
}
342349
for _, opt := range opts {
343350
opt(params)
@@ -356,19 +363,13 @@ func (r *Room) JoinWithToken(url, token string, opts ...ConnectOption) error {
356363
break
357364
}
358365

359-
logger.Debugw("RTC engine joining room", "url", bestURL)
360-
// Not exposing this timeout as an option for now so that callers don't
361-
// set unrealistic values. We may reconsider in the future though.
362-
// 4 seconds chosen to balance the trade-offs:
363-
// - Too long, users will given up.
364-
// - Too short, risk frequently timing out on a request that would have
365-
// succeeded.
366-
callCtx, cancelCallCtx := context.WithTimeout(ctx, 4*time.Second)
366+
logger.Debugw("RTC engine joining room", "url", bestURL, "connectTimeout", params.ConnectTimeout)
367+
callCtx, cancelCallCtx := context.WithTimeout(ctx, params.ConnectTimeout)
367368
isSuccess, err = r.engine.JoinContext(callCtx, bestURL, token, params)
368369
cancelCallCtx()
369370
if err != nil {
370371
// try the next URL with exponential backoff
371-
d := time.Duration(1<<min(tries, 6)) * time.Second // max 64 seconds
372+
d := time.Duration(1<<min(tries, 6)) * 100 * time.Millisecond // max 6.4 seconds
372373
logger.Errorw(
373374
"failed to join room", err,
374375
"retrying in", d,

signalling/interfaces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package signalling
1717
import (
1818
"context"
1919
"net/http"
20+
"time"
2021

2122
"github.com/livekit/mediatransportutil/pkg/pacer"
2223
"github.com/livekit/protocol/livekit"
@@ -71,6 +72,8 @@ type ConnectParams struct {
7172
AutoSubscribe bool
7273
Reconnect bool
7374
DisableRegionDiscovery bool
75+
// timeout for each connection attempt, default is 3 seconds
76+
ConnectTimeout time.Duration
7477

7578
RetransmitBufferSize uint16
7679

0 commit comments

Comments
 (0)