storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/sys/rlimit-file_nix.go (about)

     1  //go:build linux || darwin || openbsd || netbsd || solaris
     2  // +build linux darwin openbsd netbsd solaris
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package sys
    21  
    22  import (
    23  	"runtime"
    24  	"syscall"
    25  )
    26  
    27  // GetMaxOpenFileLimit - returns maximum file descriptor number that can be opened by this process.
    28  func GetMaxOpenFileLimit() (curLimit, maxLimit uint64, err error) {
    29  	var rlimit syscall.Rlimit
    30  	if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err == nil {
    31  		curLimit = rlimit.Cur
    32  		maxLimit = rlimit.Max
    33  	}
    34  
    35  	return curLimit, maxLimit, err
    36  }
    37  
    38  // SetMaxOpenFileLimit - sets maximum file descriptor number that can be opened by this process.
    39  func SetMaxOpenFileLimit(curLimit, maxLimit uint64) error {
    40  	if runtime.GOOS == "darwin" && curLimit > 10240 {
    41  		// The max file limit is 10240, even though
    42  		// the max returned by Getrlimit is 1<<63-1.
    43  		// This is OPEN_MAX in sys/syslimits.h.
    44  		// refer https://github.com/golang/go/issues/30401
    45  		curLimit = 10240
    46  	}
    47  	rlimit := syscall.Rlimit{Cur: curLimit, Max: maxLimit}
    48  	return syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rlimit)
    49  }