@@ -103,6 +103,12 @@ type ConnectInfo struct {
103103
104104type 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.
108114func 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 ,
0 commit comments