提交 182507d0 作者: rob-deutsch

fixed test of raised fd limits

Raising FD limits was erroring when the OS's max was at the maximum signed integer value. Switched the code to using uint64 instead of int64.

fixed #5495

License: MIT
Signed-off-by: 's avatarRob Deutsch <rdeutschob@gmail.com>
上级 ac53d3aa
......@@ -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
......@@ -56,9 +56,7 @@ func ManageFdLimit() error {
return err
}
max := int64(maxFds)
if max <= soft {
if maxFds <= soft {
return nil
}
......@@ -67,25 +65,25 @@ func ManageFdLimit() error {
// 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)
}
// the process does not have permission so we should only
// set the soft value
if max > hard {
if maxFds > hard {
return errors.New(
"cannot set rlimit, IPFS_FD_MAX is larger than the hard limit",
)
}
if err = setLimit(max, hard); err != nil {
if err = setLimit(maxFds, hard); err != nil {
return fmt.Errorf("error setting ulimit wihout hard limit: %s", err)
}
}
fmt.Printf("Successfully raised file descriptor limit to %d.\n", max)
fmt.Printf("Successfully raised file descriptor limit to %d.\n", maxFds)
return nil
}
......@@ -3,6 +3,9 @@
package util
import (
"errors"
"math"
unix "gx/ipfs/QmVGjyM9i2msKvLXwh9VosCTgP4mL91kC7hDmqnwTTx6Hu/sys/unix"
)
......@@ -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)
}
......@@ -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)
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论