Skip to content

Commit 4136d91

Browse files
committed
ci: Improve peer logging to help identify race
1 parent 2b922b1 commit 4136d91

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

peer/conn.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"crypto/rand"
7+
"crypto/sha256"
78
"io"
89
"sync"
910
"time"
@@ -140,6 +141,14 @@ type Conn struct {
140141

141142
func (c *Conn) init() error {
142143
c.rtc.OnNegotiationNeeded(c.negotiate)
144+
c.rtc.OnICEConnectionStateChange(func(is webrtc.ICEConnectionState) {
145+
c.opts.Logger.Debug(context.Background(), "ice connection updated",
146+
slog.F("state", is))
147+
})
148+
c.rtc.OnICEGatheringStateChange(func(is webrtc.ICEGathererState) {
149+
c.opts.Logger.Debug(context.Background(), "ice gathering state updated",
150+
slog.F("state", is))
151+
})
143152
c.rtc.OnICECandidate(func(iceCandidate *webrtc.ICECandidate) {
144153
if iceCandidate == nil {
145154
return
@@ -169,8 +178,7 @@ func (c *Conn) init() error {
169178
}
170179

171180
c.opts.Logger.Debug(context.Background(), "rtc connection updated",
172-
slog.F("state", pcs),
173-
slog.F("ice", c.rtc.ICEConnectionState()))
181+
slog.F("state", pcs))
174182

175183
switch pcs {
176184
case webrtc.PeerConnectionStateDisconnected:
@@ -311,15 +319,22 @@ func (c *Conn) negotiate() {
311319
c.pendingCandidatesMutex.Lock()
312320
defer c.pendingCandidatesMutex.Unlock()
313321
for _, pendingCandidate := range c.pendingRemoteCandidates {
314-
c.opts.Logger.Debug(context.Background(), "flushing remote candidate")
322+
hash := sha256.Sum224([]byte(pendingCandidate.Candidate))
323+
c.opts.Logger.Debug(context.Background(), "flushing buffered remote candidate",
324+
slog.F("hash", hash),
325+
slog.F("length", len(pendingCandidate.Candidate)),
326+
)
315327
err := c.rtc.AddICECandidate(pendingCandidate)
316328
if err != nil {
317-
_ = c.CloseWithError(xerrors.Errorf("flush pending candidates: %w", err))
329+
_ = c.CloseWithError(xerrors.Errorf("flush pending remote candidate: %w", err))
318330
return
319331
}
320332
}
333+
c.opts.Logger.Debug(context.Background(), "flushed buffered remote candidates",
334+
slog.F("count", len(c.pendingRemoteCandidates)),
335+
)
321336
c.pendingCandidatesFlushed = true
322-
c.opts.Logger.Debug(context.Background(), "flushed remote candidates")
337+
c.pendingRemoteCandidates = make([]webrtc.ICECandidateInit, 0)
323338
}
324339

325340
// LocalCandidate returns a channel that emits when a local candidate
@@ -332,12 +347,16 @@ func (c *Conn) LocalCandidate() <-chan webrtc.ICECandidateInit {
332347
func (c *Conn) AddRemoteCandidate(i webrtc.ICECandidateInit) error {
333348
c.pendingCandidatesMutex.Lock()
334349
defer c.pendingCandidatesMutex.Unlock()
350+
fields := []slog.Field{
351+
slog.F("hash", sha256.Sum224([]byte(i.Candidate))),
352+
slog.F("length", len(i.Candidate)),
353+
}
335354
if !c.pendingCandidatesFlushed {
336-
c.opts.Logger.Debug(context.Background(), "adding remote candidate to buffer")
355+
c.opts.Logger.Debug(context.Background(), "bufferring remote candidate", fields...)
337356
c.pendingRemoteCandidates = append(c.pendingRemoteCandidates, i)
338357
return nil
339358
}
340-
c.opts.Logger.Debug(context.Background(), "adding remote candidate")
359+
c.opts.Logger.Debug(context.Background(), "adding remote candidate", fields...)
341360
return c.rtc.AddICECandidate(i)
342361
}
343362

0 commit comments

Comments
 (0)