Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common/fdlimit: Move fdlimit files to separate package #15850

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/fdlimit"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/ethash"
Expand Down Expand Up @@ -721,10 +722,10 @@ func setIPC(ctx *cli.Context, cfg *node.Config) {
// makeDatabaseHandles raises out the number of allowed file handles per process
// for Geth and returns half of the allowance to assign to the database.
func makeDatabaseHandles() int {
if err := raiseFdLimit(2048); err != nil {
if err := fdlimit.Raise(2048); err != nil {
Fatalf("Failed to raise file descriptor allowance: %v", err)
}
limit, err := getFdLimit()
limit, err := fdlimit.Current()
if err != nil {
Fatalf("Failed to retrieve file descriptor allowance: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@

// +build freebsd

package utils
package fdlimit

import "syscall"

// This file is largely identical to fdlimit_unix.go,
// but Rlimit fields have type int64 on FreeBSD so it needs
// an extra conversion.

// raiseFdLimit tries to maximize the file descriptor allowance of this process
// Raise tries to maximize the file descriptor allowance of this process
// to the maximum hard-limit allowed by the OS.
func raiseFdLimit(max uint64) error {
func Raise(max uint64) error {
// Get the current limit
var limit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
Expand All @@ -43,19 +43,19 @@ func raiseFdLimit(max uint64) error {
return nil
}

// getFdLimit retrieves the number of file descriptors allowed to be opened by this
// Current retrieves the number of file descriptors allowed to be opened by this
// process.
func getFdLimit() (int, error) {
func Current() (int, error) {
var limit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
return 0, err
}
return int(limit.Cur), nil
}

// getFdMaxLimit retrieves the maximum number of file descriptors this process is
// Maximum retrieves the maximum number of file descriptors this process is
// allowed to request for itself.
func getFdMaxLimit() (int, error) {
func Maximum() (int, error) {
var limit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
return 0, err
Expand Down
10 changes: 5 additions & 5 deletions cmd/utils/fdlimit_test.go → common/fdlimit/fdlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package utils
package fdlimit

import (
"fmt"
Expand All @@ -25,21 +25,21 @@ import (
// per this process can be retrieved.
func TestFileDescriptorLimits(t *testing.T) {
target := 4096
hardlimit, err := getFdMaxLimit()
hardlimit, err := Maximum()
if err != nil {
t.Fatal(err)
}
if hardlimit < target {
t.Skip(fmt.Sprintf("system limit is less than desired test target: %d < %d", hardlimit, target))
}

if limit, err := getFdLimit(); err != nil || limit <= 0 {
if limit, err := Current(); err != nil || limit <= 0 {
t.Fatalf("failed to retrieve file descriptor limit (%d): %v", limit, err)
}
if err := raiseFdLimit(uint64(target)); err != nil {
if err := Raise(uint64(target)); err != nil {
t.Fatalf("failed to raise file allowance")
}
if limit, err := getFdLimit(); err != nil || limit < target {
if limit, err := Current(); err != nil || limit < target {
t.Fatalf("failed to retrieve raised descriptor limit (have %v, want %v): %v", limit, target, err)
}
}
14 changes: 7 additions & 7 deletions cmd/utils/fdlimit_unix.go → common/fdlimit/fdlimit_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

// +build linux darwin netbsd openbsd solaris

package utils
package fdlimit

import "syscall"

// raiseFdLimit tries to maximize the file descriptor allowance of this process
// Raise tries to maximize the file descriptor allowance of this process
// to the maximum hard-limit allowed by the OS.
func raiseFdLimit(max uint64) error {
func Raise(max uint64) error {
// Get the current limit
var limit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
Expand All @@ -39,19 +39,19 @@ func raiseFdLimit(max uint64) error {
return nil
}

// getFdLimit retrieves the number of file descriptors allowed to be opened by this
// Current retrieves the number of file descriptors allowed to be opened by this
// process.
func getFdLimit() (int, error) {
func Current() (int, error) {
var limit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
return 0, err
}
return int(limit.Cur), nil
}

// getFdMaxLimit retrieves the maximum number of file descriptors this process is
// Maximum retrieves the maximum number of file descriptors this process is
// allowed to request for itself.
func getFdMaxLimit() (int, error) {
func Maximum() (int, error) {
var limit syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
return 0, err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

package utils
package fdlimit

import "errors"

// raiseFdLimit tries to maximize the file descriptor allowance of this process
// Raise tries to maximize the file descriptor allowance of this process
// to the maximum hard-limit allowed by the OS.
func raiseFdLimit(max uint64) error {
func Raise(max uint64) error {
// This method is NOP by design:
// * Linux/Darwin counterparts need to manually increase per process limits
// * On Windows Go uses the CreateFile API, which is limited to 16K files, non
Expand All @@ -33,15 +33,15 @@ func raiseFdLimit(max uint64) error {
return nil
}

// getFdLimit retrieves the number of file descriptors allowed to be opened by this
// Current retrieves the number of file descriptors allowed to be opened by this
// process.
func getFdLimit() (int, error) {
// Please see raiseFdLimit for the reason why we use hard coded 16K as the limit
func Current() (int, error) {
// Please see Raise for the reason why we use hard coded 16K as the limit
return 16384, nil
}

// getFdMaxLimit retrieves the maximum number of file descriptors this process is
// Maximum retrieves the maximum number of file descriptors this process is
// allowed to request for itself.
func getFdMaxLimit() (int, error) {
return getFdLimit()
func Maximum() (int, error) {
return Current()
}