-
Notifications
You must be signed in to change notification settings - Fork 570
/
rlimit.go
56 lines (45 loc) · 1.36 KB
/
rlimit.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
52
53
54
55
56
// +build !windows,!freebsd
package main
import (
"fmt"
"syscall"
)
func (s *Supervisor) checkRequiredResources() error {
if minfds, vErr := s.getMinRequiredRes("minfds"); vErr == nil {
return s.checkMinLimit(syscall.RLIMIT_NOFILE, "NOFILE", minfds)
}
if minprocs, vErr := s.getMinRequiredRes("minprocs"); vErr == nil {
// RPROC = 6
return s.checkMinLimit(6, "NPROC", minprocs)
}
return nil
}
func (s *Supervisor) getMinRequiredRes(resourceName string) (uint64, error) {
if entry, ok := s.config.GetSupervisord(); ok {
value := uint64(entry.GetInt(resourceName, 0))
if value > 0 {
return value, nil
} else {
return 0, fmt.Errorf("No such key %s", resourceName)
}
} else {
return 0, fmt.Errorf("No supervisord section")
}
}
func (s *Supervisor) checkMinLimit(resource int, resourceName string, minRequiredSource uint64) error {
var limit syscall.Rlimit
if syscall.Getrlimit(resource, &limit) != nil {
return fmt.Errorf("fail to get the %s limit", resourceName)
}
if minRequiredSource > limit.Max {
return fmt.Errorf("%s %d is greater than Hard limit %d", resourceName, minRequiredSource, limit.Max)
}
if limit.Cur >= minRequiredSource {
return nil
}
limit.Cur = limit.Max
if syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit) != nil {
return fmt.Errorf(fmt.Sprintf("fail to set the %s to %d", resourceName, limit.Cur))
}
return nil
}