dubbo.apache.org/dubbo-go/v3@v3.1.1/config/method_config.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package config
    19  
    20  import (
    21  	"fmt"
    22  	"strconv"
    23  )
    24  
    25  import (
    26  	"github.com/creasty/defaults"
    27  )
    28  
    29  import (
    30  	"dubbo.apache.org/dubbo-go/v3/common/constant"
    31  	"dubbo.apache.org/dubbo-go/v3/common/extension"
    32  )
    33  
    34  // MethodConfig defines method config
    35  type MethodConfig struct {
    36  	InterfaceId                 string
    37  	InterfaceName               string
    38  	Name                        string `yaml:"name"  json:"name,omitempty" property:"name"`
    39  	Retries                     string `yaml:"retries"  json:"retries,omitempty" property:"retries"`
    40  	LoadBalance                 string `yaml:"loadbalance"  json:"loadbalance,omitempty" property:"loadbalance"`
    41  	Weight                      int64  `yaml:"weight"  json:"weight,omitempty" property:"weight"`
    42  	TpsLimitInterval            string `yaml:"tps.limit.interval" json:"tps.limit.interval,omitempty" property:"tps.limit.interval"`
    43  	TpsLimitRate                string `yaml:"tps.limit.rate" json:"tps.limit.rate,omitempty" property:"tps.limit.rate"`
    44  	TpsLimitStrategy            string `yaml:"tps.limit.strategy" json:"tps.limit.strategy,omitempty" property:"tps.limit.strategy"`
    45  	ExecuteLimit                string `yaml:"execute.limit" json:"execute.limit,omitempty" property:"execute.limit"`
    46  	ExecuteLimitRejectedHandler string `yaml:"execute.limit.rejected.handler" json:"execute.limit.rejected.handler,omitempty" property:"execute.limit.rejected.handler"`
    47  	Sticky                      bool   `yaml:"sticky"   json:"sticky,omitempty" property:"sticky"`
    48  	RequestTimeout              string `yaml:"timeout"  json:"timeout,omitempty" property:"timeout"`
    49  }
    50  
    51  // nolint
    52  func (m *MethodConfig) Prefix() string {
    53  	if len(m.InterfaceId) != 0 {
    54  		return constant.Dubbo + "." + m.InterfaceName + "." + m.InterfaceId + "." + m.Name + "."
    55  	}
    56  
    57  	return constant.Dubbo + "." + m.InterfaceName + "." + m.Name + "."
    58  }
    59  
    60  func (m *MethodConfig) Init() error {
    61  	return m.check()
    62  }
    63  
    64  func initProviderMethodConfig(sc *ServiceConfig) error {
    65  	methods := sc.Methods
    66  	if methods == nil {
    67  		return nil
    68  	}
    69  	for _, method := range methods {
    70  		if err := method.check(); err != nil {
    71  			return err
    72  		}
    73  	}
    74  	sc.Methods = methods
    75  	return nil
    76  }
    77  
    78  // check set default value and verify
    79  func (m *MethodConfig) check() error {
    80  	qualifieldMethodName := m.InterfaceName + "#" + m.Name
    81  	if m.TpsLimitStrategy != "" {
    82  		_, err := extension.GetTpsLimitStrategyCreator(m.TpsLimitStrategy)
    83  		if err != nil {
    84  			panic(err)
    85  		}
    86  	}
    87  
    88  	if m.TpsLimitInterval != "" {
    89  		tpsLimitInterval, err := strconv.ParseInt(m.TpsLimitInterval, 0, 0)
    90  		if err != nil {
    91  			return fmt.Errorf("[MethodConfig] Cannot parse the configuration tps.limit.interval for method %s, please check your configuration", qualifieldMethodName)
    92  		}
    93  		if tpsLimitInterval < 0 {
    94  			return fmt.Errorf("[MethodConfig] The configuration tps.limit.interval for %s must be positive, please check your configuration", qualifieldMethodName)
    95  		}
    96  	}
    97  
    98  	if m.TpsLimitRate != "" {
    99  		tpsLimitRate, err := strconv.ParseInt(m.TpsLimitRate, 0, 0)
   100  		if err != nil {
   101  			return fmt.Errorf("[MethodConfig] Cannot parse the configuration tps.limit.rate for method %s, please check your configuration", qualifieldMethodName)
   102  		}
   103  		if tpsLimitRate < 0 {
   104  			return fmt.Errorf("[MethodConfig] The configuration tps.limit.rate for method %s must be positive, please check your configuration", qualifieldMethodName)
   105  		}
   106  	}
   107  
   108  	if err := defaults.Set(m); err != nil {
   109  		return err
   110  	}
   111  	return verify(m)
   112  }