gitee.com/mirrors/Hugo-Go@v0.47.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  			jww.FEEDBACK.Println("Attempting to increase limit")
    45  			rLimit.Max = 999999
    46  			rLimit.Cur = 999999
    47  			err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    48  			if err != nil {
    49  				return newSystemError("Error Setting rLimit ", err)
    50  			}
    51  			err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    52  			if err != nil {
    53  				return newSystemError("Error Getting rLimit ", err)
    54  			}
    55  			jww.FEEDBACK.Println("rLimit after change:", rLimit)
    56  
    57  			return nil
    58  		},
    59  	}
    60  
    61  	return &limitCmd{baseCmd: newBaseCmd(ccmd)}
    62  }
    63  
    64  func tweakLimit() {
    65  	var rLimit syscall.Rlimit
    66  	err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    67  	if err != nil {
    68  		jww.ERROR.Println("Unable to obtain rLimit", err)
    69  	}
    70  	if rLimit.Cur < rLimit.Max {
    71  		rLimit.Max = 64000
    72  		rLimit.Cur = 64000
    73  		err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
    74  		if err != nil {
    75  			jww.WARN.Println("Unable to increase number of open files limit", err)
    76  		}
    77  	}
    78  }