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