-
Notifications
You must be signed in to change notification settings - Fork 1
/
limits.go
51 lines (42 loc) · 1.18 KB
/
limits.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
51
//go:build linux || darwin
// +build linux darwin
package runtime
// This package will only build on linux, requires the Linux (not FreeBDS or Windows) syscalls such as prlimit
// More info on build directives: https://pkg.go.dev/go/build
import (
"fmt"
"github.com/runner-x/runner-x/util/print"
"golang.org/x/sys/unix"
"runtime"
)
func NewLimiterOnSelf() *OnSelf {
return &OnSelf{}
}
func (s *OnSelf) ApplyLimits(rlimits *ResourceLimits) error {
switch runtime.GOOS {
case "darwin":
print.DebugPrintf("skip applying limits on Darwin")
return nil
case "linux":
return applyLimitsLinux(rlimits)
}
return nil
}
func applyLimitsLinux(rlimits *ResourceLimits) error {
// setrlimit syscall sets resource limits for the current process
err := unix.Setrlimit(unix.RLIMIT_NPROC, rlimits.NumProcesses)
if err != nil {
fmt.Printf("error setting NPROC rlimit: %v", err)
return err
}
err = unix.Setrlimit(unix.RLIMIT_FSIZE, rlimits.MaxFileSize)
if err != nil {
fmt.Printf("error setting FSIZE rlimit: %v", err)
return err
}
return nil
}
func (n *NilLimiter) ApplyLimits(rlimits *ResourceLimits) error {
// this is just for unit tests that isolate to just runtime tests
return nil
}