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 #include "src/core/client_channel/local_subchannel_pool.h"
20
21 #include <grpc/support/port_platform.h>
22
23 #include <utility>
24
25 #include "absl/log/check.h"
26 #include "src/core/client_channel/subchannel.h"
27
28 namespace grpc_core {
29
RegisterSubchannel(const SubchannelKey & key,RefCountedPtr<Subchannel> constructed)30 RefCountedPtr<Subchannel> LocalSubchannelPool::RegisterSubchannel(
31 const SubchannelKey& key, RefCountedPtr<Subchannel> constructed) {
32 auto it = subchannel_map_.find(key);
33 // Because this pool is only accessed under the client channel's work
34 // serializer, and because FindSubchannel is checked before invoking
35 // RegisterSubchannel, no such subchannel should exist in the map.
36 CHECK(it == subchannel_map_.end());
37 subchannel_map_[key] = constructed.get();
38 return constructed;
39 }
40
UnregisterSubchannel(const SubchannelKey & key,Subchannel * subchannel)41 void LocalSubchannelPool::UnregisterSubchannel(const SubchannelKey& key,
42 Subchannel* subchannel) {
43 auto it = subchannel_map_.find(key);
44 // Because this subchannel pool is accessed only under the client
45 // channel's work serializer, any subchannel created by RegisterSubchannel
46 // will be deleted from the map in UnregisterSubchannel.
47 CHECK(it != subchannel_map_.end());
48 CHECK(it->second == subchannel);
49 subchannel_map_.erase(it);
50 }
51
FindSubchannel(const SubchannelKey & key)52 RefCountedPtr<Subchannel> LocalSubchannelPool::FindSubchannel(
53 const SubchannelKey& key) {
54 auto it = subchannel_map_.find(key);
55 if (it == subchannel_map_.end()) return nullptr;
56 return it->second->Ref();
57 }
58
59 } // namespace grpc_core
60