github.com/cloudwego/frugal@v0.1.15/internal/opts/limits.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package opts
    18  
    19  import (
    20      `os`
    21      `strconv`
    22  )
    23  
    24  const (
    25      _DefaultMaxInlineDepth  = 5     // cutoff at 5 levels of inlining
    26      _DefaultMaxInlineILSize = 50000 // cutoff at 50k of IL instructions
    27  )
    28  
    29  var (
    30      MaxInlineDepth  = parseOrDefault("FRUGAL_MAX_INLINE_DEPTH", _DefaultMaxInlineDepth, 1)
    31      MaxInlineILSize = parseOrDefault("FRUGAL_MAX_INLINE_IL_SIZE", _DefaultMaxInlineILSize, 256)
    32  )
    33  
    34  func parseOrDefault(key string, def int, min int) int {
    35      if env := os.Getenv(key); env == "" {
    36          return def
    37      } else if val, err := strconv.ParseUint(env, 0, 64); err != nil {
    38          panic("frugal: invalid value for " + key)
    39      } else if ret := int(val); ret <= min {
    40          panic("frugal: value too small for " + key)
    41      } else {
    42          return ret
    43      }
    44  }