@@ -47,7 +47,7 @@ type Watcher struct {
47
47
memoryCurrentPath string
48
48
// memoryUsagePercentThreshold is the threshold at which the system is
49
49
// considered to be near OOM.
50
- memoryUsagePercentThreshold float64
50
+ memoryUsagePercentThreshold uint8
51
51
// interval is the interval at which to check for OOM.
52
52
interval time.Duration
53
53
// logger is the logger to use.
@@ -62,9 +62,13 @@ type Watcher struct {
62
62
}
63
63
64
64
// New returns a new Watcher.
65
- func New (memoryMaxPath , memoryCurrentPath string , memoryUsagePercentThreshold float64 , interval time.Duration , logger logr.Logger ) (* Watcher , error ) {
65
+ func New (memoryMaxPath , memoryCurrentPath string , memoryUsagePercentThreshold uint8 , interval time.Duration , logger logr.Logger ) (* Watcher , error ) {
66
66
if memoryUsagePercentThreshold < 1 || memoryUsagePercentThreshold > 100 {
67
- return nil , fmt .Errorf ("memory usage percent threshold must be between 1 and 100, got %.2f" , memoryUsagePercentThreshold )
67
+ return nil , fmt .Errorf ("memory usage percent threshold must be between 1 and 100, got %d" , memoryUsagePercentThreshold )
68
+ }
69
+
70
+ if minInterval := 50 * time .Millisecond ; interval < minInterval {
71
+ return nil , fmt .Errorf ("interval must be at least %s, got %s" , minInterval , interval )
68
72
}
69
73
70
74
if _ , err := os .Lstat (memoryCurrentPath ); err != nil {
@@ -86,7 +90,7 @@ func New(memoryMaxPath, memoryCurrentPath string, memoryUsagePercentThreshold fl
86
90
}
87
91
88
92
// NewDefault returns a new Watcher with default path values.
89
- func NewDefault (memoryUsagePercentThreshold float64 , interval time.Duration , logger logr.Logger ) (* Watcher , error ) {
93
+ func NewDefault (memoryUsagePercentThreshold uint8 , interval time.Duration , logger logr.Logger ) (* Watcher , error ) {
90
94
return New (
91
95
filepath .Join (DefaultCgroupPath , MemoryMaxFile ),
92
96
filepath .Join (DefaultCgroupPath , MemoryCurrentFile ),
@@ -128,13 +132,13 @@ func (w *Watcher) watchForNearOOM(ctx context.Context) {
128
132
}
129
133
130
134
currentPercentage := float64 (current ) / float64 (w .memoryMax ) * 100
131
- if currentPercentage >= w .memoryUsagePercentThreshold {
135
+ if currentPercentage >= float64 ( w .memoryUsagePercentThreshold ) {
132
136
w .logger .Info (fmt .Sprintf ("Memory usage is near OOM (%s/%s), shutting down" ,
133
137
formatSize (current ), formatSize (w .memoryMax )))
134
138
w .cancel ()
135
139
return
136
140
}
137
- w .logger .V (2 ).Info (fmt .Sprintf ("Current memory usage %s/%s (%.2f%% out of %.2f %%)" ,
141
+ w .logger .V (2 ).Info (fmt .Sprintf ("Current memory usage %s/%s (%.2f%% out of %d %%)" ,
138
142
formatSize (current ), formatSize (w .memoryMax ), currentPercentage , w .memoryUsagePercentThreshold ))
139
143
}
140
144
}
0 commit comments