-
Notifications
You must be signed in to change notification settings - Fork 24
/
policy.go
50 lines (40 loc) · 1.31 KB
/
policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package watchdog
import (
"context"
"runtime"
"time"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/durationpb"
watchdogv1 "github.com/fluxninja/aperture/api/gen/proto/go/aperture/watchdog/v1"
"github.com/fluxninja/aperture/pkg/log"
watchdogconfig "github.com/fluxninja/aperture/pkg/watchdog/config"
)
type usageFn func() (total uint64, usage uint64, err error)
func forceGC() time.Duration {
log.Info().Msg("watchdog is forcing GC")
start := time.Now()
runtime.GC()
took := time.Since(start)
log.Info().Dur("took", took).Msg("Watchdog triggered GC finished")
return took
}
func check(policy watchdogconfig.Policy, _ context.Context, fn usageFn) (proto.Message, error) {
total, used, err := fn()
if err != nil {
log.Warn().Err(err).Msg("failed to get memory usage")
return nil, nil
}
threshold := policy.NextThreshold(total, used)
result := &watchdogv1.WatchdogResult{
Used: used,
Threshold: threshold,
Total: total,
}
if used >= threshold {
log.Warn().Uint64("used", used).Uint64("threshold", threshold).Msg("Watchdog triggering GC")
result.ForceGcTook = durationpb.New(forceGC())
log.Warn().Msg("usage > threshold, force gc triggered")
}
log.Info().Uint64("used", used).Uint64("threshold", threshold).Msg("Memory utilization in bytes")
return result, nil
}