github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/common/fdlimit/fdlimit_unix.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 linux darwin netbsd openbsd solaris 13 14 package fdlimit 15 16 import "syscall" 17 18 // Raise tries to maximize the file descriptor allowance of this process 19 // to the maximum hard-limit allowed by the OS. 20 func Raise(max uint64) error { 21 // Get the current limit 22 var limit syscall.Rlimit 23 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 24 return err 25 } 26 // Try to update the limit to the max allowance 27 limit.Cur = limit.Max 28 if limit.Cur > max { 29 limit.Cur = max 30 } 31 if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 32 return err 33 } 34 return nil 35 } 36 37 // Current retrieves the number of file descriptors allowed to be opened by this 38 // process. 39 func Current() (int, error) { 40 var limit syscall.Rlimit 41 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 42 return 0, err 43 } 44 return int(limit.Cur), nil 45 } 46 47 // Maximum retrieves the maximum number of file descriptors this process is 48 // allowed to request for itself. 49 func Maximum() (int, error) { 50 var limit syscall.Rlimit 51 if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil { 52 return 0, err 53 } 54 return int(limit.Max), nil 55 }