diff options
author | Audrius Butkevicius <audrius.butkevicius@gmail.com> | 2015-08-15 14:31:20 +0100 |
---|---|---|
committer | Audrius Butkevicius <audrius.butkevicius@gmail.com> | 2015-08-15 14:31:20 +0100 |
commit | f3f586773be4d40b6ccd7c81656b0edf4e9bc0ac (patch) | |
tree | e17ea800ae125aa69fe8297e3b67c4b64c06efcd | |
parent | 5e5eb9bf8e754614e771b90c5e77712994d8390b (diff) | |
parent | 1c9513e7701581263af1e55482fc04a1157b7fcc (diff) |
Merge pull request #2160 from calmh/rlimit
Increase open file (fd) limit if possible
-rw-r--r-- | cmd/syncthing/main.go | 5 | ||||
-rw-r--r-- | lib/osutil/rlimit_unix.go | 47 | ||||
-rw-r--r-- | lib/osutil/rlimit_windows.go | 17 |
3 files changed, 69 insertions, 0 deletions
diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 26c3292735..b5c45794b0 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -454,6 +454,11 @@ func syncthingMain() { runtime.GOMAXPROCS(runtime.NumCPU()) } + // Attempt to increase the limit on number of open files to the maximum + // allowed, in case we have many peers. We don't really care enough to + // report the error if there is one. + osutil.MaximizeOpenFileLimit() + // Ensure that that we have a certificate and key. cert, err := tls.LoadX509KeyPair(locations[locCertFile], locations[locKeyFile]) if err != nil { diff --git a/lib/osutil/rlimit_unix.go b/lib/osutil/rlimit_unix.go new file mode 100644 index 0000000000..85147a8c73 --- /dev/null +++ b/lib/osutil/rlimit_unix.go @@ -0,0 +1,47 @@ +// Copyright (C) 2015 The Syncthing Authors. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at http://mozilla.org/MPL/2.0/. + +// +build !windows + +package osutil + +import "syscall" + +// MaximizeOpenFileLimit tries to set the resoure limit RLIMIT_NOFILE (number +// of open file descriptors) to the max (hard limit), if the current (soft +// limit) is below the max. Returns the new (though possibly unchanged) limit, +// or an error if it was could not be changed. +func MaximizeOpenFileLimit() (int, error) { + // Get the current limit on number of open files. + var lim syscall.Rlimit + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { + return 0, err + } + + // If we're already at max, there's no need to try to raise the limit. + if lim.Cur >= lim.Max { + return int(lim.Cur), nil + } + + // Try to increase the limit to the max. + oldLimit := lim.Cur + lim.Cur = lim.Max + if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { + return int(oldLimit), err + } + + // If the set succeeded, perform a new get to see what happened. We might + // have gotten a value lower than the one in lim.Max, if lim.Max was + // something that indiciated "unlimited" (i.e. intmax). + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { + // We don't really know the correct value here since Getrlimit + // mysteriously failed after working once... Shouldn't ever happen, I + // think. + return 0, err + } + + return int(lim.Cur), nil +} diff --git a/lib/osutil/rlimit_windows.go b/lib/osutil/rlimit_windows.go new file mode 100644 index 0000000000..df1c78c5d1 --- /dev/null +++ b/lib/osutil/rlimit_windows.go @@ -0,0 +1,17 @@ +// Copyright (C) 2015 The Syncthing Authors. +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at http://mozilla.org/MPL/2.0/. + +// +build windows + +package osutil + +import ( + "errors" +) + +func MaximizeOpenFileLimit() (int, error) { + return 0, errors.New("not relevant on Windows") +} |