mirror of
https://github.com/btcsuite/btcd.git
synced 2024-11-19 18:00:11 +01:00
51 lines
919 B
Go
51 lines
919 B
Go
// 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,!plan9
|
|
|
|
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
|
|
}
|