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

@@ -159,7 +159,9 @@ func ReadLoop(d io.Reader) {
// WriteLoop shows how to write to the datachannel directly
func WriteLoop(d io.Writer) {
for range time.NewTicker(5 * time.Second).C {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for range ticker.C {
message, err := randutil.GenerateCryptoRandomString(messageSize, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
if err != nil {
handleError(err)