2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2014 The btcsuite developers
|
2013-10-07 23:24:19 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-10-16 00:16:58 +02:00
|
|
|
//go:build !windows && !plan9
|
2013-10-16 21:45:53 +02:00
|
|
|
// +build !windows,!plan9
|
2013-10-07 23:24:19 +02:00
|
|
|
|
2014-01-07 22:30:25 +01:00
|
|
|
package limits
|
2013-10-07 23:24:19 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-24 19:25:27 +02:00
|
|
|
fileLimitWant = 2048 * 12
|
|
|
|
fileLimitMin = 2048 * 8
|
2013-10-07 23:24:19 +02:00
|
|
|
)
|
|
|
|
|
2015-01-28 16:44:56 +01:00
|
|
|
// SetLimits raises some process limits to values which allow btcd and
|
|
|
|
// associated utilities to run.
|
2014-01-07 22:30:25 +01:00
|
|
|
func SetLimits() error {
|
2013-10-07 23:24:19 +02:00
|
|
|
var rLimit syscall.Rlimit
|
|
|
|
|
|
|
|
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rLimit.Cur > fileLimitWant {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if rLimit.Max < fileLimitMin {
|
|
|
|
err = fmt.Errorf("need at least %v file descriptors",
|
|
|
|
fileLimitMin)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if rLimit.Max < fileLimitWant {
|
|
|
|
rLimit.Cur = rLimit.Max
|
|
|
|
} else {
|
|
|
|
rLimit.Cur = fileLimitWant
|
|
|
|
}
|
|
|
|
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err != nil {
|
|
|
|
// try min value
|
|
|
|
rLimit.Cur = fileLimitMin
|
|
|
|
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|