Avoid leaking tickers

In Go 1.22 and earlier, a ticker needs to be explicitly stopped
when it's no longer useful in order to avoid a resource leak.
In Go 1.23 and later, an orphaned ticker will eventually be
garbage collected, but it's still more thrifty to stop it early.
This commit is contained in:
Juliusz Chroboczek
2024-08-01 10:39:30 +02:00
committed by Sean DuBois
parent cbbb1c29e5
commit f29ef99b22
15 changed files with 34 additions and 9 deletions

View File

@@ -113,7 +113,9 @@ func createAnswerer() *webrtc.PeerConnection {
since := time.Now()
// Start printing out the observed throughput
for range time.NewTicker(1000 * time.Millisecond).C {
ticker := time.NewTicker(1000 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
bps := float64(atomic.LoadUint64(&totalBytesReceived)*8) / time.Since(since).Seconds()
log.Printf("Throughput: %.03f Mbps", bps/1024/1024)
}