github.com/pidato/unsafe@v0.1.4/capture/limits.go (about)

     1  package capture
     2  
     3  import (
     4  	"syscall"
     5  )
     6  
     7  // SetRLimitFiles temporarily changes the file descriptor resource limit while calling the given function.
     8  func SetRLimitFiles(limit uint64, call func(limit uint64)) (err error) {
     9  	var tmp syscall.Rlimit
    10  	if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &tmp); err != nil {
    11  		return nil
    12  	}
    13  	defer func() {
    14  		if err == nil {
    15  			err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &tmp)
    16  		}
    17  	}()
    18  
    19  	if err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &syscall.Rlimit{
    20  		Cur: limit,
    21  		Max: tmp.Max,
    22  	}); err != nil {
    23  		return err
    24  	}
    25  
    26  	call(limit)
    27  
    28  	return nil
    29  }