diff --git a/common/oslimits.go b/common/oslimits.go new file mode 100644 index 0000000000..d61825107d --- /dev/null +++ b/common/oslimits.go @@ -0,0 +1,16 @@ +// +build !windows + +package common + +import ( + "syscall" +) + +func MaxOpenFileLimit() int { + var nofile syscall.Rlimit + err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &nofile) + if err != nil { + return 1024 + } + return int(nofile.Max) +} diff --git a/common/oslimits_windows.go b/common/oslimits_windows.go new file mode 100644 index 0000000000..d13c078eee --- /dev/null +++ b/common/oslimits_windows.go @@ -0,0 +1,9 @@ +package common + +import ( +) + +func MaxOpenFileLimit() int { + // From https://msdn.microsoft.com/en-us/library/kdfaxaay.aspx + return 512 +} diff --git a/common/path.go b/common/path.go index 6e32596560..43527ef8e7 100644 --- a/common/path.go +++ b/common/path.go @@ -7,7 +7,6 @@ import ( "path/filepath" "runtime" "strings" - "github.com/kardianos/osext" ) diff --git a/eth/backend.go b/eth/backend.go index 37fe66abfa..2cc95e33d1 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -236,9 +236,9 @@ func New(config *Config) (*Ethereum, error) { logger.NewJSONsystem(config.DataDir, config.LogJSON) } - // Let the database take 3/4 of the max open files (TODO figure out a way to get the actual limit of the open files) const dbCount = 3 - ethdb.OpenFileLimit = 128 / (dbCount + 1) + + ethdb.OpenFileLimit = common.MaxOpenFileLimit() / (dbCount + 1) newdb := config.NewDB if newdb == nil { diff --git a/ethdb/database.go b/ethdb/database.go index 019645cedd..d370b9f338 100644 --- a/ethdb/database.go +++ b/ethdb/database.go @@ -1,6 +1,7 @@ package ethdb import ( + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/compression/rle" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger/glog" @@ -10,7 +11,8 @@ import ( "github.com/syndtr/goleveldb/leveldb/opt" ) -var OpenFileLimit = 64 + +var OpenFileLimit = common.MaxOpenFileLimit() type LDBDatabase struct { // filename for reporting