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