• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2023 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_DELEGATING_HELPER_H
18 #define GRPC_SRC_CORE_LOAD_BALANCING_DELEGATING_HELPER_H
19 
20 #include <grpc/event_engine/event_engine.h>
21 #include <grpc/grpc.h>
22 #include <grpc/impl/connectivity_state.h>
23 #include <grpc/support/port_platform.h>
24 
25 #include <utility>
26 
27 #include "absl/status/status.h"
28 #include "absl/strings/string_view.h"
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/iomgr/resolved_address.h"
31 #include "src/core/lib/security/credentials/credentials.h"
32 #include "src/core/load_balancing/lb_policy.h"
33 #include "src/core/load_balancing/subchannel_interface.h"
34 #include "src/core/util/debug_location.h"
35 #include "src/core/util/ref_counted_ptr.h"
36 
37 namespace grpc_core {
38 
39 /// A helper for use in parent policies.  All methods delegate to a
40 /// parent policy's helper unless otherwise overridden.
41 class LoadBalancingPolicy::DelegatingChannelControlHelper
42     : public LoadBalancingPolicy::ChannelControlHelper {
43  public:
CreateSubchannel(const grpc_resolved_address & address,const ChannelArgs & per_address_args,const ChannelArgs & args)44   RefCountedPtr<SubchannelInterface> CreateSubchannel(
45       const grpc_resolved_address& address, const ChannelArgs& per_address_args,
46       const ChannelArgs& args) override {
47     return parent_helper()->CreateSubchannel(address, per_address_args, args);
48   }
49 
UpdateState(grpc_connectivity_state state,const absl::Status & status,RefCountedPtr<SubchannelPicker> picker)50   void UpdateState(grpc_connectivity_state state, const absl::Status& status,
51                    RefCountedPtr<SubchannelPicker> picker) override {
52     parent_helper()->UpdateState(state, status, std::move(picker));
53   }
54 
RequestReresolution()55   void RequestReresolution() override {
56     parent_helper()->RequestReresolution();
57   }
58 
GetTarget()59   absl::string_view GetTarget() override {
60     return parent_helper()->GetTarget();
61   }
62 
GetAuthority()63   absl::string_view GetAuthority() override {
64     return parent_helper()->GetAuthority();
65   }
66 
GetChannelCredentials()67   RefCountedPtr<grpc_channel_credentials> GetChannelCredentials() override {
68     return parent_helper()->GetChannelCredentials();
69   }
70 
GetUnsafeChannelCredentials()71   RefCountedPtr<grpc_channel_credentials> GetUnsafeChannelCredentials()
72       override {
73     return parent_helper()->GetUnsafeChannelCredentials();
74   }
75 
GetEventEngine()76   grpc_event_engine::experimental::EventEngine* GetEventEngine() override {
77     return parent_helper()->GetEventEngine();
78   }
79 
GetStatsPluginGroup()80   GlobalStatsPluginRegistry::StatsPluginGroup& GetStatsPluginGroup() override {
81     return parent_helper()->GetStatsPluginGroup();
82   }
83 
AddTraceEvent(TraceSeverity severity,absl::string_view message)84   void AddTraceEvent(TraceSeverity severity,
85                      absl::string_view message) override {
86     parent_helper()->AddTraceEvent(severity, message);
87   }
88 
89  private:
90   /// Returns the parent helper that we should delegate to by default.
91   virtual ChannelControlHelper* parent_helper() const = 0;
92 };
93 
94 /// A delegating helper that owns a ref to the parent policy.
95 template <typename ParentPolicy>
96 class LoadBalancingPolicy::ParentOwningDelegatingChannelControlHelper
97     : public LoadBalancingPolicy::DelegatingChannelControlHelper {
98  public:
ParentOwningDelegatingChannelControlHelper(RefCountedPtr<ParentPolicy> parent)99   explicit ParentOwningDelegatingChannelControlHelper(
100       RefCountedPtr<ParentPolicy> parent)
101       : parent_(std::move(parent)) {}
102 
~ParentOwningDelegatingChannelControlHelper()103   ~ParentOwningDelegatingChannelControlHelper() override {
104     parent_.reset(DEBUG_LOCATION, "Helper");
105   }
106 
107  protected:
parent()108   ParentPolicy* parent() const {
109     return static_cast<ParentPolicy*>(parent_.get());
110   }
111 
parent_helper()112   ChannelControlHelper* parent_helper() const override {
113     return parent_->channel_control_helper();
114   }
115 
116  private:
117   RefCountedPtr<LoadBalancingPolicy> parent_;
118 };
119 
120 }  // namespace grpc_core
121 
122 #endif  // GRPC_SRC_CORE_LOAD_BALANCING_DELEGATING_HELPER_H
123