prysm icon indicating copy to clipboard operation
prysm copied to clipboard

use `time.NewTimer()` to avoid possible memory leaks

Open bnovil opened this issue 1 year ago • 0 comments

What type of PR is this?

Uncomment one line below and remove others.

Bug fix

What does this PR do? Why is it needed? Using time.After() in a loop may cause possible memory leaks. As Golang code comment says, Timer is not recovered until fires. In a loop, too many timers may be created and are garbage collected.

// After waits for the duration to elapse and then sends the current time
// on the returned channel.
// It is equivalent to NewTimer(d).C.
// The underlying Timer is not recovered by the garbage collector
// until the timer fires. If efficiency is a concern, use NewTimer
// instead and call Timer.Stop if the timer is no longer needed.
func After(d Duration) <-chan Time {
	return NewTimer(d).C
}

So this PR is fixing possible memory leaks by using time.NewTimer() and Reset()

Which issues(s) does this PR fix?

Fixes #

Other notes for review

bnovil avatar Mar 26 '24 08:03 bnovil