From ca494d2abb8047463ec6f0a8674ed46935c6f91f Mon Sep 17 00:00:00 2001 From: Nicolas JUHEL Date: Wed, 17 Jun 2020 16:04:03 +0200 Subject: [PATCH] Fix error if one of Max or Cur limit is upper than new value --- njs-ioutils/fileDescriptor.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/njs-ioutils/fileDescriptor.go b/njs-ioutils/fileDescriptor.go index 4f3c5d63..f201abe7 100644 --- a/njs-ioutils/fileDescriptor.go +++ b/njs-ioutils/fileDescriptor.go @@ -49,12 +49,24 @@ func SystemFileDescriptor(newValue int) (current int, max int, err error) { return int(rLimit.Cur), int(rLimit.Max), nil } - rLimit.Max = uint64(newValue) - rLimit.Cur = uint64(newValue) + var chg = false - if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { - return + if newValue > int(rLimit.Max) { + chg = true + rLimit.Max = uint64(newValue) + } + if newValue > int(rLimit.Cur) { + chg = true + rLimit.Cur = uint64(newValue) + } + + if chg { + if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); err != nil { + return + } + + return SystemFileDescriptor(0) } - return SystemFileDescriptor(0) + return int(rLimit.Cur), int(rLimit.Max), nil }