diff options
author | Ricardo Domingos <ricardohsd@gmail.com> | 2018-01-12 04:55:21 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2018-01-12 04:55:21 +0800 |
commit | 56152b31ac251d1cc68fcddbdad159ba5234c415 (patch) | |
tree | 0b3bca4a858593df684a20a37b5598a483f4b17f | |
parent | 023769d9d48fab92c1d4d7d97c0a08ae3ef2cdca (diff) | |
download | dexon-56152b31ac251d1cc68fcddbdad159ba5234c415.tar dexon-56152b31ac251d1cc68fcddbdad159ba5234c415.tar.gz dexon-56152b31ac251d1cc68fcddbdad159ba5234c415.tar.bz2 dexon-56152b31ac251d1cc68fcddbdad159ba5234c415.tar.lz dexon-56152b31ac251d1cc68fcddbdad159ba5234c415.tar.xz dexon-56152b31ac251d1cc68fcddbdad159ba5234c415.tar.zst dexon-56152b31ac251d1cc68fcddbdad159ba5234c415.zip |
common/fdlimit: Move fdlimit files to separate package (#15850)
* common/fdlimit: Move fdlimit files to separate package
When go-ethereum is used as a library the calling program need to set
the FD limit.
This commit extract fdlimit files to a separate package so it can be
used outside of go-ethereum.
* common/fdlimit: Remove FdLimit from functions signature
* common/fdlimit: Rename fdlimit functions
-rw-r--r-- | cmd/utils/flags.go | 5 | ||||
-rw-r--r-- | common/fdlimit/fdlimit_freebsd.go (renamed from cmd/utils/fdlimit_freebsd.go) | 14 | ||||
-rw-r--r-- | common/fdlimit/fdlimit_test.go (renamed from cmd/utils/fdlimit_test.go) | 10 | ||||
-rw-r--r-- | common/fdlimit/fdlimit_unix.go (renamed from cmd/utils/fdlimit_unix.go) | 14 | ||||
-rw-r--r-- | common/fdlimit/fdlimit_windows.go (renamed from cmd/utils/fdlimit_windows.go) | 18 |
5 files changed, 31 insertions, 30 deletions
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index edf3dc2c2..94ab64c2e 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -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" @@ -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) } diff --git a/cmd/utils/fdlimit_freebsd.go b/common/fdlimit/fdlimit_freebsd.go index f9ed8937e..25caaafe2 100644 --- a/cmd/utils/fdlimit_freebsd.go +++ b/common/fdlimit/fdlimit_freebsd.go @@ -16,7 +16,7 @@ // +build freebsd -package utils +package fdlimit import "syscall" @@ -24,9 +24,9 @@ import "syscall" // 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 { @@ -43,9 +43,9 @@ 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 @@ -53,9 +53,9 @@ func getFdLimit() (int, error) { 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 diff --git a/cmd/utils/fdlimit_test.go b/common/fdlimit/fdlimit_test.go index 48489cf4c..05e9f0b65 100644 --- a/cmd/utils/fdlimit_test.go +++ b/common/fdlimit/fdlimit_test.go @@ -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" @@ -25,7 +25,7 @@ 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) } @@ -33,13 +33,13 @@ func TestFileDescriptorLimits(t *testing.T) { 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) } } diff --git a/cmd/utils/fdlimit_unix.go b/common/fdlimit/fdlimit_unix.go index c08d1fab0..27c7e783f 100644 --- a/cmd/utils/fdlimit_unix.go +++ b/common/fdlimit/fdlimit_unix.go @@ -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 { @@ -39,9 +39,9 @@ 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 @@ -49,9 +49,9 @@ func getFdLimit() (int, error) { 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 diff --git a/cmd/utils/fdlimit_windows.go b/common/fdlimit/fdlimit_windows.go index f239683d2..efcd3220e 100644 --- a/cmd/utils/fdlimit_windows.go +++ b/common/fdlimit/fdlimit_windows.go @@ -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 @@ -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() } |