Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit3e78729

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.goCo-authored-by: Dean Sheather <dean@coder.com>* Fix var nameCo-authored-by: Dean Sheather <dean@coder.com>
1 parentb1d9ef4 commit3e78729

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-
returnDial(nconn,netOpts)
55+
returnDial(ctx,nconn,netOpts)
5656
}
5757

5858
// Dial negotiates a connection to a listener.
59-
funcDial(conn net.Conn,options*DialOptions) (*Dialer,error) {
59+
funcDial(ctx context.Context,conn net.Conn,options*DialOptions) (*Dialer,error) {
6060
ifoptions==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-
returndialer,dialer.negotiate()
124+
returndialer,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() (errerror) {
141+
func (d*Dialer)negotiate(ctx context.Context) (errerror) {
142142
var (
143143
decoder=json.NewDecoder(d.conn)
144144
errCh=make(chanerror)
@@ -153,7 +153,7 @@ func (d *Dialer) negotiate() (err error) {
153153
deferfunc() {
154154
_=d.conn.Close()
155155
}()
156-
err:=waitForConnectionOpen(context.Background(),d.rtc)
156+
err:=waitForConnectionOpen(ctx,d.rtc)
157157
iferr!=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
ifconn.ConnectionState()==webrtc.PeerConnectionStateConnected {
243243
returnnil
244244
}
245-
ctx,cancelFunc:=context.WithTimeout(ctx,time.Second*15)
246-
defercancelFunc()
245+
varcancel 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+
defercancel()
247252
conn.OnConnectionStateChange(func(pcs webrtc.PeerConnectionState) {
248253
ifpcs==webrtc.PeerConnectionStateConnected {
249-
cancelFunc()
254+
cancel()
250255
}
251256
})
252257
<-ctx.Done()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp