diff --git a/btcd.go b/btcd.go index c3da510d..1947418f 100644 --- a/btcd.go +++ b/btcd.go @@ -157,8 +157,14 @@ func main() { // Use all processor cores. runtime.GOMAXPROCS(runtime.NumCPU()) + // Up some limits. + err := setLimits() + if err != nil { + os.Exit(1) + } + // Work around defer not working after os.Exit() - err := btcdMain() + err = btcdMain() if err != nil { os.Exit(1) } diff --git a/limits_unix.go b/limits_unix.go new file mode 100644 index 00000000..44e2541c --- /dev/null +++ b/limits_unix.go @@ -0,0 +1,50 @@ +// Copyright (c) 2013 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +// +build !windows + +package main + +import ( + "fmt" + "syscall" +) + +const ( + fileLimitWant = 2048 + fileLimitMin = 1024 +) + +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 +} diff --git a/limits_windows.go b/limits_windows.go new file mode 100644 index 00000000..8c02ff9f --- /dev/null +++ b/limits_windows.go @@ -0,0 +1,9 @@ +// Copyright (c) 2013 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package main + +func setLimits() error { + return nil +}