Skip to content

Commit

Permalink
Merge pull request #5496 from rob-deutsch/fix/5495
Browse files Browse the repository at this point in the history
fixed tests of raised fd limits
  • Loading branch information
Stebalien committed Sep 25, 2018
2 parents 30414a4 + aada0cc commit 1e0d53f
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
6 changes: 5 additions & 1 deletion cmd/ipfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,12 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment

managefd, _ := req.Options[adjustFDLimitKwd].(bool)
if managefd {
if err := utilmain.ManageFdLimit(); err != nil {
if changedFds, newFdsLimit, err := utilmain.ManageFdLimit(); err != nil {
log.Errorf("setting file descriptor limit: %s", err)
} else {
if changedFds {
fmt.Printf("Successfully raised file descriptor limit to %d.\n", newFdsLimit)
}
}
}

Expand Down
32 changes: 14 additions & 18 deletions cmd/ipfs/util/ulimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var (
supportsFDManagement = false

// getlimit returns the soft and hard limits of file descriptors counts
getLimit func() (int64, int64, error)
getLimit func() (uint64, uint64, error)
// set limit sets the soft and hard limits of file descriptors counts
setLimit func(int64, int64) error
setLimit func(uint64, uint64) error
)

// maxFds is the maximum number of file descriptors that go-ipfs
Expand All @@ -45,47 +45,43 @@ func setMaxFds() {

// ManageFdLimit raise the current max file descriptor count
// of the process based on the IPFS_FD_MAX value
func ManageFdLimit() error {
func ManageFdLimit() (changed bool, newLimit uint64, err error) {
if !supportsFDManagement {
return nil
return false, 0, nil
}

setMaxFds()
soft, hard, err := getLimit()
if err != nil {
return err
return false, 0, err
}

max := int64(maxFds)

if max <= soft {
return nil
if maxFds <= soft {
return false, 0, nil
}

// the soft limit is the value that the kernel enforces for the
// corresponding resource
// the hard limit acts as a ceiling for the soft limit
// an unprivileged process may only set it's soft limit to a
// alue in the range from 0 up to the hard limit
if err = setLimit(max, max); err != nil {
if err = setLimit(maxFds, maxFds); err != nil {
if err != syscall.EPERM {
return fmt.Errorf("error setting: ulimit: %s", err)
return false, 0, fmt.Errorf("error setting: ulimit: %s", err)
}

// the process does not have permission so we should only
// set the soft value
if max > hard {
return errors.New(
if maxFds > hard {
return false, 0, errors.New(
"cannot set rlimit, IPFS_FD_MAX is larger than the hard limit",
)
}

if err = setLimit(max, hard); err != nil {
return fmt.Errorf("error setting ulimit wihout hard limit: %s", err)
if err = setLimit(maxFds, hard); err != nil {
return false, 0, fmt.Errorf("error setting ulimit wihout hard limit: %s", err)
}
}

fmt.Printf("Successfully raised file descriptor limit to %d.\n", max)

return nil
return true, maxFds, nil
}
19 changes: 14 additions & 5 deletions cmd/ipfs/util/ulimit_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
package util

import (
"errors"
"math"

unix "gx/ipfs/QmVGjyM9i2msKvLXwh9VosCTgP4mL91kC7hDmqnwTTx6Hu/sys/unix"
)

Expand All @@ -12,16 +15,22 @@ func init() {
setLimit = freebsdSetLimit
}

func freebsdGetLimit() (int64, int64, error) {
func freebsdGetLimit() (uint64, uint64, error) {
rlimit := unix.Rlimit{}
err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit)
return rlimit.Cur, rlimit.Max, err
if (rlimit.Cur < 0) || (rlimit.Max < 0) {
return 0, 0, errors.New("invalid rlimits")
}
return uint64(rlimit.Cur), uint64(rlimit.Max), err
}

func freebsdSetLimit(soft int64, max int64) error {
func freebsdSetLimit(soft uint64, max uint64) error {
if (soft > math.MaxInt64) || (max > math.MaxInt64) {
return errors.New("invalid rlimits")
}
rlimit := unix.Rlimit{
Cur: soft,
Max: max,
Cur: int64(soft),
Max: int64(max),
}
return unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
}
6 changes: 3 additions & 3 deletions cmd/ipfs/util/ulimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func TestManageFdLimit(t *testing.T) {
t.Log("Testing file descriptor count")
if err := ManageFdLimit(); err != nil {
if _, _, err := ManageFdLimit(); err != nil {
t.Errorf("Cannot manage file descriptors")
}

Expand Down Expand Up @@ -41,7 +41,7 @@ func TestManageInvalidNFds(t *testing.T) {
// call to check and set the maximum file descriptor from the env
setMaxFds()

if err = ManageFdLimit(); err == nil {
if _, _, err := ManageFdLimit(); err == nil {
t.Errorf("ManageFdLimit should return an error")
} else if err != nil {
flag := strings.Contains(err.Error(),
Expand Down Expand Up @@ -79,7 +79,7 @@ func TestManageFdLimitWithEnvSet(t *testing.T) {
t.Errorf("The maxfds is not set from IPFS_FD_MAX")
}

if err = ManageFdLimit(); err != nil {
if _, _, err = ManageFdLimit(); err != nil {
t.Errorf("Cannot manage file descriptor count")
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/ipfs/util/ulimit_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ func init() {
setLimit = unixSetLimit
}

func unixGetLimit() (int64, int64, error) {
func unixGetLimit() (uint64, uint64, error) {
rlimit := unix.Rlimit{}
err := unix.Getrlimit(unix.RLIMIT_NOFILE, &rlimit)
return int64(rlimit.Cur), int64(rlimit.Max), err
return rlimit.Cur, rlimit.Max, err
}

func unixSetLimit(soft int64, max int64) error {
func unixSetLimit(soft uint64, max uint64) error {
rlimit := unix.Rlimit{
Cur: uint64(soft),
Max: uint64(max),
Cur: soft,
Max: max,
}
return unix.Setrlimit(unix.RLIMIT_NOFILE, &rlimit)
}
2 changes: 2 additions & 0 deletions mk/util.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ SUPPORTED_PLATFORMS += linux-amd64
SUPPORTED_PLATFORMS += darwin-386
SUPPORTED_PLATFORMS += darwin-amd64

SUPPORTED_PLATFORMS += freebsd-amd64

space:=
space+=
comma:=,
Expand Down

0 comments on commit 1e0d53f

Please sign in to comment.