1 // 2 // Copyright 2015 gRPC authors. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef GRPC_SRC_CORE_LIB_LOAD_BALANCING_LB_POLICY_REGISTRY_H 18 #define GRPC_SRC_CORE_LIB_LOAD_BALANCING_LB_POLICY_REGISTRY_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include <map> 23 #include <memory> 24 25 #include "absl/status/statusor.h" 26 #include "absl/strings/string_view.h" 27 28 #include "src/core/lib/gprpp/orphanable.h" 29 #include "src/core/lib/gprpp/ref_counted_ptr.h" 30 #include "src/core/lib/json/json.h" 31 #include "src/core/lib/load_balancing/lb_policy.h" 32 #include "src/core/lib/load_balancing/lb_policy_factory.h" 33 34 namespace grpc_core { 35 36 class LoadBalancingPolicyRegistry { 37 public: 38 /// Methods used to create and populate the LoadBalancingPolicyRegistry. 39 /// NOT THREAD SAFE -- to be used only during global gRPC 40 /// initialization and shutdown. 41 class Builder { 42 public: 43 /// Registers an LB policy factory. The factory will be used to create an 44 /// LB policy whose name matches that of the factory. 45 void RegisterLoadBalancingPolicyFactory( 46 std::unique_ptr<LoadBalancingPolicyFactory> factory); 47 48 LoadBalancingPolicyRegistry Build(); 49 50 private: 51 std::map<absl::string_view, std::unique_ptr<LoadBalancingPolicyFactory>> 52 factories_; 53 }; 54 55 /// Creates an LB policy of the type specified by \a name. 56 OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( 57 absl::string_view name, LoadBalancingPolicy::Args args) const; 58 59 /// Returns true if the LB policy factory specified by \a name exists in this 60 /// registry. If the load balancing policy requires a config to be specified 61 /// then sets \a requires_config to true. 62 bool LoadBalancingPolicyExists(absl::string_view name, 63 bool* requires_config) const; 64 65 /// Returns a parsed object of the load balancing policy to be used from a 66 /// LoadBalancingConfig array \a json. 67 absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>> 68 ParseLoadBalancingConfig(const Json& json) const; 69 70 private: 71 LoadBalancingPolicyFactory* GetLoadBalancingPolicyFactory( 72 absl::string_view name) const; 73 absl::StatusOr<Json::Object::const_iterator> ParseLoadBalancingConfigHelper( 74 const Json& lb_config_array) const; 75 76 std::map<absl::string_view, std::unique_ptr<LoadBalancingPolicyFactory>> 77 factories_; 78 }; 79 80 } // namespace grpc_core 81 82 #endif // GRPC_SRC_CORE_LIB_LOAD_BALANCING_LB_POLICY_REGISTRY_H 83