github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/fdlimit/fdlimit_darwin.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package fdlimit 19 20 import "syscall" 21 22 // hardlimit is the number of file descriptors allowed at max by the kernel. 23 const hardlimit = 10240 24 25 // Raise tries to maximize the file descriptor allowance of this process 26 // to the maximum hard-limit allowed by the OS. 27 // Returns the size it was set to (may differ from the desired 'max') 28 func Raise(max uint64) (uint64, error) { 29 // Get the current limit 30 var limit syscall.Rlimit 31 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 32 return 0, err 33 } 34 // Try to update the limit to the max allowance 35 limit.Cur = limit.Max 36 if limit.Cur > max { 37 limit.Cur = max 38 } 39 if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 40 return 0, err 41 } 42 // MacOS can silently apply further caps, so retrieve the actually set limit 43 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 44 return 0, err 45 } 46 return limit.Cur, nil 47 } 48 49 // Current retrieves the number of file descriptors allowed to be opened by this 50 // process. 51 func Current() (int, error) { 52 var limit syscall.Rlimit 53 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 54 return 0, err 55 } 56 return int(limit.Cur), nil 57 } 58 59 // Maximum retrieves the maximum number of file descriptors this process is 60 // allowed to request for itself. 61 func Maximum() (int, error) { 62 // Retrieve the maximum allowed by dynamic OS limits 63 var limit syscall.Rlimit 64 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 65 return 0, err 66 } 67 // Cap it to OPEN_MAX (10240) because macos is a special snowflake 68 if limit.Max > hardlimit { 69 limit.Max = hardlimit 70 } 71 return int(limit.Max), nil 72 }