...

Source file src/os/rlimit.go

Documentation: os

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     6  
     7  package os
     8  
     9  import "syscall"
    10  
    11  // Some systems set an artificially low soft limit on open file count, for compatibility
    12  // with code that uses select and its hard-coded maximum file descriptor
    13  // (limited by the size of fd_set).
    14  //
    15  // Go does not use select, so it should not be subject to these limits.
    16  // On some systems the limit is 256, which is very easy to run into,
    17  // even in simple programs like gofmt when they parallelize walking
    18  // a file tree.
    19  //
    20  // After a long discussion on go.dev/issue/46279, we decided the
    21  // best approach was for Go to raise the limit unconditionally for itself,
    22  // and then leave old software to set the limit back as needed.
    23  // Code that really wants Go to leave the limit alone can set the hard limit,
    24  // which Go of course has no choice but to respect.
    25  func init() {
    26  	var lim syscall.Rlimit
    27  	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err == nil && lim.Cur != lim.Max {
    28  		lim.Cur = lim.Max
    29  		adjustFileLimit(&lim)
    30  		syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim)
    31  	}
    32  }
    33  

View as plain text