Skip to content

Commit

Permalink
Ulimit check is incorrect (#327)
Browse files Browse the repository at this point in the history
* close test connection to google DNS

* ulimit fix: # threads == # open sockets

* rename

* adding margin fo ulimit
  • Loading branch information
bayerhonza authored Apr 2, 2023
1 parent c8e1d8e commit 898b3c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
10 changes: 5 additions & 5 deletions pkg/zdns/ulimit_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import (
log "github.com/sirupsen/logrus"
)

func ulimit_check(max_open_files uint64) {
func ulimitCheck(maxOpenFiles uint64) {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Fatal("Failed to fetch ulimit ", err)
}

if max_open_files > rLimit.Cur {
log.Warn("Current nofile limit (", rLimit.Cur, ") lower than maximum connection count (", max_open_files, "), try to update.")
if maxOpenFiles > rLimit.Cur {
log.Warn("Current nofile limit (", rLimit.Cur, ") lower than maximum connection count (", maxOpenFiles, "), trying to update.")

rLimit.Max = max_open_files
rLimit.Cur = max_open_files
rLimit.Max = maxOpenFiles
rLimit.Cur = maxOpenFiles
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Fatal("Error setting nofile limit to ", rLimit.Cur, ": ", err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/zdns/ulimit_check_unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
*/
package zdns

func ulimit_check(max_open_files uint64) {
func ulimitCheck(maxOpenFiles uint64) {
// fallback for ulimit check on unsupported platform
}
12 changes: 9 additions & 3 deletions pkg/zdns/zdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ func Run(gc GlobalConf, flags *pflag.FlagSet,
log.Fatal("Unable to find default IP address: ", err)
} else {
gc.LocalAddrs = append(gc.LocalAddrs, conn.LocalAddr().(*net.UDPAddr).IP)
err := conn.Close()
if err != nil {
log.Warn("Unable to close test connection to Google Public DNS: ", err)
}
}
}
if *nanoSeconds {
Expand All @@ -216,11 +220,13 @@ func Run(gc GlobalConf, flags *pflag.FlagSet,

if gc.GoMaxProcs != 0 {
runtime.GOMAXPROCS(gc.GoMaxProcs)
ulimit_check(uint64(gc.GoMaxProcs * gc.Threads))
} else {
ulimit_check(uint64(runtime.NumCPU() * gc.Threads))
}

// the margin for limit of open files covers different build platforms (linux/darwin), metadata files, or
// input and output files etc.
// check ulimit if value is high enough and if not, try to fix it
ulimitCheck(uint64(gc.Threads + 100))

if gc.UDPOnly && gc.TCPOnly {
log.Fatal("TCP Only and UDP Only are conflicting")
}
Expand Down

0 comments on commit 898b3c9

Please sign in to comment.