@@ -192,24 +192,20 @@ type Dialer struct {
192
192
func (d * Dialer ) negotiate (ctx context.Context ) (err error ) {
193
193
var (
194
194
decoder = json .NewDecoder (d .conn )
195
- errCh = make (chan error )
195
+ errCh = make (chan error , 1 )
196
196
// If candidates are sent before an offer, we place them here.
197
197
// We currently have no assurances to ensure this can't happen,
198
198
// so it's better to buffer and process than fail.
199
199
pendingCandidates = []webrtc.ICECandidateInit {}
200
200
)
201
201
go func () {
202
202
defer close (errCh )
203
- defer func () {
204
- _ = d .conn .Close ()
205
- }()
203
+ defer func () { _ = d .conn .Close () }()
206
204
207
205
err := waitForConnectionOpen (context .Background (), d .rtc )
208
206
if err != nil {
209
207
d .log .Debug (ctx , "negotiation error" , slog .Error (err ))
210
- if errors .Is (err , context .DeadlineExceeded ) {
211
- _ = d .conn .Close ()
212
- }
208
+
213
209
errCh <- fmt .Errorf ("wait for connection to open: %w" , err )
214
210
return
215
211
}
@@ -325,23 +321,28 @@ func (d *Dialer) Ping(ctx context.Context) error {
325
321
return err
326
322
}
327
323
}
324
+
328
325
d .pingMut .Lock ()
329
326
defer d .pingMut .Unlock ()
327
+
330
328
d .log .Debug (ctx , "sending ping" )
331
329
_ , err = d .ctrlrw .Write ([]byte {'a' })
332
330
if err != nil {
333
331
return fmt .Errorf ("write: %w" , err )
334
332
}
335
- errCh := make (chan error )
333
+
334
+ errCh := make (chan error , 1 )
336
335
go func () {
337
336
// There's a race in which connections can get lost-mid ping
338
337
// in which case this would block forever.
339
338
defer close (errCh )
340
339
_ , err = d .ctrlrw .Read (make ([]byte , 4 ))
341
340
errCh <- err
342
341
}()
343
- ctx , cancelFunc := context .WithTimeout (ctx , time .Second * 15 )
344
- defer cancelFunc ()
342
+
343
+ ctx , cancel := context .WithTimeout (ctx , time .Second * 15 )
344
+ defer cancel ()
345
+
345
346
select {
346
347
case err := <- errCh :
347
348
return err
0 commit comments