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_SRC_CORE_CLIENT_CHANNEL_GLOBAL_SUBCHANNEL_POOL_H 20 #define GRPC_SRC_CORE_CLIENT_CHANNEL_GLOBAL_SUBCHANNEL_POOL_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <map> 25 26 #include "absl/base/thread_annotations.h" 27 #include "src/core/client_channel/subchannel_pool_interface.h" 28 #include "src/core/util/ref_counted_ptr.h" 29 #include "src/core/util/sync.h" 30 31 namespace grpc_core { 32 33 // The global subchannel pool. It shares subchannels among channels. There 34 // should be only one instance of this class. 35 class GlobalSubchannelPool final : public SubchannelPoolInterface { 36 public: 37 // Gets the singleton instance. 38 static RefCountedPtr<GlobalSubchannelPool> instance(); 39 40 // Implements interface methods. 41 RefCountedPtr<Subchannel> RegisterSubchannel( 42 const SubchannelKey& key, RefCountedPtr<Subchannel> constructed) override 43 ABSL_LOCKS_EXCLUDED(mu_); 44 void UnregisterSubchannel(const SubchannelKey& key, 45 Subchannel* subchannel) override 46 ABSL_LOCKS_EXCLUDED(mu_); 47 RefCountedPtr<Subchannel> FindSubchannel(const SubchannelKey& key) override 48 ABSL_LOCKS_EXCLUDED(mu_); 49 50 private: GlobalSubchannelPool()51 GlobalSubchannelPool() {} ~GlobalSubchannelPool()52 ~GlobalSubchannelPool() override {} 53 54 // A map from subchannel key to subchannel. 55 std::map<SubchannelKey, Subchannel*> subchannel_map_ ABSL_GUARDED_BY(mu_); 56 // To protect subchannel_map_. 57 Mutex mu_; 58 }; 59 60 } // namespace grpc_core 61 62 #endif // GRPC_SRC_CORE_CLIENT_CHANNEL_GLOBAL_SUBCHANNEL_POOL_H 63