forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulimit_unix.go
57 lines (44 loc) · 1.62 KB
/
ulimit_unix.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
//go:build linux || netbsd || openbsd
// +build linux netbsd openbsd
package ulimit
import (
"fmt"
"syscall"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/utils/logging"
)
const DefaultFDLimit = 32 * 1024
// Set attempts to bump the Rlimit which has a soft (Cur) and a hard (Max) value.
// The soft limit is what is used by the kernel to report EMFILE errors. The hard
// limit is a secondary limit which the process can be bumped to without additional
// privileges. Bumping the Max limit further would require superuser privileges.
// If the current Max is below our recommendation we will warn on start.
// see: http://0pointer.net/blog/file-descriptor-limits.html
func Set(max uint64, log logging.Logger) error {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
return fmt.Errorf("error getting rlimit: %w", err)
}
if max > rLimit.Max {
return fmt.Errorf("error fd-limit: (%d) greater than max: (%d)", max, rLimit.Max)
}
rLimit.Cur = max
// set new limit
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
return fmt.Errorf("error setting fd-limit: %w", err)
}
// verify limit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
return fmt.Errorf("error getting rlimit: %w", err)
}
if rLimit.Cur < DefaultFDLimit {
log.Warn("fd-limit is less than recommended and could result in reduced performance",
zap.Uint64("limit", rLimit.Cur),
zap.Uint64("recommendedLimit", DefaultFDLimit),
)
}
return nil
}