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_GLOBAL_SUBCHANNEL_POOL_H 20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_GLOBAL_SUBCHANNEL_POOL_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <map> 25 26 #include "src/core/ext/filters/client_channel/subchannel_pool_interface.h" 27 #include "src/core/lib/gprpp/sync.h" 28 29 namespace grpc_core { 30 31 // The global subchannel pool. It shares subchannels among channels. There 32 // should be only one instance of this class. Init() should be called once at 33 // the filter initialization time; Shutdown() should be called once at the 34 // filter shutdown time. 35 // TODO(juanlishen): Enable subchannel retention. 36 class GlobalSubchannelPool final : public SubchannelPoolInterface { 37 public: 38 // The ctor and dtor are not intended to use directly. GlobalSubchannelPool()39 GlobalSubchannelPool() {} ~GlobalSubchannelPool()40 ~GlobalSubchannelPool() override {} 41 42 // Should be called exactly once at filter initialization time. 43 static void Init(); 44 // Should be called exactly once at filter shutdown time. 45 static void Shutdown(); 46 47 // Gets the singleton instance. 48 static RefCountedPtr<GlobalSubchannelPool> instance(); 49 50 // Implements interface methods. 51 RefCountedPtr<Subchannel> RegisterSubchannel( 52 const SubchannelKey& key, RefCountedPtr<Subchannel> constructed) override 53 ABSL_LOCKS_EXCLUDED(mu_); 54 void UnregisterSubchannel(const SubchannelKey& key, 55 Subchannel* subchannel) override 56 ABSL_LOCKS_EXCLUDED(mu_); 57 RefCountedPtr<Subchannel> FindSubchannel(const SubchannelKey& key) override 58 ABSL_LOCKS_EXCLUDED(mu_); 59 60 private: 61 // The singleton instance. (It's a pointer to RefCountedPtr so that this 62 // non-local static object can be trivially destructible.) 63 static RefCountedPtr<GlobalSubchannelPool>* instance_; 64 65 // A map from subchannel key to subchannel. 66 std::map<SubchannelKey, Subchannel*> subchannel_map_ ABSL_GUARDED_BY(mu_); 67 // To protect subchannel_map_. 68 Mutex mu_; 69 }; 70 71 } // namespace grpc_core 72 73 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_GLOBAL_SUBCHANNEL_POOL_H */ 74