github.com/MetalBlockchain/metalgo@v1.11.9/utils/ulimit/ulimit_darwin.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 darwin 5 // +build darwin 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 = 10 * 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 var rLimit syscall.Rlimit 28 err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) 29 if err != nil { 30 return fmt.Errorf("error getting rlimit: %w", err) 31 } 32 33 // Darwin max number of FDs to allocatable for darwin systems. 34 // The max file limit is 10240, even though the max returned by 35 // Getrlimit is 1<<63-1. This is OPEN_MAX in sys/syslimits.h. 36 // See https://github.com/golang/go/issues/30401 37 if max > DefaultFDLimit { 38 return fmt.Errorf("error fd-limit: (%d) greater than max: (%d)", max, DefaultFDLimit) 39 } 40 41 rLimit.Cur = max 42 43 // set new limit 44 if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { 45 return fmt.Errorf("error setting fd-limit: %w", err) 46 } 47 48 // verify limit 49 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { 50 return fmt.Errorf("error getting rlimit: %w", err) 51 } 52 53 if rLimit.Cur < DefaultFDLimit { 54 log.Warn("fd-limit is less than recommended and could result in reduced performance", 55 zap.Uint64("currentLimit", rLimit.Cur), 56 zap.Uint64("recommendedLimit", DefaultFDLimit), 57 ) 58 } 59 60 return nil 61 }