Skip to content

Commit d03f508

Browse files
committed
Invert closed log logic
1 parent 46260a8 commit d03f508

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

peer/conn.go

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -145,39 +145,39 @@ func (c *Conn) init() error {
145145

146146
c.rtc.OnNegotiationNeeded(c.negotiate)
147147
c.rtc.OnICEConnectionStateChange(func(iceConnectionState webrtc.ICEConnectionState) {
148-
if c.isClosed() {
148+
c.closedICEMutex.Lock()
149+
defer c.closedICEMutex.Unlock()
150+
select {
151+
case <-c.closedICE:
152+
// Don't log more state changes if we've already closed.
149153
return
150-
}
151-
152-
c.opts.Logger.Debug(context.Background(), "ice connection state updated",
153-
slog.F("state", iceConnectionState))
154+
default:
155+
c.opts.Logger.Debug(context.Background(), "ice connection state updated",
156+
slog.F("state", iceConnectionState))
154157

155-
if iceConnectionState == webrtc.ICEConnectionStateClosed {
156-
// pion/webrtc can update this state multiple times.
157-
// A connection can never become un-closed, so we
158-
// close the channel if it isn't already.
159-
c.closedICEMutex.Lock()
160-
defer c.closedICEMutex.Unlock()
161-
select {
162-
case <-c.closedICE:
163-
default:
158+
if iceConnectionState == webrtc.ICEConnectionStateClosed {
159+
// pion/webrtc can update this state multiple times.
160+
// A connection can never become un-closed, so we
161+
// close the channel if it isn't already.
164162
close(c.closedICE)
165163
}
166164
}
167165
})
168166
c.rtc.OnICEGatheringStateChange(func(iceGatherState webrtc.ICEGathererState) {
169-
c.opts.Logger.Debug(context.Background(), "ice gathering state updated",
170-
slog.F("state", iceGatherState))
167+
c.closedICEMutex.Lock()
168+
defer c.closedICEMutex.Unlock()
169+
select {
170+
case <-c.closedICE:
171+
// Don't log more state changes if we've already closed.
172+
return
173+
default:
174+
c.opts.Logger.Debug(context.Background(), "ice gathering state updated",
175+
slog.F("state", iceGatherState))
171176

172-
if iceGatherState == webrtc.ICEGathererStateClosed {
173-
// pion/webrtc can update this state multiple times.
174-
// A connection can never become un-closed, so we
175-
// close the channel if it isn't already.
176-
c.closedICEMutex.Lock()
177-
defer c.closedICEMutex.Unlock()
178-
select {
179-
case <-c.closedICE:
180-
default:
177+
if iceGatherState == webrtc.ICEGathererStateClosed {
178+
// pion/webrtc can update this state multiple times.
179+
// A connection can never become un-closed, so we
180+
// close the channel if it isn't already.
181181
close(c.closedICE)
182182
}
183183
}

0 commit comments

Comments
 (0)