github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/common/fdlimit/fdlimit_freebsd.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 // +build freebsd 13 14 package fdlimit 15 16 import "syscall" 17 18 // This file is largely identical to fdlimit_unix.go, 19 // but Rlimit fields have type int64 on FreeBSD so it needs 20 // an extra conversion. 21 22 // Raise tries to maximize the file descriptor allowance of this process 23 // to the maximum hard-limit allowed by the OS. 24 func Raise(max uint64) error { 25 // Get the current limit 26 var limit syscall.Rlimit 27 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 28 return err 29 } 30 // Try to update the limit to the max allowance 31 limit.Cur = limit.Max 32 if limit.Cur > int64(max) { 33 limit.Cur = int64(max) 34 } 35 if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 36 return err 37 } 38 return nil 39 } 40 41 // Current retrieves the number of file descriptors allowed to be opened by this 42 // process. 43 func Current() (int, error) { 44 var limit syscall.Rlimit 45 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 46 return 0, err 47 } 48 return int(limit.Cur), nil 49 } 50 51 // Maximum retrieves the maximum number of file descriptors this process is 52 // allowed to request for itself. 53 func Maximum() (int, error) { 54 var limit syscall.Rlimit 55 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 56 return 0, err 57 } 58 return int(limit.Max), nil 59 }