github.com/koko1123/flow-go-1@v0.29.6/network/p2p/p2pbuilder/libp2pscaler.go (about) 1 package p2pbuilder 2 3 import ( 4 "fmt" 5 "math" 6 7 "github.com/pbnjay/memory" 8 "golang.org/x/sys/unix" 9 ) 10 11 func getNumFDs() (int, error) { 12 var l unix.Rlimit 13 if err := unix.Getrlimit(unix.RLIMIT_NOFILE, &l); err != nil { 14 return 0, err 15 } 16 return int(l.Cur), nil 17 } 18 19 func allowedMemory(scaleFactor float64) (int64, error) { 20 if scaleFactor <= 0 || scaleFactor > 1 { 21 return 0, fmt.Errorf("memory scale factor must be greater than 0 and less than or equal to 1: %f", scaleFactor) 22 } 23 return int64(math.Floor(float64(memory.TotalMemory()) * scaleFactor)), nil 24 } 25 26 func allowedFileDescriptors(scaleFactor float64) (int, error) { 27 if scaleFactor <= 0 || scaleFactor > 1 { 28 return 0, fmt.Errorf("fd scale factor must be greater than 0 and less than or equal to 1: %f", scaleFactor) 29 } 30 fd, err := getNumFDs() 31 if err != nil { 32 return 0, fmt.Errorf("failed to get file descriptor limit: %w", err) 33 } 34 return int(math.Floor(float64(fd) * scaleFactor)), nil 35 }