1 // 2 // Copyright 2018 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_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_CHILD_POLICY_HANDLER_H 18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_CHILD_POLICY_HANDLER_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include "src/core/ext/filters/client_channel/lb_policy.h" 23 #include "src/core/lib/debug/trace.h" 24 #include "src/core/lib/gprpp/orphanable.h" 25 26 namespace grpc_core { 27 28 // A class that makes it easy to gracefully switch child policies. 29 // 30 // Callers should instantiate this instead of using 31 // LoadBalancingPolicyRegistry::CreateLoadBalancingPolicy(). Once 32 // instantiated, this object will automatically take care of 33 // constructing the child policy as needed upon receiving an update. 34 class ChildPolicyHandler : public LoadBalancingPolicy { 35 public: ChildPolicyHandler(Args args,TraceFlag * tracer)36 ChildPolicyHandler(Args args, TraceFlag* tracer) 37 : LoadBalancingPolicy(std::move(args)), tracer_(tracer) {} 38 name()39 const char* name() const override { return "child_policy_handler"; } 40 41 void UpdateLocked(UpdateArgs args) override; 42 void ExitIdleLocked() override; 43 void ResetBackoffLocked() override; 44 45 // Returns true if transitioning from the old config to the new config 46 // requires instantiating a new policy object. 47 virtual bool ConfigChangeRequiresNewPolicyInstance( 48 LoadBalancingPolicy::Config* old_config, 49 LoadBalancingPolicy::Config* new_config) const; 50 51 // Instantiates a new policy of the specified name. 52 // May be overridden by subclasses to avoid recursion when an LB 53 // policy factory returns a ChildPolicyHandler. 54 virtual OrphanablePtr<LoadBalancingPolicy> CreateLoadBalancingPolicy( 55 const char* name, LoadBalancingPolicy::Args args) const; 56 57 private: 58 class Helper; 59 60 void ShutdownLocked() override; 61 62 OrphanablePtr<LoadBalancingPolicy> CreateChildPolicy( 63 const char* child_policy_name, const grpc_channel_args& args); 64 65 // Passed in from caller at construction time. 66 TraceFlag* tracer_; 67 68 bool shutting_down_ = false; 69 70 // The most recent config passed to UpdateLocked(). 71 // If pending_child_policy_ is non-null, this is the config passed to 72 // pending_child_policy_; otherwise, it's the config passed to child_policy_. 73 RefCountedPtr<LoadBalancingPolicy::Config> current_config_; 74 75 // Child LB policy. 76 OrphanablePtr<LoadBalancingPolicy> child_policy_; 77 OrphanablePtr<LoadBalancingPolicy> pending_child_policy_; 78 }; 79 80 } // namespace grpc_core 81 82 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_CHILD_POLICY_HANDLER_H \ 83 */ 84