recycle timer.

This commit is contained in:
Raúl Kripalani 2020-12-09 16:13:27 +00:00
parent 5f00469e3a
commit 1df6596e03
2 changed files with 16 additions and 3 deletions

View File

@ -267,9 +267,19 @@ func SystemDriven(limit uint64, frequency time.Duration, policyCtor PolicyCtor)
// initialize the threshold. // initialize the threshold.
renewThreshold() renewThreshold()
// initialize an empty timer.
timer := Clock.Timer(0)
stopTimer := func() {
if !timer.Stop() {
<-timer.C
}
}
for { for {
timer.Reset(frequency)
select { select {
case <-Clock.After(frequency): case <-timer.C:
// get the current usage. // get the current usage.
if err := sysmemFn(&sysmem); err != nil { if err := sysmemFn(&sysmem); err != nil {
Logger.Warnf("failed to obtain system memory stats; err: %s", err) Logger.Warnf("failed to obtain system memory stats; err: %s", err)
@ -290,7 +300,10 @@ func SystemDriven(limit uint64, frequency time.Duration, policyCtor PolicyCtor)
renewThreshold() renewThreshold()
stopTimer()
case <-_watchdog.closing: case <-_watchdog.closing:
stopTimer()
return return
} }
} }

View File

@ -52,7 +52,7 @@ func TestControl(t *testing.T) {
var ms runtime.MemStats var ms runtime.MemStats
runtime.ReadMemStats(&ms) runtime.ReadMemStats(&ms)
require.LessOrEqual(t, ms.NumGC, uint32(8)) // a maximum of 8 GCs should've happened. require.LessOrEqual(t, ms.NumGC, uint32(5)) // a maximum of 8 GCs should've happened.
require.Zero(t, ms.NumForcedGC) // no forced GCs. require.Zero(t, ms.NumForcedGC) // no forced GCs.
} }
@ -91,7 +91,7 @@ func TestHeapDriven(t *testing.T) {
var ms runtime.MemStats var ms runtime.MemStats
runtime.ReadMemStats(&ms) runtime.ReadMemStats(&ms)
require.GreaterOrEqual(t, ms.NumGC, uint32(10)) // over 12 GCs should've taken place. require.GreaterOrEqual(t, ms.NumGC, uint32(9)) // over 9 GCs should've taken place.
} }
func TestSystemDriven(t *testing.T) { func TestSystemDriven(t *testing.T) {