mirror of
https://github.com/status-im/go-watchdog.git
synced 2025-02-16 03:06:50 +00:00
recycle timer.
This commit is contained in:
parent
5f00469e3a
commit
1df6596e03
15
watchdog.go
15
watchdog.go
@ -267,9 +267,19 @@ func SystemDriven(limit uint64, frequency time.Duration, policyCtor PolicyCtor)
|
||||
// initialize the threshold.
|
||||
renewThreshold()
|
||||
|
||||
// initialize an empty timer.
|
||||
timer := Clock.Timer(0)
|
||||
stopTimer := func() {
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
timer.Reset(frequency)
|
||||
|
||||
select {
|
||||
case <-Clock.After(frequency):
|
||||
case <-timer.C:
|
||||
// get the current usage.
|
||||
if err := sysmemFn(&sysmem); err != nil {
|
||||
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()
|
||||
|
||||
stopTimer()
|
||||
|
||||
case <-_watchdog.closing:
|
||||
stopTimer()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ func TestControl(t *testing.T) {
|
||||
|
||||
var ms runtime.MemStats
|
||||
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.
|
||||
}
|
||||
|
||||
@ -91,7 +91,7 @@ func TestHeapDriven(t *testing.T) {
|
||||
|
||||
var ms runtime.MemStats
|
||||
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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user