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