Perform SnowflakeConn.Close() logic only once

Use synchronization to avoid a panic if SnowflakeConn.Close is called
more than once.
This commit is contained in:
Cecylia Bocovich 2024-10-24 11:08:24 -04:00
parent 028ff82683
commit a019fdaec9
No known key found for this signature in database
GPG key ID: 009DE379FD9B7B90

View file

@ -29,6 +29,7 @@ type Peers struct {
melt chan struct{}
collectLock sync.Mutex
closeOnce sync.Once
}
// NewPeers constructs a fresh container of remote peers.
@ -122,17 +123,19 @@ func (p *Peers) purgeClosedPeers() {
// End closes all active connections to Peers contained here, and stops the
// collection of future Peers.
func (p *Peers) End() {
close(p.melt)
p.collectLock.Lock()
defer p.collectLock.Unlock()
close(p.snowflakeChan)
cnt := p.Count()
for e := p.activePeers.Front(); e != nil; {
next := e.Next()
conn := e.Value.(*WebRTCPeer)
conn.Close()
p.activePeers.Remove(e)
e = next
}
log.Printf("WebRTC: melted all %d snowflakes.", cnt)
p.closeOnce.Do(func() {
close(p.melt)
p.collectLock.Lock()
defer p.collectLock.Unlock()
close(p.snowflakeChan)
cnt := p.Count()
for e := p.activePeers.Front(); e != nil; {
next := e.Next()
conn := e.Value.(*WebRTCPeer)
conn.Close()
p.activePeers.Remove(e)
e = next
}
log.Printf("WebRTC: melted all %d snowflakes.", cnt)
})
}