-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoom.go
47 lines (36 loc) · 947 Bytes
/
oom.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
package main
import (
"io/ioutil"
"os"
"strconv"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/sirupsen/logrus"
)
func configOutOfMemoryKiller() error {
// Magic "don't kill me" value as documented in
// <https://www.kernel.org/doc/Documentation/filesystems/proc.txt>
path := "/proc/self/oom_score_adj"
adjustValue := strconv.Itoa(-1000)
isPermanentError := func(err error) bool {
return err != nil && os.IsPermission(err)
}
fn := func() error {
err := ioutil.WriteFile(path, []byte(adjustValue), 0600)
if isPermanentError(err) {
return backoff.Permanent(err)
}
return err
}
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 10 * time.Millisecond
bo.MaxInterval = 100 * time.Millisecond
bo.MaxElapsedTime = 1 * time.Second
bo.Reset()
err := backoff.Retry(fn, bo)
if isPermanentError(err) {
logrus.Warningf("Setting OOM adjust score in %q: %s", path, err)
return nil
}
return err
}