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 3e78729

Browse files
fix: Add context to negotiate RTC func (#394)
* fix: Add context to negotiate RTC func * Wait for connection open with proper context * Add deadline if doesn't exist * Defer multiple cancels * Update wsnet/rtc.go Co-authored-by: Dean Sheather <[email protected]> * Fix var name Co-authored-by: Dean Sheather <[email protected]>
1 parent b1d9ef4 commit 3e78729

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

wsnet/dial.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func DialWebsocket(ctx context.Context, broker string, netOpts *DialOptions, wsO
5252
// We should close the socket intentionally.
5353
_ = conn.Close(websocket.StatusInternalError, "an error occurred")
5454
}()
55-
return Dial(nconn, netOpts)
55+
return Dial(ctx, nconn, netOpts)
5656
}
5757

5858
// Dial negotiates a connection to a listener.
59-
func Dial(conn net.Conn, options *DialOptions) (*Dialer, error) {
59+
func Dial(ctx context.Context, conn net.Conn, options *DialOptions) (*Dialer, error) {
6060
if options == nil {
6161
options = &DialOptions{}
6262
}
@@ -121,7 +121,7 @@ func Dial(conn net.Conn, options *DialOptions) (*Dialer, error) {
121121
connClosers: []io.Closer{ctrl},
122122
}
123123

124-
return dialer, dialer.negotiate()
124+
return dialer, dialer.negotiate(ctx)
125125
}
126126

127127
// Dialer enables arbitrary dialing to any network and address
@@ -138,7 +138,7 @@ type Dialer struct {
138138
pingMut sync.Mutex
139139
}
140140

141-
func (d *Dialer) negotiate() (err error) {
141+
func (d *Dialer) negotiate(ctx context.Context) (err error) {
142142
var (
143143
decoder = json.NewDecoder(d.conn)
144144
errCh = make(chan error)
@@ -153,7 +153,7 @@ func (d *Dialer) negotiate() (err error) {
153153
defer func() {
154154
_ = d.conn.Close()
155155
}()
156-
err := waitForConnectionOpen(context.Background(), d.rtc)
156+
err := waitForConnectionOpen(ctx, d.rtc)
157157
if err != nil {
158158
errCh <- err
159159
return

wsnet/rtc.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,16 @@ func waitForConnectionOpen(ctx context.Context, conn *webrtc.PeerConnection) err
242242
if conn.ConnectionState() == webrtc.PeerConnectionStateConnected {
243243
return nil
244244
}
245-
ctx, cancelFunc := context.WithTimeout(ctx, time.Second*15)
246-
defer cancelFunc()
245+
var cancel context.CancelFunc
246+
if _, deadlineSet := ctx.Deadline(); deadlineSet {
247+
ctx, cancel = context.WithCancel(ctx)
248+
} else {
249+
ctx, cancel = context.WithTimeout(ctx, time.Second*15)
250+
}
251+
defer cancel()
247252
conn.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) {
248253
if pcs == webrtc.PeerConnectionStateConnected {
249-
cancelFunc()
254+
cancel()
250255
}
251256
})
252257
<-ctx.Done()

0 commit comments

Comments
 (0)