limits_unix.go raw

   1  // +build !windows,!plan9
   2  
   3  package limits
   4  
   5  import (
   6  	"fmt"
   7  	"syscall"
   8  )
   9  
  10  const (
  11  	fileLimitWant = 32768
  12  	fileLimitMin  = 1024
  13  )
  14  
  15  // SetLimits raises some process limits to values which allow pod and associated utilities to run.
  16  func SetLimits() (e error) {
  17  	var rLimit syscall.Rlimit
  18  	if e = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit); E.Chk(e) {
  19  		return
  20  	}
  21  	if rLimit.Cur > fileLimitWant {
  22  		return
  23  	}
  24  	if rLimit.Max < fileLimitMin {
  25  		e = fmt.Errorf(
  26  			"need at least %v file descriptors",
  27  			fileLimitMin,
  28  		)
  29  		return
  30  	}
  31  	if rLimit.Max < fileLimitWant {
  32  		rLimit.Cur = rLimit.Max
  33  	} else {
  34  		rLimit.Cur = fileLimitWant
  35  	}
  36  	e = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
  37  	if e != nil {
  38  		E.Ln(e)
  39  		// try min value
  40  		rLimit.Cur = fileLimitMin
  41  		if e = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit); E.Chk(e) {
  42  			return
  43  		}
  44  	}
  45  	return
  46  }
  47