lbcd/limits/limits_unix.go

53 lines
1 KiB
Go
Raw Normal View History

2014-01-01 17:16:15 +01:00
// Copyright (c) 2013-2014 Conformal Systems LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
2013-10-16 21:45:53 +02:00
// +build !windows,!plan9
package limits
import (
"fmt"
"syscall"
)
const (
fileLimitWant = 2048
fileLimitMin = 1024
)
2015-01-28 16:44:56 +01:00
// 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
}