Skip to content

Commit

Permalink
added ulimit setting
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Sep 28, 2020
1 parent 0a08b04 commit 60760a3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions main/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/ava-labs/avalanchego/utils/hashing"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/password"
"github.com/ava-labs/avalanchego/utils/ulimit"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/utils/wrappers"
)
Expand Down Expand Up @@ -190,6 +191,8 @@ func init() {
consensusGossipFrequency := fs.Int64("consensus-gossip-frequency", int64(10*time.Second), "Frequency of gossiping accepted frontiers.")
consensusShutdownTimeout := fs.Int64("consensus-shutdown-timeout", int64(1*time.Second), "Timeout before killing an unresponsive chain.")

fdLimit := fs.Uint64("fd-limit", ulimit.DefaultFDLimit, "Attempts to raise the process file descriptor limit to at least this value.")

ferr := fs.Parse(os.Args[1:])

if *version { // If --version used, print version and exit
Expand Down Expand Up @@ -436,6 +439,10 @@ func init() {
Config.ConsensusGossipFrequency = time.Duration(*consensusGossipFrequency)
Config.ConsensusShutdownTimeout = time.Duration(*consensusShutdownTimeout)

if err := ulimit.Set(*fdLimit); err != nil {
errs.Add(fmt.Errorf("failed to set fd limit correctly due to: %s", err))
}

if networkID != constants.MainnetID && networkID != constants.FujiID {
Config.TxFee = *txFee
Config.CreationTxFee = *creationTxFee
Expand Down
11 changes: 11 additions & 0 deletions utils/ulimit/ulimit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file was taken from:
// https://github.com/OpenBazaar/openbazaar-go/blob/master/core/ulimit_non_unix.go

// +build darwin linux netbsd openbsd

package ulimit

const (
// DefaultFDLimit is the default recommended number of FDs to allocate.
DefaultFDLimit uint64 = 1 << 15 // 32k
)
12 changes: 12 additions & 0 deletions utils/ulimit/ulimit_non_unix.go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// This file was taken from:
// https://github.com/OpenBazaar/openbazaar-go/blob/master/core/ulimit_non_unix.go

// +build !darwin
// +build !linux
// +build !netbsd
// +build !openbsd

package ulimit

// Set is a no-op on non-unix systems
func Set(uint64) error { return nil }
51 changes: 51 additions & 0 deletions utils/ulimit/ulimit_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file was taken from:
// https://github.com/OpenBazaar/openbazaar-go/blob/master/core/ulimit_non_unix.go

// +build darwin linux netbsd openbsd

package ulimit

import (
"fmt"
"runtime"
"syscall"
)

// Set the file descriptor limit
func Set(limit uint64) error {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
return fmt.Errorf("error getting rlimit: %s", err)
}

oldMax := rLimit.Max
if rLimit.Cur < limit {
if rLimit.Max < limit {
rLimit.Max = limit
}
rLimit.Cur = limit
}

// If we're on darwin, work around the fact that Getrlimit reports the wrong
// value. See https://github.com/golang/go/issues/30401
if runtime.GOOS == "darwin" && rLimit.Cur > 10240 {
// The max file limit is 10240, even though the max returned by
// Getrlimit is 1<<63-1. This is OPEN_MAX in sys/syslimits.h.
rLimit.Max = 10240
rLimit.Cur = 10240
}

// Try updating the limit. If it fails, try using the previous maximum
// instead of our new maximum. Not all users have permissions to increase
// the maximum.
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
rLimit.Max = oldMax
rLimit.Cur = oldMax
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil {
return fmt.Errorf("error setting ulimit: %s", err)
}
}

return nil
}

0 comments on commit 60760a3

Please sign in to comment.