• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_LOAD_BALANCING_LB_POLICY_REGISTRY_H
18 #define GRPC_SRC_CORE_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 #include "src/core/load_balancing/lb_policy.h"
28 #include "src/core/load_balancing/lb_policy_factory.h"
29 #include "src/core/util/json/json.h"
30 #include "src/core/util/orphanable.h"
31 #include "src/core/util/ref_counted_ptr.h"
32 
33 namespace grpc_core {
34 
35 class LoadBalancingPolicyRegistry final {
36  public:
37   /// Methods used to create and populate the LoadBalancingPolicyRegistry.
38   /// NOT THREAD SAFE -- to be used only during global gRPC
39   /// initialization and shutdown.
40   class Builder final {
41    public:
42     /// Registers an LB policy factory.  The factory will be used to create an
43     /// LB policy whose name matches that of the factory.
44     void RegisterLoadBalancingPolicyFactory(
45         std::unique_ptr<LoadBalancingPolicyFactory> factory);
46 
47     LoadBalancingPolicyRegistry Build();
48 
49    private:
50     std::map<absl::string_view, std::unique_ptr<LoadBalancingPolicyFactory>>
51         factories_;
52   };
53 
54   /// Creates an LB policy of the type specified by \a name.
55   OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy(
56       absl::string_view name, LoadBalancingPolicy::Args args) const;
57 
58   /// Returns true if the LB policy factory specified by \a name exists in this
59   /// registry. If the load balancing policy requires a config to be specified
60   /// then sets \a requires_config to true.
61   bool LoadBalancingPolicyExists(absl::string_view name,
62                                  bool* requires_config) const;
63 
64   /// Returns a parsed object of the load balancing policy to be used from a
65   /// LoadBalancingConfig array \a json.
66   absl::StatusOr<RefCountedPtr<LoadBalancingPolicy::Config>>
67   ParseLoadBalancingConfig(const Json& json) const;
68 
69  private:
70   LoadBalancingPolicyFactory* GetLoadBalancingPolicyFactory(
71       absl::string_view name) const;
72   absl::StatusOr<Json::Object::const_iterator> ParseLoadBalancingConfigHelper(
73       const Json& lb_config_array) const;
74 
75   std::map<absl::string_view, std::unique_ptr<LoadBalancingPolicyFactory>>
76       factories_;
77 };
78 
79 }  // namespace grpc_core
80 
81 #endif  // GRPC_SRC_CORE_LOAD_BALANCING_LB_POLICY_REGISTRY_H
82