Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use preexisting connection in constructor if it's provided
Browse files Browse the repository at this point in the history
bashar-515 committed Jan 30, 2025

Verified

This commit was signed with the committer’s verified signature.
mjsir911 M Sirabella
1 parent 3a4a9d0 commit 503332b
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions rpc/wrtc_signaling_answerer.go
Original file line number Diff line number Diff line change
@@ -40,8 +40,9 @@ type webrtcSignalingAnswerer struct {
// conn is used to share the direct gRPC connection used by the answerer workers. As direct gRPC connections
// reconnect on their own, custom reconnect logic is not needed. However, keepalives are necessary for the connection
// to realize it's been disconnected quickly and start reconnecting.
connMu sync.Mutex
conn ClientConn
connMu sync.Mutex
conn ClientConn
sharedConn bool

logger utils.ZapCompatibleLogger
}
@@ -61,8 +62,12 @@ func newWebRTCSignalingAnswerer(
dialOptsCopy := make([]DialOption, len(dialOpts))
copy(dialOptsCopy, dialOpts)
dialOptsCopy = append(dialOptsCopy, WithWebRTCOptions(DialWebRTCOptions{Disable: true}))
options := &dialOptions{}
for _, opt := range dialOptsCopy {
opt.apply(options)
}
bgWorkers := utils.NewBackgroundStoppableWorkers()
return &webrtcSignalingAnswerer{
ans := &webrtcSignalingAnswerer{
address: address,
hosts: hosts,
server: server,
@@ -71,6 +76,11 @@ func newWebRTCSignalingAnswerer(
bgWorkers: bgWorkers,
logger: logger,
}
if options.conn != nil {
ans.conn = options.conn
ans.sharedConn = true
}
return ans
}

const (
@@ -88,28 +98,30 @@ func (ans *webrtcSignalingAnswerer) Start() {

// attempt to make connection in a loop
ans.bgWorkers.Add(func(ctx context.Context) {
for ans.conn == nil {
if ctx.Err() != nil {
return
}
if ans.sharedConn == false {

Check failure on line 101 in rpc/wrtc_signaling_answerer.go

GitHub Actions / Build and Test

S1002: should omit comparison to bool constant, can be simplified to `!ans.sharedConn` (gosimple)
for ans.conn == nil {
if ctx.Err() != nil {
return
}

timeout := answererConnectTimeout
// Bump timeout from 10 seconds to 1 minute if behind a SOCKS proxy. It
// may take longer to connect to the signaling server in that case.
if proxyAddr := os.Getenv(SocksProxyEnvVar); proxyAddr != "" {
timeout = answererConnectTimeoutBehindProxy
}
setupCtx, timeoutCancel := context.WithTimeout(ctx, timeout)
conn, err := Dial(setupCtx, ans.address, ans.logger, ans.dialOpts...)
timeoutCancel()
if err != nil {
ans.logger.Errorw("error connecting answer client", "error", err)
utils.SelectContextOrWait(ctx, answererReconnectWait)
continue
timeout := answererConnectTimeout
// Bump timeout from 10 seconds to 1 minute if behind a SOCKS proxy. It
// may take longer to connect to the signaling server in that case.
if proxyAddr := os.Getenv(SocksProxyEnvVar); proxyAddr != "" {
timeout = answererConnectTimeoutBehindProxy
}
setupCtx, timeoutCancel := context.WithTimeout(ctx, timeout)
conn, err := Dial(setupCtx, ans.address, ans.logger, ans.dialOpts...)
timeoutCancel()
if err != nil {
ans.logger.Errorw("error connecting answer client", "error", err)
utils.SelectContextOrWait(ctx, answererReconnectWait)
continue
}
ans.connMu.Lock()
ans.conn = conn
ans.connMu.Unlock()
}
ans.connMu.Lock()
ans.conn = conn
ans.connMu.Unlock()
}
// spin off the actual answerer workers
for i := 0; i < defaultMaxAnswerers; i++ {

0 comments on commit 503332b

Please sign in to comment.