Skip to content

Commit

Permalink
go/oasis-node: Warn if the rlimit appears to be low
Browse files Browse the repository at this point in the history
1024 file descriptors is low, and probably insufficient for an actual
node.
  • Loading branch information
Yawning committed Jan 21, 2020
1 parent 7c17fc0 commit f783821
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion go/oasis-node/cmd/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"syscall"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand All @@ -27,8 +28,13 @@ const (
// keys.
CfgDebugAllowTestKeys = "debug.allow_test_keys"

// CfgDebugRlimit is the command flag to set RLIMIT_NOFILE on launch.
CfgDebugRlimit = "debug.rlimit"

cfgConfigFile = "config"
CfgDataDir = "datadir"

badDefaultRlimit = 1024
)

var (
Expand All @@ -37,6 +43,7 @@ var (
rootLog = logging.GetLogger("oasis-node")

debugAllowTestKeysFlag = flag.NewFlagSet("", flag.ContinueOnError)
debugRlimitFlag = flag.NewFlagSet("", flag.ContinueOnError)

// RootFlags has the flags that are common across all commands.
RootFlags = flag.NewFlagSet("", flag.ContinueOnError)
Expand Down Expand Up @@ -90,6 +97,7 @@ func Init() error {
initDataDir,
initLogging,
initPublicKeyBlacklist,
initRlimit,
}

for _, fn := range initFns {
Expand All @@ -111,16 +119,21 @@ func Logger() *logging.Logger {
func init() {
initLoggingFlags()

debugAllowTestKeysFlag.Bool(CfgDebugAllowTestKeys, false, "Allow test keys (UNSAFE)")
debugAllowTestKeysFlag.Bool(CfgDebugAllowTestKeys, false, "allow test keys (UNSAFE)")
_ = debugAllowTestKeysFlag.MarkHidden(CfgDebugAllowTestKeys)
_ = viper.BindPFlags(debugAllowTestKeysFlag)

debugRlimitFlag.Uint64(CfgDebugRlimit, 0, "set RLIMIT_NOFILE")
_ = debugRlimitFlag.MarkHidden(CfgDebugRlimit)
_ = viper.BindPFlags(debugRlimitFlag)

RootFlags.StringVar(&cfgFile, cfgConfigFile, "", "config file")
RootFlags.String(CfgDataDir, "", "data directory")
_ = viper.BindPFlags(RootFlags)

RootFlags.AddFlagSet(loggingFlags)
RootFlags.AddFlagSet(debugAllowTestKeysFlag)
RootFlags.AddFlagSet(debugRlimitFlag)
RootFlags.AddFlagSet(flags.DebugDontBlameOasisFlag)
}

Expand Down Expand Up @@ -184,6 +197,33 @@ func initPublicKeyBlacklist() error {
return nil
}

func initRlimit() error {
var rlim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlim); err != nil {
// Log, but don't return the error, this is only used for testing
// and to warn the user if it's set too low.
rootLog.Warn("failed to query RLIMIT_NOFILE",
"err", err,
)
return nil
}

if rlim.Cur <= badDefaultRlimit {
rootLog.Warn("the node user has a very low RLIMIT_NOFILE, consider increasing",
"cur", rlim.Cur,
"max", rlim.Max,
)
}

desiredLimit := viper.GetUint64(CfgDebugRlimit)
if !flags.DebugDontBlameOasis() || desiredLimit == 0 {
return nil
}

rlim.Cur = desiredLimit
return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlim)
}

// GetOutputWriter will create a file if the config string is set,
// and otherwise return os.Stdout.
func GetOutputWriter(cmd *cobra.Command, cfg string) (io.WriteCloser, bool, error) {
Expand Down

0 comments on commit f783821

Please sign in to comment.