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