Skip to content

Instantly share code, notes, and snippets.

@whyrusleeping
Created March 23, 2017 22:43
Show Gist options
  • Save whyrusleeping/8c3bae34723f209665480f39d2163848 to your computer and use it in GitHub Desktop.
Save whyrusleeping/8c3bae34723f209665480f39d2163848 to your computer and use it in GitHub Desktop.
package ulimit
var supportsFDManagement = false
// getFDLimit returns the soft and hard limits of file descriptor counts
var getFDLimit func() (int64, int64, error)
var setFDLimit func(int64, int64) error
// maxFds is the maximum number of file descriptors that go-ipfs
// can use. The default value is 1024. This can be overwritten by the
// IPFS_FD_MAX env variable
var maxFds = uint64(1024)
// SetMaxFds sets the maxFds value from IPFS_FD_MAX
// env variable if it's present on the system
func SetMaxFds() {
// check if the IPFS_FD_MAX is set up and if it does
// not have a valid fds number notify the user
if val := os.Getenv("IPFS_FD_MAX"); val != "" {
fds, err := strconv.ParseUint(val, 10, 64)
if err != nil {
log.Errorf("bad value for IPFS_FD_MAX: %s", err)
return
}
maxFds = fds
}
}
func ManageFDLimit() {
if !supportsFDManagement {
return // no support on this platform, maybe log a message?
}
SetMaxFds()
// Same logic you currently have in ManageFD, just using the generalized functions defined above
}
// +build freebsd
package ulimit
func init() {
supportsFDManagement = true
getFDLimit = freebsdGetFDLimit
setFDLimit = freebsdSetFDLimit
}
func freebsdGetFDLimit() (int64, int64, error) {
...
}
func freebsdSetFDLimit(int64, int64) error {
...
}
// +build darwin linux netbsd openbsd
package ulimit
func init() {
supportsFDManagement = true
getFDLimit = getFDLimit
setFDLimit = setFDLimit
}
func getFDLimit() (int64, int64, error) {
...
}
func setFDLimit(int64, int64) error {
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment