dubbo.apache.org/dubbo-go/v3@v3.1.1/common/extension/tps_limit.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 extension
    19  
    20  import (
    21  	"errors"
    22  )
    23  
    24  import (
    25  	"dubbo.apache.org/dubbo-go/v3/filter"
    26  )
    27  
    28  var (
    29  	tpsLimitStrategy = make(map[string]filter.TpsLimitStrategyCreator)
    30  	tpsLimiter       = make(map[string]func() filter.TpsLimiter)
    31  )
    32  
    33  // SetTpsLimiter sets the TpsLimiter with @name
    34  func SetTpsLimiter(name string, creator func() filter.TpsLimiter) {
    35  	tpsLimiter[name] = creator
    36  }
    37  
    38  // GetTpsLimiter finds the TpsLimiter with @name
    39  func GetTpsLimiter(name string) (filter.TpsLimiter, error) {
    40  	creator, ok := tpsLimiter[name]
    41  	if !ok {
    42  		return nil, errors.New("TpsLimiter for " + name + " is not existing, make sure you have import the package " +
    43  			"and you have register it by invoking extension.SetTpsLimiter.")
    44  	}
    45  	return creator(), nil
    46  }
    47  
    48  // SetTpsLimitStrategy sets the TpsLimitStrategyCreator with @name
    49  func SetTpsLimitStrategy(name string, creator filter.TpsLimitStrategyCreator) {
    50  	tpsLimitStrategy[name] = creator
    51  }
    52  
    53  // GetTpsLimitStrategyCreator finds the TpsLimitStrategyCreator with @name
    54  func GetTpsLimitStrategyCreator(name string) (filter.TpsLimitStrategyCreator, error) {
    55  	creator, ok := tpsLimitStrategy[name]
    56  	if !ok {
    57  		return nil, errors.New("TpsLimitStrategy for " + name + " is not existing, make sure you have import the package " +
    58  			"and you have register it by invoking extension.SetTpsLimitStrategy.")
    59  	}
    60  	return creator, nil
    61  }