1 /* 2 * 3 * Copyright 2018 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H 20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include "src/core/lib/avl/avl.h" 25 #include "src/core/lib/channel/channel_args.h" 26 #include "src/core/lib/debug/trace.h" 27 #include "src/core/lib/gprpp/ref_counted.h" 28 29 namespace grpc_core { 30 31 class Subchannel; 32 33 extern TraceFlag grpc_subchannel_pool_trace; 34 35 // A key that can uniquely identify a subchannel. 36 class SubchannelKey { 37 public: 38 explicit SubchannelKey(const grpc_channel_args* args); 39 ~SubchannelKey(); 40 41 // Copyable. 42 SubchannelKey(const SubchannelKey& other); 43 SubchannelKey& operator=(const SubchannelKey& other); 44 // Not movable. 45 SubchannelKey(SubchannelKey&&) = delete; 46 SubchannelKey& operator=(SubchannelKey&&) = delete; 47 48 int Cmp(const SubchannelKey& other) const; 49 50 private: 51 // Initializes the subchannel key with the given \a args and the function to 52 // copy channel args. 53 void Init( 54 const grpc_channel_args* args, 55 grpc_channel_args* (*copy_channel_args)(const grpc_channel_args* args)); 56 57 const grpc_channel_args* args_; 58 }; 59 60 // Interface for subchannel pool. 61 // TODO(juanlishen): This refcounting mechanism may lead to memory leak. 62 // To solve that, we should force polling to flush any pending callbacks, then 63 // shut down safely. See https://github.com/grpc/grpc/issues/12560. 64 class SubchannelPoolInterface : public RefCounted<SubchannelPoolInterface> { 65 public: SubchannelPoolInterface()66 SubchannelPoolInterface() 67 : RefCounted(GRPC_TRACE_FLAG_ENABLED(grpc_subchannel_pool_trace) 68 ? "SubchannelPoolInterface" 69 : nullptr) {} ~SubchannelPoolInterface()70 ~SubchannelPoolInterface() override {} 71 72 // Registers a subchannel against a key. Returns the subchannel registered 73 // with \a key, which may be different from \a constructed because we reuse 74 // (instead of update) any existing subchannel already registered with \a key. 75 virtual Subchannel* RegisterSubchannel(SubchannelKey* key, 76 Subchannel* constructed) = 0; 77 78 // Removes the registered subchannel found by \a key. 79 virtual void UnregisterSubchannel(SubchannelKey* key) = 0; 80 81 // Finds the subchannel registered for the given subchannel key. Returns NULL 82 // if no such channel exists. Thread-safe. 83 virtual Subchannel* FindSubchannel(SubchannelKey* key) = 0; 84 85 // Creates a channel arg from \a subchannel pool. 86 static grpc_arg CreateChannelArg(SubchannelPoolInterface* subchannel_pool); 87 88 // Gets the subchannel pool from the channel args. 89 static SubchannelPoolInterface* GetSubchannelPoolFromChannelArgs( 90 const grpc_channel_args* args); 91 }; 92 93 } // namespace grpc_core 94 95 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SUBCHANNEL_POOL_INTERFACE_H */ 96