[server] Add support for increasing (or decreasing) descriptors count.

This commit is contained in:
Ciprian Dorin Craciun 2022-09-11 20:42:39 +03:00
parent 07fede788d
commit 7d58cd3eca
5 changed files with 91 additions and 22 deletions

View file

@ -96,6 +96,8 @@ var _seccompPhase1Syscalls = append ([]string {
"mmap", "mmap",
"setrlimit",
"seccomp", "seccomp",
"prctl", "prctl",

View file

@ -825,6 +825,7 @@ func main_0 () (error) {
var _dummyDelay time.Duration var _dummyDelay time.Duration
var _profileCpu string var _profileCpu string
var _profileMem string var _profileMem string
var _limitDescriptors uint
var _limitMemory uint var _limitMemory uint
var _seccompEnabled bool var _seccompEnabled bool
@ -876,6 +877,7 @@ func main_0 () (error) {
_dummyDelay_0 := _flags.Duration ("dummy-delay", 0, "") _dummyDelay_0 := _flags.Duration ("dummy-delay", 0, "")
_profileCpu_0 := _flags.String ("profile-cpu", "", "") _profileCpu_0 := _flags.String ("profile-cpu", "", "")
_profileMem_0 := _flags.String ("profile-mem", "", "") _profileMem_0 := _flags.String ("profile-mem", "", "")
_limitDescriptors_0 := _flags.Uint ("limit-descriptors", 0, "")
_limitMemory_0 := _flags.Uint ("limit-memory", 0, "") _limitMemory_0 := _flags.Uint ("limit-memory", 0, "")
_seccompEnabled_0 := _flags.Bool ("seccomp-enable", false, "") _seccompEnabled_0 := _flags.Bool ("seccomp-enable", false, "")
@ -913,6 +915,7 @@ func main_0 () (error) {
_dummyDelay = *_dummyDelay_0 _dummyDelay = *_dummyDelay_0
_profileCpu = *_profileCpu_0 _profileCpu = *_profileCpu_0
_profileMem = *_profileMem_0 _profileMem = *_profileMem_0
_limitDescriptors = *_limitDescriptors_0
_limitMemory = *_limitMemory_0 _limitMemory = *_limitMemory_0
_seccompEnabled = *_seccompEnabled_0 _seccompEnabled = *_seccompEnabled_0
@ -1025,6 +1028,9 @@ func main_0 () (error) {
AbortError (nil, "[b0177488] maximum number of allowed threads in total is 1024!") AbortError (nil, "[b0177488] maximum number of allowed threads in total is 1024!")
} }
if (_limitDescriptors != 0) && ((_limitDescriptors > (128 * 1024)) || (_limitDescriptors < 128)) {
AbortError (nil, "[10a440d7] maximum descriptors limit is between 128 and 131072!")
}
if (_limitMemory != 0) && ((_limitMemory > (16 * 1024)) || (_limitMemory < 128)) { if (_limitMemory != 0) && ((_limitMemory > (16 * 1024)) || (_limitMemory < 128)) {
AbortError (nil, "[2781f54c] maximum memory limit is between 128 and 16384 MiB!") AbortError (nil, "[2781f54c] maximum memory limit is between 128 and 16384 MiB!")
} }
@ -1056,6 +1062,19 @@ func main_0 () (error) {
if _seccompEnabled {
seccompApplyPhase1 ()
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
runtime.GOMAXPROCS (int (_threads)) runtime.GOMAXPROCS (int (_threads))
debug.SetGCPercent (50) debug.SetGCPercent (50)
@ -1066,11 +1085,20 @@ func main_0 () (error) {
_httpReduceMemory := false _httpReduceMemory := false
if _limitDescriptors > 0 {
if !_quiet && _isMaster {
log.Printf ("[ii] [33a2c5e9] [limits..] limiting descriptors to %d;\n", _limitDescriptors)
}
if _error := SysSetrlimitDescriptors (_limitDescriptors); _error != nil {
AbortError (_error, "[3dc44251] failed to configure descriptors limit!")
}
}
if _limitMemory > 0 { if _limitMemory > 0 {
if !_quiet && _isMaster { if !_quiet && _isMaster {
log.Printf ("[ii] [2c130d70] limiting memory to %d MiB;\n", _limitMemory) log.Printf ("[ii] [2c130d70] [limits..] limiting memory to %d MiB;\n", _limitMemory)
} }
if _error := SysSetrlimit (_limitMemory); _error != nil { if _error := SysSetrlimitMemory (_limitMemory); _error != nil {
AbortError (_error, "[4da96378] failed to configure memory limit!") AbortError (_error, "[4da96378] failed to configure memory limit!")
} }
} }
@ -1084,19 +1112,6 @@ func main_0 () (error) {
if _seccompEnabled {
seccompApplyPhase1 ()
}
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
if _processes > 1 { if _processes > 1 {
log.Printf ("[ii] [06f8c944] [master..] sub-processes starting (`%d` processes with `%d` threads each)...\n", _processes, _threads) log.Printf ("[ii] [06f8c944] [master..] sub-processes starting (`%d` processes with `%d` threads each)...\n", _processes, _threads)
@ -1176,6 +1191,9 @@ func main_0 () (error) {
if _timeoutDisabled { if _timeoutDisabled {
_processArguments = append (_processArguments, "--timeout-disable") _processArguments = append (_processArguments, "--timeout-disable")
} }
if _limitDescriptors != 0 {
_processArguments = append (_processArguments, "--limit-descriptors", fmt.Sprintf ("%d", _limitDescriptors))
}
if _limitMemory != 0 { if _limitMemory != 0 {
_processArguments = append (_processArguments, "--limit-memory", fmt.Sprintf ("%d", _limitMemory)) _processArguments = append (_processArguments, "--limit-memory", fmt.Sprintf ("%d", _limitMemory))
} }

View file

@ -9,7 +9,8 @@ import "syscall"
func SysSetrlimit (_limitMemory uint) (error) {
func SysSetrlimitMemory (_limitMemory uint) (error) {
{ {
_limitMb := (2 * _limitMemory) + (1 * 1024) _limitMb := (2 * _limitMemory) + (1 * 1024)
_limit := syscall.Rlimit { _limit := syscall.Rlimit {
@ -33,3 +34,19 @@ func SysSetrlimit (_limitMemory uint) (error) {
return nil return nil
} }
func SysSetrlimitDescriptors (_limitDescriptors uint) (error) {
{
_limit := syscall.Rlimit {
Cur : int64 (_limitDescriptors),
Max : int64 (_limitDescriptors),
}
if _error := syscall.Setrlimit (syscall.RLIMIT_NOFILE, &_limit); _error != nil {
return _error
}
}
return nil
}

View file

@ -10,7 +10,7 @@ import "syscall"
func SysSetrlimit (_limitMemory uint) (error) { func SysSetrlimitMemory (_limitMemory uint) (error) {
{ {
_limitMb := (2 * _limitMemory) + (1 * 1024) _limitMb := (2 * _limitMemory) + (1 * 1024)
_limit := syscall.Rlimit { _limit := syscall.Rlimit {
@ -34,3 +34,19 @@ func SysSetrlimit (_limitMemory uint) (error) {
return nil return nil
} }
func SysSetrlimitDescriptors (_limitDescriptors uint) (error) {
{
_limit := syscall.Rlimit {
Cur : uint64 (_limitDescriptors),
Max : uint64 (_limitDescriptors),
}
if _error := syscall.Setrlimit (syscall.RLIMIT_NOFILE, &_limit); _error != nil {
return _error
}
}
return nil
}

View file

@ -10,7 +10,7 @@ import "syscall"
func SysSetrlimit (_limitMemory uint) (error) { func SysSetrlimitMemory (_limitMemory uint) (error) {
{ {
_limitMb := _limitMemory _limitMb := _limitMemory
_limit := syscall.Rlimit { _limit := syscall.Rlimit {
@ -24,3 +24,19 @@ func SysSetrlimit (_limitMemory uint) (error) {
return nil return nil
} }
func SysSetrlimitDescriptors (_limitDescriptors uint) (error) {
{
_limit := syscall.Rlimit {
Cur : uint64 (_limitDescriptors),
Max : uint64 (_limitDescriptors),
}
if _error := syscall.Setrlimit (syscall.RLIMIT_NOFILE, &_limit); _error != nil {
return _error
}
}
return nil
}