Skip to content

Commit 20115d6

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

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

peer/conn.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -145,42 +145,42 @@ 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
154+
default:
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+
close(c.closedICE)
160+
}
150161
}
151162

152163
c.opts.Logger.Debug(context.Background(), "ice connection state updated",
153164
slog.F("state", iceConnectionState))
154-
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:
164-
close(c.closedICE)
165-
}
166-
}
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))
171-
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:
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+
if iceGatherState == webrtc.ICEGathererStateClosed {
175+
// pion/webrtc can update this state multiple times.
176+
// A connection can never become un-closed, so we
177+
// close the channel if it isn't already.
181178
close(c.closedICE)
182179
}
183180
}
181+
182+
c.opts.Logger.Debug(context.Background(), "ice gathering state updated",
183+
slog.F("state", iceGatherState))
184184
})
185185
c.rtc.OnConnectionStateChange(func(peerConnectionState webrtc.PeerConnectionState) {
186186
if c.isClosed() {

0 commit comments

Comments
 (0)