Skip to content

Commit 56152b3

Browse files
ricardohsdkaralabe
authored andcommitted
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
1 parent 023769d commit 56152b3

5 files changed

Lines changed: 31 additions & 30 deletions

File tree

‎cmd/utils/flags.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/ethereum/go-ethereum/accounts"
3232
"github.com/ethereum/go-ethereum/accounts/keystore"
3333
"github.com/ethereum/go-ethereum/common"
34+
"github.com/ethereum/go-ethereum/common/fdlimit"
3435
"github.com/ethereum/go-ethereum/consensus"
3536
"github.com/ethereum/go-ethereum/consensus/clique"
3637
"github.com/ethereum/go-ethereum/consensus/ethash"
@@ -721,10 +722,10 @@ func setIPC(ctx *cli.Context, cfg *node.Config) {
721722
// makeDatabaseHandles raises out the number of allowed file handles per process
722723
// for Geth and returns half of the allowance to assign to the database.
723724
func makeDatabaseHandles() int {
724-
if err := raiseFdLimit(2048); err != nil {
725+
if err := fdlimit.Raise(2048); err != nil {
725726
Fatalf("Failed to raise file descriptor allowance: %v", err)
726727
}
727-
limit, err := getFdLimit()
728+
limit, err := fdlimit.Current()
728729
if err != nil {
729730
Fatalf("Failed to retrieve file descriptor allowance: %v", err)
730731
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
// +build freebsd
1818

19-
package utils
19+
package fdlimit
2020

2121
import "syscall"
2222

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

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

46-
// getFdLimit retrieves the number of file descriptors allowed to be opened by this
46+
// Current retrieves the number of file descriptors allowed to be opened by this
4747
// process.
48-
func getFdLimit() (int, error) {
48+
func Current() (int, error) {
4949
var limit syscall.Rlimit
5050
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
5151
return 0, err
5252
}
5353
return int(limit.Cur), nil
5454
}
5555

56-
// getFdMaxLimit retrieves the maximum number of file descriptors this process is
56+
// Maximum retrieves the maximum number of file descriptors this process is
5757
// allowed to request for itself.
58-
func getFdMaxLimit() (int, error) {
58+
func Maximum() (int, error) {
5959
var limit syscall.Rlimit
6060
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
6161
return 0, err
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package utils
17+
package fdlimit
1818

1919
import (
2020
"fmt"
@@ -25,21 +25,21 @@ import (
2525
// per this process can be retrieved.
2626
func TestFileDescriptorLimits(t *testing.T) {
2727
target := 4096
28-
hardlimit, err := getFdMaxLimit()
28+
hardlimit, err := Maximum()
2929
if err != nil {
3030
t.Fatal(err)
3131
}
3232
if hardlimit < target {
3333
t.Skip(fmt.Sprintf("system limit is less than desired test target: %d < %d", hardlimit, target))
3434
}
3535

36-
if limit, err := getFdLimit(); err != nil || limit <= 0 {
36+
if limit, err := Current(); err != nil || limit <= 0 {
3737
t.Fatalf("failed to retrieve file descriptor limit (%d): %v", limit, err)
3838
}
39-
if err := raiseFdLimit(uint64(target)); err != nil {
39+
if err := Raise(uint64(target)); err != nil {
4040
t.Fatalf("failed to raise file allowance")
4141
}
42-
if limit, err := getFdLimit(); err != nil || limit < target {
42+
if limit, err := Current(); err != nil || limit < target {
4343
t.Fatalf("failed to retrieve raised descriptor limit (have %v, want %v): %v", limit, target, err)
4444
}
4545
}
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
// +build linux darwin netbsd openbsd solaris
1818

19-
package utils
19+
package fdlimit
2020

2121
import "syscall"
2222

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

42-
// getFdLimit retrieves the number of file descriptors allowed to be opened by this
42+
// Current retrieves the number of file descriptors allowed to be opened by this
4343
// process.
44-
func getFdLimit() (int, error) {
44+
func Current() (int, error) {
4545
var limit syscall.Rlimit
4646
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
4747
return 0, err
4848
}
4949
return int(limit.Cur), nil
5050
}
5151

52-
// getFdMaxLimit retrieves the maximum number of file descriptors this process is
52+
// Maximum retrieves the maximum number of file descriptors this process is
5353
// allowed to request for itself.
54-
func getFdMaxLimit() (int, error) {
54+
func Maximum() (int, error) {
5555
var limit syscall.Rlimit
5656
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
5757
return 0, err
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
1616

17-
package utils
17+
package fdlimit
1818

1919
import "errors"
2020

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

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

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

0 commit comments

Comments
 (0)