forked from ipfs/kubo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulimit.go
87 lines (71 loc) · 2.16 KB
/
ulimit.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package util
import (
"errors"
"fmt"
"os"
"strconv"
"syscall"
logging "gx/ipfs/QmbkT7eMTyXfpeyB3ZMxxcxg7XH8t6uXp49jqzz4HB7BGF/go-log"
)
var log = logging.Logger("ulimit")
var (
supportsFDManagement = false
// getlimit returns the soft and hard limits of file descriptors counts
getLimit func() (uint64, uint64, error)
// set limit sets the soft and hard limits of file descriptors counts
setLimit func(uint64, uint64) error
)
// maxFds is the maximum number of file descriptors that go-ipfs
// can use. The default value is 2048. This can be overwritten by the
// IPFS_FD_MAX env variable
var maxFds = uint64(2048)
// setMaxFds sets the maxFds value from IPFS_FD_MAX
// env variable if it's present on the system
func setMaxFds() {
// check if the IPFS_FD_MAX is set up and if it does
// not have a valid fds number notify the user
if val := os.Getenv("IPFS_FD_MAX"); val != "" {
fds, err := strconv.ParseUint(val, 10, 64)
if err != nil {
log.Errorf("bad value for IPFS_FD_MAX: %s", err)
return
}
maxFds = fds
}
}
// ManageFdLimit raise the current max file descriptor count
// of the process based on the IPFS_FD_MAX value
func ManageFdLimit() (changed bool, newLimit uint64, err error) {
if !supportsFDManagement {
return false, 0, nil
}
setMaxFds()
soft, hard, err := getLimit()
if err != nil {
return false, 0, err
}
if maxFds <= soft {
return false, 0, nil
}
// the soft limit is the value that the kernel enforces for the
// corresponding resource
// the hard limit acts as a ceiling for the soft limit
// an unprivileged process may only set it's soft limit to a
// alue in the range from 0 up to the hard limit
if err = setLimit(maxFds, maxFds); err != nil {
if err != syscall.EPERM {
return false, 0, fmt.Errorf("error setting: ulimit: %s", err)
}
// the process does not have permission so we should only
// set the soft value
if maxFds > hard {
return false, 0, errors.New(
"cannot set rlimit, IPFS_FD_MAX is larger than the hard limit",
)
}
if err = setLimit(maxFds, hard); err != nil {
return false, 0, fmt.Errorf("error setting ulimit wihout hard limit: %s", err)
}
}
return true, maxFds, nil
}