dubbo.apache.org/dubbo-go/v3@v3.1.1/common/extension/filter.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  	"github.com/pkg/errors"
    22  )
    23  
    24  import (
    25  	"dubbo.apache.org/dubbo-go/v3/filter"
    26  )
    27  
    28  var (
    29  	filters                  = make(map[string]func() filter.Filter)
    30  	rejectedExecutionHandler = make(map[string]func() filter.RejectedExecutionHandler)
    31  )
    32  
    33  // SetFilter sets the filter extension with @name
    34  // For example: hystrix/metrics/token/tracing/limit/...
    35  func SetFilter(name string, v func() filter.Filter) {
    36  	filters[name] = v
    37  }
    38  
    39  // GetFilter finds the filter extension with @name
    40  func GetFilter(name string) (filter.Filter, bool) {
    41  	if filters[name] == nil {
    42  		return nil, false
    43  	}
    44  	return filters[name](), true
    45  }
    46  
    47  // SetRejectedExecutionHandler sets the RejectedExecutionHandler with @name
    48  func SetRejectedExecutionHandler(name string, creator func() filter.RejectedExecutionHandler) {
    49  	rejectedExecutionHandler[name] = creator
    50  }
    51  
    52  // GetRejectedExecutionHandler finds the RejectedExecutionHandler with @name
    53  func GetRejectedExecutionHandler(name string) (filter.RejectedExecutionHandler, error) {
    54  	creator, ok := rejectedExecutionHandler[name]
    55  	if !ok {
    56  		return nil, errors.New("RejectedExecutionHandler for " + name + " is not existing, make sure you have import the package " +
    57  			"and you have register it by invoking extension.SetRejectedExecutionHandler.")
    58  	}
    59  	return creator(), nil
    60  }