github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/clusterspecifier/cluster_specifier.go (about)

     1  /*
     2   *
     3   * Copyright 2021 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * 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  
    19  // Package clusterspecifier contains the ClusterSpecifier interface and a registry for
    20  // storing and retrieving their implementations.
    21  package clusterspecifier
    22  
    23  import (
    24  	"github.com/golang/protobuf/proto"
    25  )
    26  
    27  // BalancerConfig is the Go Native JSON representation of a balancer
    28  // configuration.
    29  type BalancerConfig []map[string]interface{}
    30  
    31  // ClusterSpecifier defines the parsing functionality of a Cluster Specifier.
    32  type ClusterSpecifier interface {
    33  	// TypeURLs are the proto message types supported by this
    34  	// ClusterSpecifierPlugin. A ClusterSpecifierPlugin will be registered by
    35  	// each of its supported message types.
    36  	TypeURLs() []string
    37  	// ParseClusterSpecifierConfig parses the provided configuration
    38  	// proto.Message from the top level RDS configuration. The resulting
    39  	// BalancerConfig will be used as configuration for a child LB Policy of the
    40  	// Cluster Manager LB Policy.
    41  	ParseClusterSpecifierConfig(proto.Message) (BalancerConfig, error)
    42  }
    43  
    44  var (
    45  	// m is a map from scheme to filter.
    46  	m = make(map[string]ClusterSpecifier)
    47  )
    48  
    49  // Register registers the ClusterSpecifierPlugin to the ClusterSpecifier map.
    50  // cs.TypeURLs() will be used as the types for this ClusterSpecifierPlugin.
    51  //
    52  // NOTE: this function must only be called during initialization time (i.e. in
    53  // an init() function), and is not thread-safe. If multiple cluster specifier
    54  // plugins are registered with the same type URL, the one registered last will
    55  // take effect.
    56  func Register(cs ClusterSpecifier) {
    57  	for _, u := range cs.TypeURLs() {
    58  		m[u] = cs
    59  	}
    60  }
    61  
    62  // Get returns the ClusterSpecifier registered with typeURL.
    63  //
    64  // If no cluster specifier is registered with typeURL, nil will be returned.
    65  func Get(typeURL string) ClusterSpecifier {
    66  	return m[typeURL]
    67  }