github.com/MetalBlockchain/metalgo@v1.11.9/utils/ulimit/ulimit_bsd.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 //go:build freebsd 5 // +build freebsd 6 7 package ulimit 8 9 import ( 10 "fmt" 11 "syscall" 12 13 "go.uber.org/zap" 14 15 "github.com/MetalBlockchain/metalgo/utils/logging" 16 ) 17 18 const DefaultFDLimit = 32 * 1024 19 20 // Set attempts to bump the Rlimit which has a soft (Cur) and a hard (Max) value. 21 // The soft limit is what is used by the kernel to report EMFILE errors. The hard 22 // limit is a secondary limit which the process can be bumped to without additional 23 // privileges. Bumping the Max limit further would require superuser privileges. 24 // If the value is below the recommendation warn on start. 25 // see: http://0pointer.net/blog/file-descriptor-limits.html 26 func Set(max uint64, log logging.Logger) error { 27 // Note: BSD Rlimit is type int64 28 // ref: https://cs.opensource.google/go/x/sys/+/b874c991:unix/ztypes_freebsd_amd64.go 29 bsdMax := int64(max) 30 var rLimit syscall.Rlimit 31 err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) 32 if err != nil { 33 return fmt.Errorf("error getting rlimit: %w", err) 34 } 35 36 if bsdMax > rLimit.Max { 37 return fmt.Errorf("error fd-limit: (%d) greater than max: (%d)", limit, rLimit.Max) 38 } 39 40 rLimit.Cur = bsdMax 41 42 // set new limit 43 if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { 44 return fmt.Errorf("error setting fd-limit: %w", err) 45 } 46 47 // verify limit 48 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { 49 return fmt.Errorf("error getting rlimit: %w", err) 50 } 51 52 if rLimit.Cur < DefaultFDLimit { 53 log.Warn("fd-limit is less than recommended and could result in reduced performance", 54 zap.Uint64("currentLimit", rLimit.Cur), 55 zap.Uint64("recommendedLimit", DefaultFDLimit), 56 ) 57 } 58 59 return nil 60 }