Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increasing nofiles limit on UNIX systems
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/github.com/getlantern/flashlight/main/increase_nofiles.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// +build !windows | ||
|
||
package main | ||
|
||
import ( | ||
"syscall" | ||
) | ||
|
||
const ( | ||
// 1024 is the default hard limit on Ubuntu, so let's not get greedier than | ||
// that. On OS X, the default hard limit is unlimited. | ||
DesiredNoFilesLimit = 1024 | ||
) | ||
|
||
func init() { | ||
// We increase the nofiles limit on UNIX platforms to avoid running out of | ||
// file descriptors | ||
var rLimit syscall.Rlimit | ||
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) | ||
if err != nil { | ||
log.Errorf("Error getting nofiles limit %v", err) | ||
return | ||
} | ||
if rLimit.Cur > DesiredNoFilesLimit { | ||
log.Debugf("Current nofiles soft limit of %d is enough", rLimit.Cur) | ||
return | ||
} | ||
rLimit.Cur = DesiredNoFilesLimit | ||
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) | ||
if err != nil { | ||
log.Errorf("Unable to increase nofiles limit: %v", err) | ||
} | ||
log.Debugf("Changed nofiles soft limit to %d", rLimit.Cur) | ||
} |