From 48ee44c46a0abbc42f40b2637fa75ceb6b090a40 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Mon, 14 Mar 2016 17:10:19 -0700 Subject: [PATCH 1/3] try to raise ulimit if its too low License: MIT Signed-off-by: Jeromy --- cmd/ipfs/daemon.go | 6 +++++ cmd/ipfs/ulimit_unix.go | 50 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 cmd/ipfs/ulimit_unix.go diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 1512c3ed2..0d5741cc1 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -152,10 +152,16 @@ func defaultMux(path string) corehttp.ServeOption { } } +var fileDescriptorCheck = func() error { return nil } + func daemonFunc(req cmds.Request, res cmds.Response) { // let the user know we're going. fmt.Printf("Initializing daemon...\n") + if err := fileDescriptorCheck(); err != nil { + log.Error("setting file descriptor limit: %s", err) + } + ctx := req.InvocContext() go func() { diff --git a/cmd/ipfs/ulimit_unix.go b/cmd/ipfs/ulimit_unix.go new file mode 100644 index 000000000..c2c24b787 --- /dev/null +++ b/cmd/ipfs/ulimit_unix.go @@ -0,0 +1,50 @@ +// +build linux darwin + +package main + +import ( + "fmt" + "os" + "strconv" + "syscall" +) + +var ipfsFileDescNum = uint64(1024) + +func init() { + if val := os.Getenv("IPFS_FD_MAX"); val != "" { + n, err := strconv.Atoi(val) + if err != nil { + log.Error("bad value for IPFS_FD_MAX: %s", err) + } else { + ipfsFileDescNum = uint64(n) + } + } + fileDescriptorCheck = checkAndSetUlimit +} + +func checkAndSetUlimit() error { + var rLimit syscall.Rlimit + err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) + if err != nil { + return fmt.Errorf("error getting rlimit: %s", err) + } + + if rLimit.Cur < ipfsFileDescNum { + if rLimit.Max > ipfsFileDescNum { + fmt.Printf("Adjusting current ulimit to %d.\n", rLimit.Max) + rLimit.Cur = rLimit.Max + } else { + fmt.Printf("Adjusting current and maximum ulimit to %d.\n", ipfsFileDescNum) + rLimit.Cur = ipfsFileDescNum + rLimit.Max = ipfsFileDescNum + } + } + + err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) + if err != nil { + return fmt.Errorf("error setting ulimit: %s", err) + } + + return nil +} From 0cb01acd718d8231634a56cc94fd3d345bf389cd Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 16 Mar 2016 11:58:12 -0700 Subject: [PATCH 2/3] don't use Max for setting Current License: MIT Signed-off-by: Jeromy --- cmd/ipfs/ulimit_unix.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/cmd/ipfs/ulimit_unix.go b/cmd/ipfs/ulimit_unix.go index c2c24b787..b4f2a6c7d 100644 --- a/cmd/ipfs/ulimit_unix.go +++ b/cmd/ipfs/ulimit_unix.go @@ -31,14 +31,11 @@ func checkAndSetUlimit() error { } if rLimit.Cur < ipfsFileDescNum { - if rLimit.Max > ipfsFileDescNum { - fmt.Printf("Adjusting current ulimit to %d.\n", rLimit.Max) - rLimit.Cur = rLimit.Max - } else { - fmt.Printf("Adjusting current and maximum ulimit to %d.\n", ipfsFileDescNum) - rLimit.Cur = ipfsFileDescNum + if rLimit.Max < ipfsFileDescNum { rLimit.Max = ipfsFileDescNum } + fmt.Printf("Adjusting current ulimit to %d.\n", ipfsFileDescNum) + rLimit.Cur = ipfsFileDescNum } err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) From e661832575a26610bfee55f370961b2ce11b489b Mon Sep 17 00:00:00 2001 From: Jeromy Date: Wed, 6 Apr 2016 18:13:12 -0700 Subject: [PATCH 3/3] hide fd adjusting code behind daemon feature flag License: MIT Signed-off-by: Jeromy --- cmd/ipfs/daemon.go | 9 +++++++-- cmd/ipfs/ulimit_unix.go | 2 +- test/sharness/t0061-daemon-opts.sh | 3 --- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/cmd/ipfs/daemon.go b/cmd/ipfs/daemon.go index 0d5741cc1..38dd9b8f2 100644 --- a/cmd/ipfs/daemon.go +++ b/cmd/ipfs/daemon.go @@ -40,6 +40,7 @@ const ( unrestrictedApiAccessKwd = "unrestricted-api" unencryptTransportKwd = "disable-transport-encryption" enableGCKwd = "enable-gc" + adjustFDLimitKwd = "manage-fdlimit" // apiAddrKwd = "address-api" // swarmAddrKwd = "address-swarm" ) @@ -132,6 +133,7 @@ future version, along with this notice. Please move to setting the HTTP Headers. cmds.BoolOption(unrestrictedApiAccessKwd, "Allow API access to unlisted hashes"), cmds.BoolOption(unencryptTransportKwd, "Disable transport encryption (for debugging protocols)"), cmds.BoolOption(enableGCKwd, "Enable automatic periodic repo garbage collection"), + cmds.BoolOption(adjustFDLimitKwd, "Check and raise file descriptor limits if needed"), // TODO: add way to override addresses. tricky part: updating the config if also --init. // cmds.StringOption(apiAddrKwd, "Address for the daemon rpc API (overrides config)"), @@ -158,8 +160,11 @@ func daemonFunc(req cmds.Request, res cmds.Response) { // let the user know we're going. fmt.Printf("Initializing daemon...\n") - if err := fileDescriptorCheck(); err != nil { - log.Error("setting file descriptor limit: %s", err) + managefd, _, _ := req.Option(adjustFDLimitKwd).Bool() + if managefd { + if err := fileDescriptorCheck(); err != nil { + log.Error("setting file descriptor limit: %s", err) + } } ctx := req.InvocContext() diff --git a/cmd/ipfs/ulimit_unix.go b/cmd/ipfs/ulimit_unix.go index b4f2a6c7d..1ad630f74 100644 --- a/cmd/ipfs/ulimit_unix.go +++ b/cmd/ipfs/ulimit_unix.go @@ -9,7 +9,7 @@ import ( "syscall" ) -var ipfsFileDescNum = uint64(1024) +var ipfsFileDescNum = uint64(2048) func init() { if val := os.Getenv("IPFS_FD_MAX"); val != "" { diff --git a/test/sharness/t0061-daemon-opts.sh b/test/sharness/t0061-daemon-opts.sh index 9f39d8197..c961a8651 100755 --- a/test/sharness/t0061-daemon-opts.sh +++ b/test/sharness/t0061-daemon-opts.sh @@ -13,9 +13,6 @@ test_init_ipfs test_launch_ipfs_daemon --unrestricted-api --disable-transport-encryption -test_expect_success "convert addresses from multiaddrs" ' -' - gwyaddr=$GWAY_ADDR apiaddr=$API_ADDR