go-watchdog/adaptive.go

33 lines
819 B
Go
Raw Normal View History

2020-12-02 00:03:20 +00:00
package watchdog
// NewAdaptivePolicy creates a policy that forces GC when the usage surpasses a
// user-configured percentage (factor) of the available memory.
2020-12-02 00:03:20 +00:00
//
// This policy recalculates the next target as usage+(limit-usage)*factor, and
// forces immediate GC when used >= limit.
func NewAdaptivePolicy(factor float64) PolicyCtor {
return func(limit uint64) (Policy, error) {
return &adaptivePolicy{
factor: factor,
limit: limit,
}, nil
}
2020-12-02 00:03:20 +00:00
}
type adaptivePolicy struct {
factor float64
limit uint64
}
2020-12-02 00:03:20 +00:00
var _ Policy = (*adaptivePolicy)(nil)
2020-12-02 00:03:20 +00:00
func (p *adaptivePolicy) Evaluate(_ UtilizationType, used uint64) (next uint64, immediate bool) {
if used >= p.limit {
return used, true
2020-12-02 00:03:20 +00:00
}
available := float64(p.limit) - float64(used)
next = used + uint64(available*p.factor)
return next, false
2020-12-02 00:03:20 +00:00
}