github.com/gohugoio/hugo@v0.88.1/commands/limit_darwin.go (about)

     1  // Copyright 2018 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package commands
    15  
    16  import (
    17  	"syscall"
    18  
    19  	"github.com/spf13/cobra"
    20  	jww "github.com/spf13/jwalterweatherman"
    21  )
    22  
    23  var _ cmder = (*limitCmd)(nil)
    24  
    25  type limitCmd struct {
    26  	*baseCmd
    27  }
    28  
    29  func newLimitCmd() *limitCmd {
    30  	ccmd := &cobra.Command{
    31  		Use:   "ulimit",
    32  		Short: "Check system ulimit settings",
    33  		Long: `Hugo will inspect the current ulimit settings on the system.
    34  This is primarily to ensure that Hugo can watch enough files on some OSs`,
    35  		RunE: func(cmd *cobra.Command, args []string) error {
    36  			var rLimit syscall.Rlimit
    37  			err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    38  			if err != nil {
    39  				return newSystemError("Error Getting rlimit ", err)
    40  			}
    41  
    42  			jww.FEEDBACK.Println("Current rLimit:", rLimit)
    43  
    44  			if rLimit.Cur >= newRlimit {
    45  				return nil
    46  			}
    47  
    48  			jww.FEEDBACK.Println("Attempting to increase limit")
    49  			rLimit.Cur = newRlimit
    50  			err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    51  			if err != nil {
    52  				return newSystemError("Error Setting rLimit ", err)
    53  			}
    54  			err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    55  			if err != nil {
    56  				return newSystemError("Error Getting rLimit ", err)
    57  			}
    58  			jww.FEEDBACK.Println("rLimit after change:", rLimit)
    59  
    60  			return nil
    61  		},
    62  	}
    63  
    64  	return &limitCmd{baseCmd: newBaseCmd(ccmd)}
    65  }
    66  
    67  const newRlimit = 10240
    68  
    69  func tweakLimit() {
    70  	var rLimit syscall.Rlimit
    71  	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    72  	if err != nil {
    73  		jww.WARN.Println("Unable to get rlimit:", err)
    74  		return
    75  	}
    76  	if rLimit.Cur < newRlimit {
    77  		rLimit.Cur = newRlimit
    78  		err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    79  		if err != nil {
    80  			// This may not succeed, see https://github.com/golang/go/issues/30401
    81  			jww.INFO.Println("Unable to increase number of open files limit:", err)
    82  		}
    83  	}
    84  }