btcd/limits/limits_unix.go
Calvin Kim ba5407615d multi: Run gofmt on the entire repository
The doc formatting changes introduced in the recent go version is
increasing the diff for all of the new commits.  Formatting it all in
this commit will help the readability of future PRs by reducing the
diff.
2023-06-21 22:31:09 +09:00

54 lines
1.0 KiB
Go

// Copyright (c) 2013-2014 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
//go:build !windows && !plan9
// +build !windows,!plan9
package limits
import (
"fmt"
"syscall"
)
const (
fileLimitWant = 2048
fileLimitMin = 1024
)
// SetLimits raises some process limits to values which allow btcd and
// associated utilities to run.
func SetLimits() error {
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
}