github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zutil/utils_nowin.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package zutil
     5  
     6  import (
     7  	"syscall"
     8  )
     9  
    10  const (
    11  	darwinOpenMax = 10240
    12  )
    13  
    14  func IsDoubleClickStartUp() bool {
    15  	return false
    16  }
    17  
    18  func GetParentProcessName() (string, error) {
    19  	return "", nil
    20  }
    21  
    22  // MaxRlimit tries to set the resource limit RLIMIT_NOFILE to the max (hard limit)
    23  func MaxRlimit() (int, error) {
    24  	var lim syscall.Rlimit
    25  	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
    26  		return 0, err
    27  	}
    28  
    29  	if lim.Cur >= lim.Max {
    30  		return int(lim.Cur), nil
    31  	}
    32  
    33  	if IsMac() && lim.Max > darwinOpenMax {
    34  		lim.Max = darwinOpenMax
    35  	}
    36  
    37  	oldLimit := lim.Cur
    38  	lim.Cur = lim.Max
    39  	if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
    40  		return int(oldLimit), err
    41  	}
    42  
    43  	if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
    44  		return 0, err
    45  	}
    46  
    47  	return int(lim.Cur), nil
    48  }